vendor/store.shopware.com/swagb2bplatform/components/Order/BridgePlatform/OrderTransitionSubscriber.php line 39

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.     private ShopOrderRepository $shopOrderRepository;
  14.     private OrderContextRepository $orderContextRepository;
  15.     private EventDispatcherInterface $dispatcher;
  16.     public function __construct(
  17.         ShopOrderRepository $shopOrderRepository,
  18.         OrderContextRepository $orderContextRepository,
  19.         EventDispatcherInterface $dispatcher
  20.     ) {
  21.         $this->shopOrderRepository $shopOrderRepository;
  22.         $this->orderContextRepository $orderContextRepository;
  23.         $this->dispatcher $dispatcher;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             StateMachineTransitionEvent::class => 'onStateChange',
  29.         ];
  30.     }
  31.     public function onStateChange(StateMachineTransitionEvent $event): void
  32.     {
  33.         if ($event->getEntityName() !== OrderDefinition::ENTITY_NAME) {
  34.             return;
  35.         }
  36.         try {
  37.             $orderContext $this->updateOrderContext($event);
  38.         } catch (NotFoundException $e) {
  39.             return;
  40.         }
  41.         $this->dispatcher->dispatch(
  42.             new OrderContextStateChangedEvent(
  43.                 $event->getFromPlace()->getTechnicalName(),
  44.                 $event->getToPlace()->getTechnicalName(),
  45.                 $orderContext
  46.             )
  47.         );
  48.     }
  49.     /**
  50.      * @internal
  51.      */
  52.     protected function updateOrderContext(StateMachineTransitionEvent $event): OrderContext
  53.     {
  54.         $orderContext $this->shopOrderRepository
  55.             ->fetchOneOrderContextByShopOrderId(IdValue::create($event->getEntityId()));
  56.         $orderContext->status $event->getToPlace()->getTechnicalName();
  57.         $this->orderContextRepository
  58.             ->updateContext($orderContext);
  59.         return $orderContext;
  60.     }
  61. }