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

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