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

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