vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/EventListener/ShopCartBlamerListener.php line 56

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\Core\Model\ShopUserInterface;
  17. use Sylius\Component\Order\Context\CartContextInterface;
  18. use Sylius\Component\Order\Context\CartNotFoundException;
  19. use Sylius\Component\Resource\Exception\UnexpectedTypeException;
  20. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  21. final class ShopCartBlamerListener
  22. {
  23.     /** @var CartContextInterface */
  24.     private $cartContext;
  25.     /** @var SectionProviderInterface */
  26.     private $uriBasedSectionContext;
  27.     public function __construct(
  28.         CartContextInterface $cartContext,
  29.         SectionProviderInterface $uriBasedSectionContext
  30.     ) {
  31.         $this->cartContext $cartContext;
  32.         $this->uriBasedSectionContext $uriBasedSectionContext;
  33.     }
  34.     public function onImplicitLogin(UserEvent $userEvent): void
  35.     {
  36.         if (!$this->uriBasedSectionContext->getSection() instanceof ShopSection) {
  37.             return;
  38.         }
  39.         $user $userEvent->getUser();
  40.         if (!$user instanceof ShopUserInterface) {
  41.             return;
  42.         }
  43.         $this->blame($user);
  44.     }
  45.     public function onInteractiveLogin(InteractiveLoginEvent $interactiveLoginEvent): void
  46.     {
  47.         $section $this->uriBasedSectionContext->getSection();
  48.         if (!$section instanceof ShopSection) {
  49.             return;
  50.         }
  51.         $user $interactiveLoginEvent->getAuthenticationToken()->getUser();
  52.         if (!$user instanceof ShopUserInterface) {
  53.             return;
  54.         }
  55.         $this->blame($user);
  56.     }
  57.     private function blame(ShopUserInterface $user): void
  58.     {
  59.         $cart $this->getCart();
  60.         if (null === $cart || null !== $cart->getCustomer()) {
  61.             return;
  62.         }
  63.         $cart->setCustomer($user->getCustomer());
  64.         $cart->setCreatedByGuest(false);
  65.     }
  66.     /**
  67.      * @throws UnexpectedTypeException
  68.      */
  69.     private function getCart(): ?OrderInterface
  70.     {
  71.         try {
  72.             $cart $this->cartContext->getCart();
  73.         } catch (CartNotFoundException $exception) {
  74.             return null;
  75.         }
  76.         if (!$cart instanceof OrderInterface) {
  77.             throw new UnexpectedTypeException($cartOrderInterface::class);
  78.         }
  79.         return $cart;
  80.     }
  81. }