<?php
namespace Maxia\MaxiaListingVariants6\Service;
use Monolog\Logger;
use Doctrine\DBAL\Connection;
use Shopware\Core\PlatformRequest;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class ListingVariantsLoaderFactory
{
public static function create(
string $class,
ContainerInterface $container,
Logger $logger,
RequestStack $requestStack,
EventDispatcherInterface $eventDispatcher,
Connection $dbConnection,
ProductConfiguratorLoaderInterface $configuratorLoader,
VariantMappingLoaderInterface $variantMappingLoader,
ProductCombinationFinder $combinationFinder,
$productRepository,
$mediaRepository,
$productMediaRepository,
VariantDisplayConfigLoader $variantDisplayConfigLoader,
ConfigService $configService
): ListingVariantsLoaderInterface
{
if (self::isCacheEnabled($requestStack, $configService)) {
if ($container->has('Maxia\MaxiaListingVariants6\Service\CachedProductConfiguratorLoader')) {
$configuratorLoader = $container
->get('Maxia\MaxiaListingVariants6\Service\CachedProductConfiguratorLoader');
}
if ($container->has('Maxia\MaxiaListingVariants6\Service\CachedVariantMappingLoader')) {
$variantMappingLoader = $container
->get('Maxia\MaxiaListingVariants6\Service\CachedVariantMappingLoader');
}
}
return new $class(
$container,
$logger,
$requestStack,
$eventDispatcher,
$dbConnection,
$configuratorLoader,
$variantMappingLoader,
$combinationFinder,
$productRepository,
$mediaRepository,
$productMediaRepository,
$variantDisplayConfigLoader,
$configService
);
}
protected static function isCacheEnabled(RequestStack $requestStack, ConfigService $configService)
{
if (!$requestStack->getMainRequest()) {
return false;
}
$salesChannelId = $requestStack->getMainRequest()
->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
if (!$salesChannelId) {
return false;
}
return $configService->getBaseConfig($salesChannelId)->isCacheEnabled();
}
}