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

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