custom/plugins/SwagB2bPlatform/components/Order/BridgePlatform/OrderTransitionSubscriber.php line 48

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\Order\BridgePlatform;
  3. use Shopware\B2B\Common\IdValue;
  4. use Shopware\B2B\Common\Repository\NotFoundException;
  5. use Shopware\B2B\Order\Framework\OrderContext;
  6. use Shopware\B2B\Order\Framework\OrderContextRepository;
  7. use Shopware\Core\Checkout\Order\OrderDefinition;
  8. use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  11. class OrderTransitionSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var ShopOrderRepository
  15.      */
  16.     private $shopOrderRepository;
  17.     /**
  18.      * @var OrderContextRepository
  19.      */
  20.     private $orderContextRepository;
  21.     /**
  22.      * @var EventDispatcherInterface
  23.      */
  24.     private $dispatcher;
  25.     public function __construct(
  26.         ShopOrderRepository $shopOrderRepository,
  27.         OrderContextRepository $orderContextRepository,
  28.         EventDispatcherInterface $dispatcher
  29.     ) {
  30.         $this->shopOrderRepository $shopOrderRepository;
  31.         $this->orderContextRepository $orderContextRepository;
  32.         $this->dispatcher $dispatcher;
  33.     }
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             StateMachineTransitionEvent::class => 'onStateChange',
  38.         ];
  39.     }
  40.     public function onStateChange(StateMachineTransitionEvent $event): void
  41.     {
  42.         if ($event->getEntityName() !== OrderDefinition::ENTITY_NAME) {
  43.             return;
  44.         }
  45.         try {
  46.             $orderContext $this->updateOrderContext($event);
  47.         } catch (NotFoundException $e) {
  48.             return;
  49.         }
  50.         $this->dispatcher->dispatch(
  51.             new OrderContextStateChangedEvent(
  52.                 $event->getFromPlace()->getTechnicalName(),
  53.                 $event->getToPlace()->getTechnicalName(),
  54.                 $orderContext
  55.             )
  56.         );
  57.     }
  58.     /**
  59.      * @internal
  60.      */
  61.     protected function updateOrderContext(StateMachineTransitionEvent $event): OrderContext
  62.     {
  63.         $orderContext $this->shopOrderRepository
  64.             ->fetchOneOrderContextByShopOrderId(IdValue::create($event->getEntityId()));
  65.         $orderContext->status $event->getToPlace()->getTechnicalName();
  66.         $this->orderContextRepository
  67.             ->updateContext($orderContext);
  68.         return $orderContext;
  69.     }
  70. }