vendor/shopware/core/Content/Flow/Dispatching/Action/SetOrderStateAction.php line 58

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Flow\Dispatching\Action;
  3. use Doctrine\DBAL\Connection;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Checkout\Order\SalesChannel\OrderService;
  6. use Shopware\Core\Framework\Event\FlowEvent;
  7. use Shopware\Core\Framework\Event\OrderAware;
  8. use Shopware\Core\Framework\ShopwareHttpException;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Shopware\Core\System\StateMachine\Exception\IllegalTransitionException;
  11. use Shopware\Core\System\StateMachine\StateMachineRegistry;
  12. use Symfony\Component\HttpFoundation\ParameterBag;
  13. class SetOrderStateAction extends FlowAction
  14. {
  15.     private Connection $connection;
  16.     private LoggerInterface $logger;
  17.     private StateMachineRegistry $stateMachineRegistry;
  18.     private OrderService $orderService;
  19.     public function __construct(
  20.         Connection $connection,
  21.         LoggerInterface $logger,
  22.         StateMachineRegistry $stateMachineRegistry,
  23.         OrderService $orderService
  24.     ) {
  25.         $this->connection $connection;
  26.         $this->logger $logger;
  27.         $this->stateMachineRegistry $stateMachineRegistry;
  28.         $this->orderService $orderService;
  29.     }
  30.     public static function getName(): string
  31.     {
  32.         return 'action.set.order.state';
  33.     }
  34.     /**
  35.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  36.      */
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             self::getName() => 'handle',
  41.         ];
  42.     }
  43.     public function requirements(): array
  44.     {
  45.         return [OrderAware::class];
  46.     }
  47.     public function handle(FlowEvent $event): void
  48.     {
  49.         $config $event->getConfig();
  50.         if (empty($config)) {
  51.             return;
  52.         }
  53.         $baseEvent $event->getEvent();
  54.         if (!$baseEvent instanceof OrderAware) {
  55.             return;
  56.         }
  57.         $this->connection->beginTransaction();
  58.         try {
  59.             if (\array_key_exists('order_transaction'$config) && $config['order_transaction']) {
  60.                 $this->setOrderTransactionState($baseEvent$config);
  61.             }
  62.             if (\array_key_exists('order_delivery'$config) && $config['order_delivery']) {
  63.                 $this->setOrderDeliveryState($baseEvent$config);
  64.             }
  65.             if (\array_key_exists('order'$config) && $config['order']) {
  66.                 $this->setOrderState($baseEvent$config);
  67.             }
  68.             $this->connection->commit();
  69.         } catch (ShopwareHttpException $e) {
  70.             $this->connection->rollBack();
  71.             $this->logger->error($e->getMessage());
  72.         }
  73.     }
  74.     /**
  75.      * @throws IllegalTransitionException
  76.      */
  77.     private function setOrderState(OrderAware $baseEvent, array $config): void
  78.     {
  79.         $orderId $baseEvent->getOrderId();
  80.         $possibleTransitions $this->getPossibleTransitions($baseEvent'order'$orderId);
  81.         if (!isset($possibleTransitions[$config['order']])) {
  82.             $fromStateId $this->getOrderStateFromId($orderId);
  83.             throw new IllegalTransitionException(
  84.                 $fromStateId,
  85.                 $config['order'],
  86.                 array_values($possibleTransitions)
  87.             );
  88.         }
  89.         $this->orderService->orderStateTransition(
  90.             $orderId,
  91.             $possibleTransitions[$config['order']],
  92.             new ParameterBag(),
  93.             $baseEvent->getContext()
  94.         );
  95.     }
  96.     /**
  97.      * @throws IllegalTransitionException
  98.      */
  99.     private function setOrderDeliveryState(OrderAware $baseEvent, array $config): void
  100.     {
  101.         $query $this->connection->createQueryBuilder();
  102.         $query->select('id');
  103.         $query->from('order_delivery');
  104.         $query->where('`order_id` = :id');
  105.         $query->setParameter('id'Uuid::fromHexToBytes($baseEvent->getOrderId()));
  106.         $orderDeliveryId $query->execute()->fetchColumn();
  107.         if (!$orderDeliveryId) {
  108.             throw new IllegalTransitionException(
  109.                 '',
  110.                 $config['order_delivery'],
  111.                 []
  112.             );
  113.         }
  114.         $orderDeliveryId Uuid::fromBytesToHex($orderDeliveryId);
  115.         $possibleTransitions $this->getPossibleTransitions($baseEvent'order_delivery'$orderDeliveryId);
  116.         if (!isset($possibleTransitions[$config['order_delivery']])) {
  117.             $fromStateId $this->getOrderDeliveryFromStateId($orderDeliveryId);
  118.             throw new IllegalTransitionException(
  119.                 $fromStateId,
  120.                 $config['order_delivery'],
  121.                 array_values($possibleTransitions)
  122.             );
  123.         }
  124.         $this->orderService->orderDeliveryStateTransition(
  125.             $orderDeliveryId,
  126.             $possibleTransitions[$config['order_delivery']],
  127.             new ParameterBag(),
  128.             $baseEvent->getContext()
  129.         );
  130.     }
  131.     /**
  132.      * @throws IllegalTransitionException
  133.      */
  134.     private function setOrderTransactionState(OrderAware $baseEvent, array $config): void
  135.     {
  136.         $query $this->connection->createQueryBuilder();
  137.         $query->select('id');
  138.         $query->from('order_transaction');
  139.         $query->where('`order_id` = :id');
  140.         $query->setParameter('id'Uuid::fromHexToBytes($baseEvent->getOrderId()));
  141.         $orderTransactionId $query->execute()->fetchColumn();
  142.         if (!$orderTransactionId) {
  143.             throw new IllegalTransitionException(
  144.                 '',
  145.                 $config['order_transaction'],
  146.                 []
  147.             );
  148.         }
  149.         $orderTransactionId Uuid::fromBytesToHex($orderTransactionId);
  150.         $possibleTransitions $this->getPossibleTransitions($baseEvent'order_transaction'$orderTransactionId);
  151.         if (!isset($possibleTransitions[$config['order_transaction']])) {
  152.             $fromStateId $this->getOrderTransactionFromStateId($orderTransactionId);
  153.             throw new IllegalTransitionException(
  154.                 $fromStateId,
  155.                 $config['order_transaction'],
  156.                 array_values($possibleTransitions)
  157.             );
  158.         }
  159.         $this->orderService->orderTransactionStateTransition(
  160.             $orderTransactionId,
  161.             $possibleTransitions[$config['order_transaction']],
  162.             new ParameterBag(),
  163.             $baseEvent->getContext()
  164.         );
  165.     }
  166.     private function getPossibleTransitions(OrderAware $baseEventstring $entityNamestring $entityId): array
  167.     {
  168.         $availableTransitions $this->stateMachineRegistry->getAvailableTransitions(
  169.             $entityName,
  170.             $entityId,
  171.             'stateId',
  172.             $baseEvent->getContext()
  173.         );
  174.         $possibleTransitions = [];
  175.         foreach ($availableTransitions as $availableTransition) {
  176.             $possibleTransitions[$availableTransition->getToStateMachineState()->getTechnicalName()] = $availableTransition->getActionName();
  177.         }
  178.         return $possibleTransitions;
  179.     }
  180.     private function getOrderTransactionFromStateId(string $orderTransactionId): string
  181.     {
  182.         $query $this->connection->createQueryBuilder();
  183.         $query->select('sms.id');
  184.         $query->from('order_transaction''ot');
  185.         $query->join('ot''state_machine_state''sms''ot.state_id = sms.id');
  186.         $query->where('ot.id = :id');
  187.         $query->setParameter('id'Uuid::fromHexToBytes($orderTransactionId));
  188.         if (!$id $query->execute()->fetchColumn()) {
  189.             return '';
  190.         }
  191.         return UUID::fromBytesToHex($id);
  192.     }
  193.     private function getOrderDeliveryFromStateId(string $orderDeliveryId): string
  194.     {
  195.         $query $this->connection->createQueryBuilder();
  196.         $query->select('sms.id');
  197.         $query->from('order_delivery''od');
  198.         $query->join('od''state_machine_state''sms''sms.id = od.state_id');
  199.         $query->where('od.id = :id');
  200.         $query->setParameter('id'Uuid::fromHexToBytes($orderDeliveryId));
  201.         if (!$id $query->execute()->fetchColumn()) {
  202.             return '';
  203.         }
  204.         return UUID::fromBytesToHex($id);
  205.     }
  206.     private function getOrderStateFromId(string $orderId): string
  207.     {
  208.         $query $this->connection->createQueryBuilder();
  209.         $query->select('state_id');
  210.         $query->from('`order`');
  211.         $query->where('`order`.id = :id');
  212.         $query->setParameter('id'Uuid::fromHexToBytes($orderId));
  213.         if (!$id $query->execute()->fetchColumn()) {
  214.             return '';
  215.         }
  216.         return UUID::fromBytesToHex($id);
  217.     }
  218. }