custom/plugins/AcrisDiscountGroupCS/src/Storefront/Subscriber/ResponseCacheSubscriber.php line 56

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