src/Sylius/Bundle/ShopBundle/EventListener/UserImpersonatedListener.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\Bundle\UserBundle\Event\UserEvent;
  13. use Sylius\Component\Channel\Context\ChannelContextInterface;
  14. use Sylius\Component\Core\Model\ChannelInterface;
  15. use Sylius\Component\Core\Model\OrderInterface;
  16. use Sylius\Component\Core\Model\ShopUserInterface;
  17. use Sylius\Component\Core\Repository\OrderRepositoryInterface;
  18. use Sylius\Component\Core\Storage\CartStorageInterface;
  19. use Webmozart\Assert\Assert;
  20. final class UserImpersonatedListener
  21. {
  22.     /** @var CartStorageInterface */
  23.     private $cartStorage;
  24.     /** @var ChannelContextInterface */
  25.     private $channelContext;
  26.     /** @var OrderRepositoryInterface */
  27.     private $orderRepository;
  28.     public function __construct(
  29.         CartStorageInterface $cartStorage,
  30.         ChannelContextInterface $channelContext,
  31.         OrderRepositoryInterface $orderRepository
  32.     ) {
  33.         $this->cartStorage $cartStorage;
  34.         $this->channelContext $channelContext;
  35.         $this->orderRepository $orderRepository;
  36.     }
  37.     public function onUserImpersonated(UserEvent $event): void
  38.     {
  39.         /** @var ShopUserInterface $user */
  40.         $user $event->getUser();
  41.         Assert::isInstanceOf($userShopUserInterface::class);
  42.         $customer $user->getCustomer();
  43.         /** @var ChannelInterface $channel */
  44.         $channel $this->channelContext->getChannel();
  45.         /** @var OrderInterface $cart */
  46.         $cart $this->orderRepository->findLatestCartByChannelAndCustomer($channel$customer);
  47.         if ($cart === null) {
  48.             $this->cartStorage->removeForChannel($channel);
  49.             return;
  50.         }
  51.         $this->cartStorage->setForChannel($channel$cart);
  52.     }
  53. }