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

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. /**
  13.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  14.  */
  15. class CacheStateSubscriber implements EventSubscriberInterface
  16. {
  17.     public const STATE_LOGGED_IN 'logged-in';
  18.     public const STATE_CART_FILLED 'cart-filled';
  19.     private CartService $cartService;
  20.     /**
  21.      * @internal
  22.      */
  23.     public function __construct(CartService $cartService)
  24.     {
  25.         $this->cartService $cartService;
  26.     }
  27.     /**
  28.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  29.      */
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             KernelEvents::CONTROLLER => [
  34.                 ['setStates'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  35.             ],
  36.             CustomerLoginEvent::class => 'login',
  37.             CartChangedEvent::class => 'cartChanged',
  38.         ];
  39.     }
  40.     public function login(CustomerLoginEvent $event): void
  41.     {
  42.         $event->getSalesChannelContext()->addState(self::STATE_LOGGED_IN);
  43.     }
  44.     public function cartChanged(CartChangedEvent $event): void
  45.     {
  46.         $event->getContext()->removeState(self::STATE_CART_FILLED);
  47.         if ($event->getCart()->getLineItems()->count() > 0) {
  48.             $event->getContext()->addState(self::STATE_CART_FILLED);
  49.         }
  50.     }
  51.     public function setStates(ControllerEvent $event): void
  52.     {
  53.         $request $event->getRequest();
  54.         if (!$request->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT)) {
  55.             return;
  56.         }
  57.         /** @var SalesChannelContext $context */
  58.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  59.         $cart $this->cartService->getCart($context->getToken(), $context);
  60.         $context->removeState(self::STATE_LOGGED_IN);
  61.         $context->removeState(self::STATE_CART_FILLED);
  62.         if ($cart->getLineItems()->count() > 0) {
  63.             $context->addState(self::STATE_CART_FILLED);
  64.         }
  65.         if ($context->getCustomer() !== null) {
  66.             $context->addState(self::STATE_LOGGED_IN);
  67.         }
  68.     }
  69. }