custom/plugins/MadMykey/src/Storefront/Framework/Cache/CacheResponseSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Madco\Mykey\Storefront\Framework\Cache;
  4. use Bodymed\Webshop\Core\Content\BodymedCenter\BodymedCenterEntity;
  5. use Madco\Mykey\Struct\CurrentMykeyPartner;
  6. use Shopware\Core\PlatformRequest;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Cookie;
  10. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Shopware\Storefront\Framework\Cache\CacheResponseSubscriber as CoreCacheResponseSubscriber;
  13. use Madco\Mykey\Constants;
  14. /**
  15.  * Erweitert den HTTP-Cache um Mykey-Funktionalität,
  16.  * damit gecachte Seiten den Mykey berücksichtigen.
  17.  */
  18. class CacheResponseSubscriber implements EventSubscriberInterface
  19. {
  20.     private bool $httpCacheEnabled;
  21.     public function __construct($httpCacheEnabled)
  22.     {
  23.         $this->httpCacheEnabled = (bool) $httpCacheEnabled;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             KernelEvents::RESPONSE => [
  29.                 ['setResponseCache', -2500],
  30.             ],
  31.         ];
  32.     }
  33.     public function setResponseCache(ResponseEvent $event)
  34.     {
  35.         $request $event->getRequest();
  36.         $response $event->getResponse();
  37.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  38.         if (!$this->httpCacheEnabled) {
  39.             return;
  40.         }
  41.         $cookies $response->headers->getCookies();
  42.         if ($salesChannelContext instanceof SalesChannelContext) {
  43.             foreach ($cookies as $cookie) {
  44.                 if ($cookie->getName() === CoreCacheResponseSubscriber::CONTEXT_CACHE_COOKIE) {
  45.                     $cookie Cookie::create(CoreCacheResponseSubscriber::CONTEXT_CACHE_COOKIE$this->buildCacheHash($salesChannelContext));
  46.                     $cookie->setSecureDefault($request->isSecure());
  47.                     $response->headers->setCookie($cookie);
  48.                 }
  49.             }
  50.             $mykeyPartnerStruct $salesChannelContext->getExtension(Constants::MYKEY_PARTNER_CONTEXT_EXTENSION_KEY);
  51.             if ($mykeyPartnerStruct instanceof CurrentMykeyPartner) {
  52.                 if ($mykeyPartnerStruct->getBodymedCenter() instanceof BodymedCenterEntity) {
  53.                     $mykeyCookie Cookie::create(Constants::MYKEY_PARTNER_SESSION_ATTRIBUTE\md5($mykeyPartnerStruct->getCode()));
  54.                     $response->headers->setCookie($mykeyCookie);
  55.                 }
  56.             }
  57.         }
  58.     }
  59.     /**
  60.      * Erweiterung des Http-Cache-hash um den Mykey, sodass für jeden Kunden mit einem Mykey
  61.      * ein individueller Cache generiert wird
  62.      */
  63.     private function buildCacheHash(SalesChannelContext $salesChannelContext): string
  64.     {
  65.         $hashData = [
  66.             $salesChannelContext->getRuleIds(),
  67.             $salesChannelContext->getContext()->getVersionId(),
  68.             $salesChannelContext->getCurrency()->getId(),
  69.         ];
  70.         $mykeyPartnerStruct $salesChannelContext->getExtension(Constants::MYKEY_PARTNER_CONTEXT_EXTENSION_KEY);
  71.         if ($mykeyPartnerStruct instanceof CurrentMykeyPartner) {
  72.             if ($mykeyPartnerStruct->getBodymedCenter() instanceof BodymedCenterEntity) {
  73.                 if ($mykeyPartnerStruct->getBodymedCenter()->getTags() &&
  74.                     $mykeyPartnerStruct->getBodymedCenter()->getTags()->count() > 0
  75.                 ) {
  76.                     $hashData[] = $mykeyPartnerStruct->getBodymedCenter()->getTags()->getIds();
  77.                 } else {
  78.                     $hashData[] = $mykeyPartnerStruct->getBodymedCenter()->getCode();
  79.                 }
  80.             }
  81.         }
  82.         return \md5(\json_encode($hashData));
  83.     }
  84. }