vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/EventListener/SessionCartSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ShopBundle\EventListener;
  12. use Sylius\Component\Core\Model\OrderInterface;
  13. use Sylius\Component\Core\Storage\CartStorageInterface;
  14. use Sylius\Component\Order\Context\CartContextInterface;
  15. use Sylius\Component\Order\Context\CartNotFoundException;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Webmozart\Assert\Assert;
  20. final class SessionCartSubscriber implements EventSubscriberInterface
  21. {
  22.     /** @var CartContextInterface */
  23.     private $cartContext;
  24.     /** @var CartStorageInterface */
  25.     private $cartStorage;
  26.     public function __construct(CartContextInterface $cartContextCartStorageInterface $cartStorage)
  27.     {
  28.         $this->cartContext $cartContext;
  29.         $this->cartStorage $cartStorage;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             KernelEvents::RESPONSE => ['onKernelResponse'],
  35.         ];
  36.     }
  37.     public function onKernelResponse(ResponseEvent $event): void
  38.     {
  39.         if (!$event->isMasterRequest()) {
  40.             return;
  41.         }
  42.         $request $event->getRequest();
  43.         if (!$request->hasSession() || !$request->getSession()->isStarted()) {
  44.             return;
  45.         }
  46.         try {
  47.             $cart $this->cartContext->getCart();
  48.             /** @var OrderInterface $cart */
  49.             Assert::isInstanceOf($cartOrderInterface::class);
  50.         } catch (CartNotFoundException $exception) {
  51.             return;
  52.         }
  53.         if (null !== $cart->getId() && null !== $cart->getChannel()) {
  54.             $this->cartStorage->setForChannel($cart->getChannel(), $cart);
  55.         }
  56.     }
  57. }