vendor/shopware/core/System/SalesChannel/Context/CachedSalesChannelContextFactory.php line 44

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\SalesChannel\Context;
  3. use Shopware\Core\Framework\Adapter\Cache\AbstractCacheTracer;
  4. use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
  5. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  6. use Symfony\Contracts\Cache\CacheInterface;
  7. use Symfony\Contracts\Cache\ItemInterface;
  8. class CachedSalesChannelContextFactory extends AbstractSalesChannelContextFactory
  9. {
  10.     public const ALL_TAG 'sales-channel-context';
  11.     private AbstractSalesChannelContextFactory $decorated;
  12.     private CacheInterface $cache;
  13.     /**
  14.      * @var AbstractCacheTracer<SalesChannelContext>
  15.      */
  16.     private AbstractCacheTracer $tracer;
  17.     /**
  18.      * @internal
  19.      *
  20.      * @param AbstractCacheTracer<SalesChannelContext> $tracer
  21.      */
  22.     public function __construct(
  23.         AbstractSalesChannelContextFactory $decorated,
  24.         CacheInterface $cache,
  25.         AbstractCacheTracer $tracer
  26.     ) {
  27.         $this->decorated $decorated;
  28.         $this->cache $cache;
  29.         $this->tracer $tracer;
  30.     }
  31.     public function getDecorated(): AbstractSalesChannelContextFactory
  32.     {
  33.         return $this->decorated;
  34.     }
  35.     public function create(string $tokenstring $salesChannelId, array $options = []): SalesChannelContext
  36.     {
  37.         $name self::buildName($salesChannelId);
  38.         if (!$this->isCacheable($options)) {
  39.             return $this->getDecorated()->create($token$salesChannelId$options);
  40.         }
  41.         ksort($options);
  42.         $key implode('-', [$namemd5(json_encode($options\JSON_THROW_ON_ERROR))]);
  43.         $value $this->cache->get($key, function (ItemInterface $item) use ($name$token$salesChannelId$options) {
  44.             $context $this->tracer->trace($name, function () use ($token$salesChannelId$options) {
  45.                 return $this->getDecorated()->create($token$salesChannelId$options);
  46.             });
  47.             $keys array_unique(array_merge(
  48.                 $this->tracer->get($name),
  49.                 [$nameself::ALL_TAG]
  50.             ));
  51.             $item->tag($keys);
  52.             return CacheValueCompressor::compress($context);
  53.         });
  54.         $context CacheValueCompressor::uncompress($value);
  55.         $context->assign(['token' => $token]);
  56.         return $context;
  57.     }
  58.     public static function buildName(string $salesChannelId): string
  59.     {
  60.         return 'context-factory-' $salesChannelId;
  61.     }
  62.     /**
  63.      * @param array<string, mixed> $options
  64.      */
  65.     private function isCacheable(array $options): bool
  66.     {
  67.         return !isset($options[SalesChannelContextService::CUSTOMER_ID])
  68.             && !isset($options[SalesChannelContextService::BILLING_ADDRESS_ID])
  69.             && !isset($options[SalesChannelContextService::SHIPPING_ADDRESS_ID]);
  70.     }
  71. }