vendor/shopware/core/Content/Product/SalesChannel/CrossSelling/CachedProductCrossSellingRoute.php line 67

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\CrossSelling;
  3. use Shopware\Core\Content\Product\Events\CrossSellingRouteCacheKeyEvent;
  4. use Shopware\Core\Content\Product\Events\CrossSellingRouteCacheTagsEvent;
  5. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  6. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Cache\EntityCacheKeyGenerator;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\RuleAreas;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\Log\Package;
  11. use Shopware\Core\Framework\Util\Json;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Contracts\Cache\CacheInterface;
  16. use Symfony\Contracts\Cache\ItemInterface;
  17. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  18. #[Route(defaults: ['_routeScope' => ['store-api']])]
  19. #[Package('inventory')]
  20. class CachedProductCrossSellingRoute extends AbstractProductCrossSellingRoute
  21. {
  22.     /**
  23.      * @internal
  24.      *
  25.      * @param AbstractCacheTracer<ProductCrossSellingRouteResponse> $tracer
  26.      * @param array<string> $states
  27.      */
  28.     public function __construct(
  29.         private readonly AbstractProductCrossSellingRoute $decorated,
  30.         private readonly CacheInterface $cache,
  31.         private readonly EntityCacheKeyGenerator $generator,
  32.         private readonly AbstractCacheTracer $tracer,
  33.         private readonly EventDispatcherInterface $dispatcher,
  34.         private readonly array $states
  35.     ) {
  36.     }
  37.     public function getDecorated(): AbstractProductCrossSellingRoute
  38.     {
  39.         return $this->decorated;
  40.     }
  41.     public static function buildName(string $id): string
  42.     {
  43.         return 'cross-selling-route-' $id;
  44.     }
  45.     #[Route(path'/store-api/product/{productId}/cross-selling'name'store-api.product.cross-selling'methods: ['POST'], defaults: ['_entity' => 'product'])]
  46.     public function load(string $productIdRequest $requestSalesChannelContext $contextCriteria $criteria): ProductCrossSellingRouteResponse
  47.     {
  48.         if ($context->hasState(...$this->states)) {
  49.             return $this->getDecorated()->load($productId$request$context$criteria);
  50.         }
  51.         $key $this->generateKey($productId$request$context$criteria);
  52.         if ($key === null) {
  53.             return $this->getDecorated()->load($productId$request$context$criteria);
  54.         }
  55.         $value $this->cache->get($key, function (ItemInterface $item) use ($productId$request$context$criteria) {
  56.             $name self::buildName($productId);
  57.             $response $this->tracer->trace($name, fn () => $this->getDecorated()->load($productId$request$context$criteria));
  58.             $item->tag($this->generateTags($productId$request$response$context$criteria));
  59.             return CacheValueCompressor::compress($response);
  60.         });
  61.         return CacheValueCompressor::uncompress($value);
  62.     }
  63.     private function generateKey(string $productIdRequest $requestSalesChannelContext $contextCriteria $criteria): ?string
  64.     {
  65.         $parts = [
  66.             $this->generator->getCriteriaHash($criteria),
  67.             $this->generator->getSalesChannelContextHash($context, [RuleAreas::PRODUCT_AREA]),
  68.         ];
  69.         $event = new CrossSellingRouteCacheKeyEvent($productId$parts$request$context$criteria);
  70.         $this->dispatcher->dispatch($event);
  71.         if (!$event->shouldCache()) {
  72.             return null;
  73.         }
  74.         return self::buildName($productId) . '-' md5(Json::encode($event->getParts()));
  75.     }
  76.     /**
  77.      * @return array<string>
  78.      */
  79.     private function generateTags(string $productIdRequest $requestProductCrossSellingRouteResponse $responseSalesChannelContext $contextCriteria $criteria): array
  80.     {
  81.         $tags array_merge(
  82.             $this->tracer->get(self::buildName($productId)),
  83.             $this->extractStreamTags($response),
  84.             $this->extractProductIds($response),
  85.             [self::buildName($productId)]
  86.         );
  87.         $event = new CrossSellingRouteCacheTagsEvent($productId$tags$request$response$context$criteria);
  88.         $this->dispatcher->dispatch($event);
  89.         return array_unique(array_filter($event->getTags()));
  90.     }
  91.     /**
  92.      * @return array<string>
  93.      */
  94.     private function extractStreamTags(ProductCrossSellingRouteResponse $response): array
  95.     {
  96.         $ids = [];
  97.         foreach ($response->getResult() as $element) {
  98.             $ids[] = $element->getStreamId();
  99.         }
  100.         $ids array_unique(array_filter($ids));
  101.         return array_map(EntityCacheKeyGenerator::buildStreamTag(...), $ids);
  102.     }
  103.     /**
  104.      * @return array<string>
  105.      */
  106.     private function extractProductIds(ProductCrossSellingRouteResponse $response): array
  107.     {
  108.         $ids = [];
  109.         foreach ($response->getResult() as $element) {
  110.             $ids = [...$ids, ...$element->getProducts()->getIds()];
  111.         }
  112.         $ids array_unique(array_filter($ids));
  113.         return array_map(EntityCacheKeyGenerator::buildProductTag(...), $ids);
  114.     }
  115. }