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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CookieConsent\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEntity;
  4. use Shopware\Core\Framework\Struct\ArrayStruct;
  5. use Shopware\Core\PlatformRequest;
  6. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Storefront\Framework\Cache\CacheResponseSubscriber;
  9. use Shopware\Storefront\Framework\Routing\MaintenanceModeResolver;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Cookie;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. class ResponseCacheSubscriber implements EventSubscriberInterface
  16. {
  17.     public const CACHE_HASH_EXTENSION 'acris_cache_hash';
  18.     public CONST CASH_HASH_COOKIE_LIFETIME 86400 30;
  19.     private bool $httpCacheEnabled;
  20.     private MaintenanceModeResolver $maintenanceModeResolver;
  21.     public function __construct(
  22.         bool $httpCacheEnabled,
  23.         MaintenanceModeResolver $maintenanceModeResolver)
  24.     {
  25.         $this->httpCacheEnabled $httpCacheEnabled;
  26.         $this->maintenanceModeResolver $maintenanceModeResolver;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             KernelEvents::RESPONSE => [
  32.                 ['setResponseCache', -2000]
  33.             ]
  34.         ];
  35.     }
  36.     public function addToCacheHash($valueSalesChannelContext $context)
  37.     {
  38.         if($context->hasExtension(self::CACHE_HASH_EXTENSION) === true) {
  39.             /** @var ArrayStruct $cacheHashExtension */
  40.             $cacheHashExtension $context->getExtension(self::CACHE_HASH_EXTENSION);
  41.         } else {
  42.             $cacheHashExtension = new ArrayStruct();
  43.         }
  44.         $context->addExtension(self::CACHE_HASH_EXTENSION$this->getCacheHashExtension($value$cacheHashExtension));
  45.     }
  46.     public function setResponseCache(ResponseEvent $event)
  47.     {
  48.         if (!$this->httpCacheEnabled) {
  49.             return;
  50.         }
  51.         $response $event->getResponse();
  52.         $request $event->getRequest();
  53.         if ($this->maintenanceModeResolver->isMaintenanceRequest($request)) {
  54.             return;
  55.         }
  56.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  57.         if (!$context instanceof SalesChannelContext) {
  58.             return;
  59.         }
  60.         if($context->hasExtension(self::CACHE_HASH_EXTENSION) !== true) {
  61.             return;
  62.         }
  63.         /** @var ArrayStruct $acrisCacheHashExtension */
  64.         $acrisCacheHashExtension $context->getExtension(self::CACHE_HASH_EXTENSION);
  65.         $acrisCacheHash $this->generateCacheHashFromExtension($acrisCacheHashExtension);
  66.         $cacheHash $this->buildCacheHash($context$acrisCacheHash$this->getCurrencyIdChanging($request));
  67.         $response->headers->setCookie(new Cookie(CacheResponseSubscriber::CONTEXT_CACHE_COOKIE$cacheHashtime() + self::CASH_HASH_COOKIE_LIFETIME));
  68.     }
  69.     public function generateCacheHash($value)
  70.     {
  71.         return $this->generateCacheHashFromExtension($this->getCacheHashExtension($value));
  72.     }
  73.     private function getCacheHashExtension($value, ?ArrayStruct $cacheHashExtension null): ArrayStruct
  74.     {
  75.         if($cacheHashExtension === null) {
  76.             $cacheHashExtension = new ArrayStruct();
  77.         }
  78.         $encodedValue md5(json_encode($value));
  79.         $cacheHashExtension->set($encodedValue$encodedValue);
  80.         return $cacheHashExtension;
  81.     }
  82.     private function generateCacheHashFromExtension(ArrayStruct $cacheHashExtension): string
  83.     {
  84.         return md5(json_encode($cacheHashExtension->all()));
  85.     }
  86.     private function buildCacheHash(SalesChannelContext $context$acrisCacheHash, ?string $currencyId): string
  87.     {
  88.         if(empty($currencyId) == true) {
  89.             $currencyId $context->getCurrency()->getId();
  90.         }
  91.         return md5(json_encode([
  92.             $context->getRuleIds(),
  93.             $context->getContext()->getVersionId(),
  94.             $currencyId,
  95.             $context->getCustomer() ? 'logged-in' 'not-logged-in',
  96.             $acrisCacheHash
  97.         ]));
  98.     }
  99.     private function getCurrencyIdChanging(Request $request): ?string
  100.     {
  101.         $route $request->attributes->get('_route');
  102.         if ($route === 'frontend.checkout.configure') {
  103.             $currencyId $request->get(SalesChannelContextService::CURRENCY_ID);
  104.             if (!$currencyId) {
  105.                 return null;
  106.             }
  107.             return $currencyId;
  108.         }
  109.         return null;
  110.     }
  111. }