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

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.     public function __construct(
  20.         private CartStorageInterface $cartStorage,
  21.         private ChannelContextInterface $channelContext,
  22.         private OrderRepositoryInterface $orderRepository,
  23.     ) {
  24.     }
  25.     public function onUserImpersonated(UserEvent $event): void
  26.     {
  27.         $user $event->getUser();
  28.         if (!$user instanceof ShopUserInterface) {
  29.             return;
  30.         }
  31.         $customer $user->getCustomer();
  32.         $channel $this->channelContext->getChannel();
  33.         $cart $this->orderRepository->findLatestCartByChannelAndCustomer($channel$customer);
  34.         if ($cart === null) {
  35.             $this->cartStorage->removeForChannel($channel);
  36.             return;
  37.         }
  38.         $this->cartStorage->setForChannel($channel$cart);
  39.     }
  40. }