src/Core/Framework/Adapter/Cache/CacheStateSubscriber.php line 61

  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.  * @internal
  15.  */
  16. #[Package('core')]
  17. class CacheStateSubscriber implements EventSubscriberInterface
  18. {
  19.     final public const STATE_LOGGED_IN 'logged-in';
  20.     final public const STATE_CART_FILLED 'cart-filled';
  21.     /**
  22.      * @internal
  23.      */
  24.     public function __construct(private readonly CartService $cartService)
  25.     {
  26.     }
  27.     /**
  28.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  29.      */
  30.     public static function getSubscribedEvents(): array
  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. }