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

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.     public function __construct(
  24.         private CartContextInterface $cartContext,
  25.         private OrderProcessorInterface $orderProcessor,
  26.         private SectionProviderInterface $uriBasedSectionContext,
  27.     ) {
  28.     }
  29.     /**
  30.      * @param InteractiveLoginEvent|UserEvent $event
  31.      */
  32.     public function recalculateCartWhileLogin(object $event): void
  33.     {
  34.         if (!$this->uriBasedSectionContext->getSection() instanceof ShopSection) {
  35.             return;
  36.         }
  37.         /** @psalm-suppress DocblockTypeContradiction */
  38.         if (!$event instanceof InteractiveLoginEvent && !$event instanceof UserEvent) {
  39.             throw new \TypeError(sprintf(
  40.                 '$event needs to be an instance of "%s" or "%s"',
  41.                 InteractiveLoginEvent::class,
  42.                 UserEvent::class,
  43.             ));
  44.         }
  45.         try {
  46.             $cart $this->cartContext->getCart();
  47.         } catch (CartNotFoundException) {
  48.             return;
  49.         }
  50.         Assert::isInstanceOf($cartOrderInterface::class);
  51.         $this->orderProcessor->process($cart);
  52.     }
  53. }