vendor/boldr/multishop-bundle/src/EventSubscriber/EventSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. namespace Boldr\Shop\MultishopBundle\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface ;
  4. use Boldr\Shop\ShopBundle\ShopContext;
  5. use Boldr\Shop\ShopBundle\Event\{ OrderUpdatedEventOrderStateUpdatedEvent };
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Boldr\Shop\ShopBundle\OrderFlow\OrderFlowManagerInterface;
  8. use Boldr\Shop\MultishopBundle\Entity\OrderShop;
  9. use Boldr\Shop\ShopBundle\Entity\Order;
  10. use Boldr\Shop\MultishopBundle\DefaultShop\DefaultShopProviderInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. class EventSubscriber implements EventSubscriberInterface
  13. {
  14.     private OrderFlowManagerInterface $orderFlowManager;
  15.     private EntityManagerInterface $em;
  16.     private DefaultShopProviderInterface $defaultShopProvider;
  17.     private RequestStack $requestStack;
  18.     public function __construct(RequestStack $requestStackOrderFlowManagerInterface $orderFlowManagerEntityManagerInterface $emDefaultShopProviderInterface $defaultShopProvider)
  19.     {
  20.         $this->requestStack $requestStack;
  21.         $this->orderFlowManager $orderFlowManager;
  22.         $this->em $em;
  23.         $this->defaultShopProvider $defaultShopProvider;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             OrderStateUpdatedEvent::class => ['onOrderStateUpdated'],
  29.             OrderUpdatedEvent::class => ['onOrderUpdated']
  30.         ];
  31.     }
  32.     public function onOrderUpdated(OrderUpdatedEvent $event)
  33.     {
  34.         $this->addDefaultShopToOrder($event->getOrder());
  35.     }
  36.     /**
  37.      * Add default shop to orders without shop
  38.      */
  39.     public function onOrderStateUpdated(OrderStateUpdatedEvent $event)
  40.     {
  41.         $this->addDefaultShopToOrder($event->getOrder());
  42.     }
  43.     private function addDefaultShopToOrder(Order $order)
  44.     {
  45.         $orderShop $order->getId() === null null $this->em->getRepository(OrderShop::class)->find($order);
  46.         if ($orderShop !== null)
  47.         {
  48.             return;
  49.         }
  50.         if (count($order->getItems()))
  51.         {
  52.             $request $this->requestStack->getCurrentRequest();
  53.             $defaultShop $this->defaultShopProvider->getDefaultShop($request$order->getCustomer());
  54.             $orderShop = new OrderShop($order$defaultShop);
  55.             $this->em->persist($orderShop);
  56.             $this->em->flush();
  57.         }
  58.     }
  59. }