src/Core/Checkout/Shipping/SalesChannel/CachedShippingMethodRoute.php line 59

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Shipping\SalesChannel;
  3. use Shopware\Core\Checkout\Shipping\Event\ShippingMethodRouteCacheKeyEvent;
  4. use Shopware\Core\Checkout\Shipping\Event\ShippingMethodRouteCacheTagsEvent;
  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 Shopware\Core\System\SalesChannel\StoreApiResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Contracts\Cache\CacheInterface;
  17. use Symfony\Contracts\Cache\ItemInterface;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. #[Route(defaults: ['_routeScope' => ['store-api']])]
  20. #[Package('checkout')]
  21. class CachedShippingMethodRoute extends AbstractShippingMethodRoute
  22. {
  23.     final public const ALL_TAG 'shipping-method-route';
  24.     /**
  25.      * @internal
  26.      *
  27.      * @param AbstractCacheTracer<ShippingMethodRouteResponse> $tracer
  28.      * @param array<string> $states
  29.      */
  30.     public function __construct(private readonly AbstractShippingMethodRoute $decorated, private readonly CacheInterface $cache, private readonly EntityCacheKeyGenerator $generator, private readonly AbstractCacheTracer $tracer, private readonly EventDispatcherInterface $dispatcher, private readonly array $states)
  31.     {
  32.     }
  33.     public function getDecorated(): AbstractShippingMethodRoute
  34.     {
  35.         return $this->decorated;
  36.     }
  37.     #[Route(path'/store-api/shipping-method'name'store-api.shipping.method'methods: ['GET''POST'], defaults: ['_entity' => 'shipping_method'])]
  38.     public function load(Request $requestSalesChannelContext $contextCriteria $criteria): ShippingMethodRouteResponse
  39.     {
  40.         if ($context->hasState(...$this->states)) {
  41.             return $this->getDecorated()->load($request$context$criteria);
  42.         }
  43.         $key $this->generateKey($request$context$criteria);
  44.         if ($key === null) {
  45.             return $this->getDecorated()->load($request$context$criteria);
  46.         }
  47.         $value $this->cache->get($key, function (ItemInterface $item) use ($request$context$criteria) {
  48.             $name self::buildName($context->getSalesChannelId());
  49.             $response $this->tracer->trace($name, fn () => $this->getDecorated()->load($request$context$criteria));
  50.             $item->tag($this->generateTags($request$response$context$criteria));
  51.             return CacheValueCompressor::compress($response);
  52.         });
  53.         return CacheValueCompressor::uncompress($value);
  54.     }
  55.     public static function buildName(string $salesChannelId): string
  56.     {
  57.         return 'shipping-method-route-' $salesChannelId;
  58.     }
  59.     private function generateKey(Request $requestSalesChannelContext $contextCriteria $criteria): ?string
  60.     {
  61.         $parts = [
  62.             $this->generator->getCriteriaHash($criteria),
  63.             $this->generator->getSalesChannelContextHash($context, [RuleAreas::SHIPPING_AREA]),
  64.             $request->query->getBoolean('onlyAvailable'false),
  65.         ];
  66.         $event = new ShippingMethodRouteCacheKeyEvent($parts$request$context$criteria);
  67.         $this->dispatcher->dispatch($event);
  68.         if (!$event->shouldCache()) {
  69.             return null;
  70.         }
  71.         return self::buildName($context->getSalesChannelId()) . '-' md5(Json::encode($event->getParts()));
  72.     }
  73.     /**
  74.      * @return array<string>
  75.      */
  76.     private function generateTags(Request $requestStoreApiResponse $responseSalesChannelContext $contextCriteria $criteria): array
  77.     {
  78.         $tags array_merge(
  79.             $this->tracer->get(self::buildName($context->getSalesChannelId())),
  80.             [self::buildName($context->getSalesChannelId()), self::ALL_TAG]
  81.         );
  82.         $event = new ShippingMethodRouteCacheTagsEvent($tags$request$response$context$criteria);
  83.         $this->dispatcher->dispatch($event);
  84.         return array_unique(array_filter($event->getTags()));
  85.     }
  86. }