custom/plugins/AcrisCookieConsentCS/src/Subscriber/ResponseCacheSubscriber.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CookieConsent\Subscriber;
  3. use Shopware\Core\Framework\Struct\ArrayStruct;
  4. use Shopware\Core\PlatformRequest;
  5. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  6. use Shopware\Storefront\Framework\Cache\CacheResponseSubscriber;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Cookie;
  9. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class ResponseCacheSubscriber implements EventSubscriberInterface
  12. {
  13.     public const CACHE_HASH_EXTENSION 'acris_cache_hash';
  14.     public static function getSubscribedEvents()
  15.     {
  16.         return [
  17.             KernelEvents::RESPONSE => [
  18.                 ['setResponseCache', -2000]
  19.             ]
  20.         ];
  21.     }
  22.     public function addToCacheHash($valueSalesChannelContext $context)
  23.     {
  24.         if($context->hasExtension(self::CACHE_HASH_EXTENSION) === true) {
  25.             /** @var ArrayStruct $cacheHashExtension */
  26.             $cacheHashExtension $context->getExtension(self::CACHE_HASH_EXTENSION);
  27.         } else {
  28.             $cacheHashExtension = new ArrayStruct();
  29.         }
  30.         $context->addExtension(self::CACHE_HASH_EXTENSION$this->getCacheHashExtension($value$cacheHashExtension));
  31.     }
  32.     public function setResponseCache(ResponseEvent $event)
  33.     {
  34.         $response $event->getResponse();
  35.         $context $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  36.         if (!$context instanceof SalesChannelContext) {
  37.             return;
  38.         }
  39.         if($context->hasExtension(self::CACHE_HASH_EXTENSION) !== true) {
  40.             return;
  41.         }
  42.         /** @var ArrayStruct $acrisCacheHashExtension */
  43.         $acrisCacheHashExtension $context->getExtension(self::CACHE_HASH_EXTENSION);
  44.         $acrisCacheHash $this->generateCacheHashFromExtension($acrisCacheHashExtension);
  45.         if ($context->getCustomer()) {
  46.             $cacheHash $this->buildCacheHash($context$acrisCacheHash);
  47.         } else {
  48.             $cacheHash $acrisCacheHash;
  49.         }
  50.         $response->headers->setCookie(new Cookie(CacheResponseSubscriber::CONTEXT_CACHE_COOKIE$cacheHash));
  51.     }
  52.     public function generateCacheHash($value)
  53.     {
  54.         return $this->generateCacheHashFromExtension($this->getCacheHashExtension($value));
  55.     }
  56.     private function getCacheHashExtension($value, ?ArrayStruct $cacheHashExtension null): ArrayStruct
  57.     {
  58.         if($cacheHashExtension === null) {
  59.             $cacheHashExtension = new ArrayStruct();
  60.         }
  61.         $encodedValue md5(json_encode($value));
  62.         $cacheHashExtension->set($encodedValue$encodedValue);
  63.         return $cacheHashExtension;
  64.     }
  65.     private function generateCacheHashFromExtension(ArrayStruct $cacheHashExtension): string
  66.     {
  67.         return md5(json_encode($cacheHashExtension->all()));
  68.     }
  69.     private function buildCacheHash(SalesChannelContext $context$acrisCacheHash): string
  70.     {
  71.         return md5(json_encode([
  72.             $context->getRuleIds(),
  73.             $context->getContext()->getVersionId(),
  74.             $context->getCurrency()->getId(),
  75.             $acrisCacheHash
  76.         ]));
  77.     }
  78. }