vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/EventListener/UserCartRecalculationListener.php line 50

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\Bundle\CoreBundle\SectionResolver\SectionProviderInterface;
  13. use Sylius\Bundle\ShopBundle\SectionResolver\ShopSection;
  14. use Sylius\Bundle\UserBundle\Event\UserEvent;
  15. use Sylius\Component\Core\Model\OrderInterface;
  16. use Sylius\Component\Order\Context\CartContextInterface;
  17. use Sylius\Component\Order\Context\CartNotFoundException;
  18. use Sylius\Component\Order\Processor\OrderProcessorInterface;
  19. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  20. use Webmozart\Assert\Assert;
  21. final class UserCartRecalculationListener
  22. {
  23.     /** @var CartContextInterface */
  24.     private $cartContext;
  25.     /** @var OrderProcessorInterface */
  26.     private $orderProcessor;
  27.     /** @var SectionProviderInterface */
  28.     private $uriBasedSectionContext;
  29.     public function __construct(
  30.         CartContextInterface $cartContext,
  31.         OrderProcessorInterface $orderProcessor,
  32.         SectionProviderInterface $uriBasedSectionContext
  33.     ) {
  34.         $this->cartContext $cartContext;
  35.         $this->orderProcessor $orderProcessor;
  36.         $this->uriBasedSectionContext $uriBasedSectionContext;
  37.     }
  38.     /**
  39.      * @param InteractiveLoginEvent|UserEvent $event
  40.      */
  41.     public function recalculateCartWhileLogin(object $event): void
  42.     {
  43.         if (!$this->uriBasedSectionContext->getSection() instanceof ShopSection) {
  44.             return;
  45.         }
  46.         /** @psalm-suppress DocblockTypeContradiction */
  47.         if (!$event instanceof InteractiveLoginEvent && !$event instanceof UserEvent) {
  48.             throw new \TypeError(sprintf(
  49.                 '$event needs to be an instance of "%s" or "%s"',
  50.                 InteractiveLoginEvent::class,
  51.                 UserEvent::class
  52.             ));
  53.         }
  54.         try {
  55.             $cart $this->cartContext->getCart();
  56.         } catch (CartNotFoundException $exception) {
  57.             return;
  58.         }
  59.         Assert::isInstanceOf($cartOrderInterface::class);
  60.         $this->orderProcessor->process($cart);
  61.     }
  62. }