custom/plugins/NetiNextAccessManager/src/Subscriber/Cache.php line 54

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextAccessManager\Subscriber;
  4. use NetInventors\NetiNextAccessManager\Constants\ConfigConstants;
  5. use NetInventors\NetiNextAccessManager\Service\PluginConfig;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Content\Category\Event\CategoryRouteCacheKeyEvent;
  8. use Shopware\Core\Content\LandingPage\Event\LandingPageRouteCacheKeyEvent;
  9. use Shopware\Core\Content\Product\Events\CrossSellingRouteCacheKeyEvent;
  10. use Shopware\Core\Content\Product\Events\ProductDetailRouteCacheKeyEvent;
  11. use Shopware\Core\Content\Product\Events\ProductListingRouteCacheKeyEvent;
  12. use Shopware\Core\Content\Product\Events\ProductSearchRouteCacheKeyEvent;
  13. use Shopware\Core\Content\Product\Events\ProductSuggestRouteCacheKeyEvent;
  14. use Shopware\Core\Framework\Adapter\Cache\StoreApiRouteCacheKeyEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  17. class Cache implements EventSubscriberInterface
  18. {
  19.     private PluginConfig $pluginConfig;
  20.     public function __construct(PluginConfig $pluginConfig)
  21.     {
  22.         $this->pluginConfig $pluginConfig;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             ProductDetailRouteCacheKeyEvent::class  => [ [ 'onCacheKeyEvent' ], [ 'storeRoute' ] ],
  28.             ProductListingRouteCacheKeyEvent::class => [ [ 'onCacheKeyEvent' ], [ 'storeRoute' ] ],
  29.             ProductSearchRouteCacheKeyEvent::class  => [ [ 'onCacheKeyEvent' ], [ 'storeRoute' ] ],
  30.             ProductSuggestRouteCacheKeyEvent::class => [ [ 'onCacheKeyEvent' ], [ 'storeRoute' ] ],
  31.             CategoryRouteCacheKeyEvent::class       => [ [ 'onCacheKeyEvent' ], [ 'storeRoute' ] ],
  32.             CrossSellingRouteCacheKeyEvent::class   => [ [ 'onCacheKeyEvent' ], [ 'storeRoute' ] ],
  33.             LandingPageRouteCacheKeyEvent::class    => 'storeRoute',
  34.         ];
  35.     }
  36.     /**
  37.      * @throws \Exception
  38.      */
  39.     public function onCacheKeyEvent(StoreApiRouteCacheKeyEvent $event): void
  40.     {
  41.         $customer   $event->getContext()->getCustomer();
  42.         $loginState = (int) ($customer instanceof CustomerEntity);
  43.         $event->addPart('neti-access-manager-login-state-' $loginState);
  44.     }
  45.     public function storeRoute(StoreApiRouteCacheKeyEvent $event): void
  46.     {
  47.         $request $event->getRequest();
  48.         if (
  49.             !$this->pluginConfig->isActive()
  50.             || !is_string($request->attributes->get('sw-sales-channel-id'))
  51.             || $event->getContext()->getCustomer() instanceof CustomerEntity
  52.         ) {
  53.             return;
  54.         }
  55.         try {
  56.             $session $request->getSession();
  57.         } catch (SessionNotFoundException $ex) {
  58.             return;
  59.         }
  60.         if (
  61.             $this->pluginConfig->getLoginRedirectType() === ConfigConstants::CONFIG_REDIRECT_TYPE_REFERER
  62.             && $request->isXmlHttpRequest() === false
  63.         ) {
  64.             $referer = [
  65.                 'route'       => (string) $request->attributes->get('_route'),
  66.                 'routeParams' => (array) $request->attributes->get('_route_params'),
  67.             ];
  68.             if (
  69.                 !\in_array($referer['route'], [
  70.                     'frontend.account.login.page',
  71.                     'frontend.account.logout.page',
  72.                     'frontend.checkout.info',
  73.                     'api.customer.update'
  74.                 ])
  75.             ) {
  76.                 $session->set('neti-access-manager-referer'$referer);
  77.             }
  78.         }
  79.     }
  80. }