vendor/shopware/core/Framework/Adapter/Cache/CacheStateSubscriber.php line 50

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Cache;
  3. use Shopware\Core\Checkout\Cart\Event\CartChangedEvent;
  4. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  5. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  6. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  7. use Shopware\Core\PlatformRequest;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. class CacheStateSubscriber implements EventSubscriberInterface
  13. {
  14.     public const STATE_LOGGED_IN 'logged-in';
  15.     public const STATE_CART_FILLED 'cart-filled';
  16.     private CartService $cartService;
  17.     /**
  18.      * @internal
  19.      */
  20.     public function __construct(CartService $cartService)
  21.     {
  22.         $this->cartService $cartService;
  23.     }
  24.     /**
  25.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  26.      */
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             KernelEvents::CONTROLLER => [
  31.                 ['setStates'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  32.             ],
  33.             CustomerLoginEvent::class => 'login',
  34.             CartChangedEvent::class => 'cartChanged',
  35.         ];
  36.     }
  37.     public function login(CustomerLoginEvent $event): void
  38.     {
  39.         $event->getSalesChannelContext()->addState(self::STATE_LOGGED_IN);
  40.     }
  41.     public function cartChanged(CartChangedEvent $event): void
  42.     {
  43.         $event->getContext()->removeState(self::STATE_CART_FILLED);
  44.         if ($event->getCart()->getLineItems()->count() > 0) {
  45.             $event->getContext()->addState(self::STATE_CART_FILLED);
  46.         }
  47.     }
  48.     public function setStates(ControllerEvent $event): void
  49.     {
  50.         $request $event->getRequest();
  51.         if (!$request->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT)) {
  52.             return;
  53.         }
  54.         /** @var SalesChannelContext $context */
  55.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  56.         $cart $this->cartService->getCart($context->getToken(), $context);
  57.         $context->removeState(self::STATE_LOGGED_IN);
  58.         $context->removeState(self::STATE_CART_FILLED);
  59.         if ($cart->getLineItems()->count() > 0) {
  60.             $context->addState(self::STATE_CART_FILLED);
  61.         }
  62.         if ($context->getCustomer() !== null) {
  63.             $context->addState(self::STATE_LOGGED_IN);
  64.         }
  65.     }
  66. }