vendor/dvdw/platform-choice/src/Framework/Cache/HttpCacheKeySubscriber.php line 32

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dvdw\PlatformChoice\Framework\Cache;
  3. use Dvdw\PlatformChoice\Framework\Routing\RequestValidator;
  4. use Dvdw\PlatformChoice\System\SalesChannel\Struct\PlatformChoiceExtension;
  5. use Shopware\Core\PlatformRequest;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Shopware\Storefront\Framework\Cache\Event\HttpCacheGenerateKeyEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class HttpCacheKeySubscriber implements EventSubscriberInterface
  10. {
  11.     private RequestValidator $requestValidator;
  12.     private CrawlerDetector $crawlerDetector;
  13.     public function __construct(
  14.         RequestValidator $requestValidator,
  15.         CrawlerDetector  $crawlerDetector
  16.     )
  17.     {
  18.         $this->requestValidator $requestValidator;
  19.         $this->crawlerDetector $crawlerDetector;
  20.     }
  21.     
  22.     public static function getSubscribedEvents(): iterable
  23.     {
  24.         return [HttpCacheGenerateKeyEvent::class => 'extendKey'];
  25.     }
  26.     public function extendKey(HttpCacheGenerateKeyEvent $event): void
  27.     {
  28.         $request $event->getRequest();
  29.         if (!$this->requestValidator->isNormalStorefrontRequest($request)) {
  30.             return;
  31.         }
  32.         $hash $event->getHash();
  33.         $suffix PlatformChoiceExtension::B2C;
  34.         if ($request->cookies->has(PlatformChoiceExtension::KEY)) {
  35.             $suffix $request->cookies->get(PlatformChoiceExtension::KEY);
  36.         }
  37.         /**@var SalesChannelContext|null $context */
  38.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  39.         if ($context !== null) {
  40.             /** @var PlatformChoiceExtension|null $extension */
  41.             $extension $context->getExtension(PlatformChoiceExtension::KEY);
  42.             if ($extension !== null) {
  43.                 $suffix $extension->getChoice();
  44.             }
  45.         }
  46.         if ($this->crawlerDetector->isCrawler($request)) {
  47.             $suffix Crawler::IS_CRAWLER;
  48.         }
  49.         $event->setHash(hash('sha256'$hash '-' $suffix));
  50.     }
  51. }