custom/plugins/MaxiaListingVariants6/src/Service/CachedVariantMappingLoader.php line 58

Open in your IDE?
  1. <?php
  2. namespace Maxia\MaxiaListingVariants6\Service;
  3. use Monolog\Logger;
  4. use Shopware\Core\Content\Product\ProductEntity;
  5. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  6. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  7. use Shopware\Core\Framework\Adapter\Cache\CacheCompressor;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  9. use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  13. class CachedVariantMappingLoader implements VariantMappingLoaderInterface {
  14.     protected Logger $logger;
  15.     protected RequestStack $requestStack;
  16.     protected VariantMappingLoaderInterface $variantMappingLoader;
  17.     protected TagAwareAdapterInterface $cache;
  18.     protected EntityCacheKeyGenerator $generator;
  19.     protected AbstractCacheTracer $tracer;
  20.     public function __construct(
  21.         Logger $logger,
  22.         RequestStack $requestStack,
  23.         VariantMappingLoaderInterface $variantMappingLoader,
  24.         TagAwareAdapterInterface $cache,
  25.         EntityCacheKeyGenerator $generator,
  26.         AbstractCacheTracer $tracer)
  27.     {
  28.         $this->logger $logger;
  29.         $this->requestStack $requestStack;
  30.         $this->cache $cache;
  31.         $this->generator $generator;
  32.         $this->tracer $tracer;
  33.         $this->variantMappingLoader $variantMappingLoader;
  34.     }
  35.     public function loadAllMappings(ProductEntity $productSalesChannelContext $context): array
  36.     {
  37.         $item $this->cache->getItem(
  38.             $this->generateKey($product$context)
  39.         );
  40.         try {
  41.             if ($item->isHit() && $item->get()) {
  42.                 return CacheCompressor::uncompress($item);
  43.             }
  44.         } catch (\Throwable $e) {
  45.             $this->logger->error($e->getMessage());
  46.         }
  47.         $name = static::buildName($product->getId());
  48.         $result $this->tracer->trace($name, function () use ($product$context) {
  49.             return $this->variantMappingLoader->loadAllMappings($product$context);
  50.         });
  51.         $item CacheCompressor::compress($item$result);
  52.         $item->tag($this->generateTags($product->getId(), $context));
  53.         $this->cache->save($item);
  54.         return $result;
  55.     }
  56.     public static function buildName(string $productId): string
  57.     {
  58.         return 'maxia-variant-mappings-' $productId;
  59.     }
  60.     protected function generateKey(SalesChannelProductEntity $productSalesChannelContext $context): string
  61.     {
  62.         $parts = [
  63.             static::buildName($product->getId()),
  64.             $this->requestStack->getMainRequest()->query->get('expandOptions'false),
  65.             $this->generator->getSalesChannelContextHash($context),
  66.         ];
  67.         return md5(JsonFieldSerializer::encodeJson($parts));
  68.     }
  69.     private function generateTags(string $productId,
  70.         SalesChannelContext $context): array
  71.     {
  72.         $tags array_merge(
  73.             $this->tracer->get(self::buildName($productId)),
  74.             [self::buildName($productId), 'maxia-variant-mappings'],
  75.         );
  76.         return array_unique($tags);
  77.     }
  78. }