custom/plugins/AcrisCountryTaxCS/src/Subscriber/ResponseCacheSubscriber.php line 54

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CountryTax\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.     private bool $httpCacheEnabled;
  18.     private MaintenanceModeResolver $maintenanceModeResolver;
  19.     public function __construct(
  20.         bool $httpCacheEnabled,
  21.         MaintenanceModeResolver $maintenanceModeResolver)
  22.     {
  23.         $this->httpCacheEnabled $httpCacheEnabled;
  24.         $this->maintenanceModeResolver $maintenanceModeResolver;
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             KernelEvents::RESPONSE => [
  30.                 ['setResponseCache', -2000]
  31.             ]
  32.         ];
  33.     }
  34.     public function addToCacheHash($valueSalesChannelContext $context)
  35.     {
  36.         if($context->hasExtension(self::CACHE_HASH_EXTENSION) === true) {
  37.             /** @var ArrayStruct $cacheHashExtension */
  38.             $cacheHashExtension $context->getExtension(self::CACHE_HASH_EXTENSION);
  39.         } else {
  40.             $cacheHashExtension = new ArrayStruct();
  41.         }
  42.         $context->addExtension(self::CACHE_HASH_EXTENSION$this->getCacheHashExtension($value$cacheHashExtension));
  43.     }
  44.     public function setResponseCache(ResponseEvent $event)
  45.     {
  46.         if (!$this->httpCacheEnabled) {
  47.             return;
  48.         }
  49.         $response $event->getResponse();
  50.         $request $event->getRequest();
  51.         if ($this->maintenanceModeResolver->isMaintenanceRequest($request)) {
  52.             return;
  53.         }
  54.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  55.         if (!$context instanceof SalesChannelContext) {
  56.             return;
  57.         }
  58.         if($context->hasExtension(self::CACHE_HASH_EXTENSION) !== true) {
  59.             return;
  60.         }
  61.         /** @var ArrayStruct $acrisCacheHashExtension */
  62.         $acrisCacheHashExtension $context->getExtension(self::CACHE_HASH_EXTENSION);
  63.         $acrisCacheHash $this->generateCacheHashFromExtension($acrisCacheHashExtension);
  64.         $cacheHash $this->buildCacheHash($context$acrisCacheHash$this->getCurrencyIdChanging($request));
  65.         $response->headers->setCookie(new Cookie(CacheResponseSubscriber::CONTEXT_CACHE_COOKIE$cacheHash));
  66.     }
  67.     public function generateCacheHash($value)
  68.     {
  69.         return $this->generateCacheHashFromExtension($this->getCacheHashExtension($value));
  70.     }
  71.     private function getCacheHashExtension($value, ?ArrayStruct $cacheHashExtension null): ArrayStruct
  72.     {
  73.         if($cacheHashExtension === null) {
  74.             $cacheHashExtension = new ArrayStruct();
  75.         }
  76.         $encodedValue md5(json_encode($value));
  77.         $cacheHashExtension->set($encodedValue$encodedValue);
  78.         return $cacheHashExtension;
  79.     }
  80.     private function generateCacheHashFromExtension(ArrayStruct $cacheHashExtension): string
  81.     {
  82.         return md5(json_encode($cacheHashExtension->all()));
  83.     }
  84.     private function buildCacheHash(SalesChannelContext $context$acrisCacheHash, ?string $currencyId): string
  85.     {
  86.         if(empty($currencyId) == true) {
  87.             $currencyId $context->getCurrency()->getId();
  88.         }
  89.         return md5(json_encode([
  90.             $context->getRuleIds(),
  91.             $context->getContext()->getVersionId(),
  92.             $currencyId,
  93.             $context->getCustomer() ? 'logged-in' 'not-logged-in',
  94.             $acrisCacheHash
  95.         ]));
  96.     }
  97.     private function getCurrencyIdChanging(Request $request): ?string
  98.     {
  99.         $route $request->attributes->get('_route');
  100.         if ($route === 'frontend.checkout.configure') {
  101.             $currencyId $request->get(SalesChannelContextService::CURRENCY_ID);
  102.             if (!$currencyId) {
  103.                 return null;
  104.             }
  105.             return $currencyId;
  106.         }
  107.         return null;
  108.     }
  109. }