vendor/lnb/shopware6-lnb-partner-auth/src/Subscriber/HttpCacheHitSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace Lnb\Shopware6\LnbPartnerAuth\Subscriber;
  3. use Lnb\Shopware6\LnbPartnerAuth\Config\ConfigServiceAdapter;
  4. use Shopware\Core\Framework\Adapter\Cache\CacheStateSubscriber;
  5. use Shopware\Storefront\Framework\Cache\CacheResponseSubscriber;
  6. use Shopware\Storefront\Framework\Cache\Event\HttpCacheHitEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. class HttpCacheHitSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private readonly ConfigServiceAdapter $configServiceAdapter
  13.     ) {
  14.     }
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             HttpCacheHitEvent::class => 'onHttpCacheHit',
  19.         ];
  20.     }
  21.     public function onHttpCacheHit(HttpCacheHitEvent $event): void
  22.     {
  23.         $salesChannelId $event->getRequest()->attributes->get('sw-sales-channel-id');
  24.         $this->configServiceAdapter->setSalesChannelId($salesChannelId);
  25.         $partnerSalesChannelId $this->configServiceAdapter->getPartnerSalesChannelId();
  26.         if ($partnerSalesChannelId !== $salesChannelId || $this->isCustomerLoggedIn($this->getSystemStates($event->getRequest()))) {
  27.             return;
  28.         }
  29.         //disable http cache for PartnerSalesChannel when customer is not logged in to always redirect partner pages to login page
  30.         $event->getResponse()->headers->set('Cache-Control''no-cache, no-store, must-revalidate');
  31.     }
  32.     /**
  33.      * @return array<string, int>
  34.      */
  35.     private function getSystemStates(Request $request): array
  36.     {
  37.         $states = [];
  38.         $swStates = (string) $request->cookies->get(CacheResponseSubscriber::SYSTEM_STATE_COOKIE);
  39.         if ($swStates !== '') {
  40.             $states array_flip(explode(','$swStates));
  41.         }
  42.         return $states;
  43.     }
  44.     /**
  45.      * @param array<string, int> $systemStates
  46.      */
  47.     private function isCustomerLoggedIn(array $systemStates): bool
  48.     {
  49.         return array_key_exists(CacheStateSubscriber::STATE_LOGGED_IN$systemStates);
  50.     }
  51. }