vendor/shopware/platform/src/Core/Framework/Adapter/Cache/CacheStateSubscriber.php line 56

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