vendor/sylius/sylius/src/Sylius/Bundle/ShopBundle/EventListener/UserImpersonatedListener.php line 43

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