platform/src/Core/Content/Flow/Dispatching/Action/SetOrderStateAction.php line 55

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.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             self::getName() => 'handle',
  38.         ];
  39.     }
  40.     public function requirements(): array
  41.     {
  42.         return [OrderAware::class];
  43.     }
  44.     public function handle(FlowEvent $event): void
  45.     {
  46.         $config $event->getConfig();
  47.         if (empty($config)) {
  48.             return;
  49.         }
  50.         $baseEvent $event->getEvent();
  51.         if (!$baseEvent instanceof OrderAware) {
  52.             return;
  53.         }
  54.         $this->connection->beginTransaction();
  55.         try {
  56.             if (\array_key_exists('order_transaction'$config) && $config['order_transaction']) {
  57.                 $this->setOrderTransactionState($baseEvent$config);
  58.             }
  59.             if (\array_key_exists('order_delivery'$config) && $config['order_delivery']) {
  60.                 $this->setOrderDeliveryState($baseEvent$config);
  61.             }
  62.             if (\array_key_exists('order'$config) && $config['order']) {
  63.                 $this->setOrderState($baseEvent$config);
  64.             }
  65.             $this->connection->commit();
  66.         } catch (ShopwareHttpException $e) {
  67.             $this->connection->rollBack();
  68.             $this->logger->error($e->getMessage());
  69.         }
  70.     }
  71.     /**
  72.      * @throws IllegalTransitionException
  73.      */
  74.     private function setOrderState(OrderAware $baseEvent, array $config): void
  75.     {
  76.         $orderId $baseEvent->getOrderId();
  77.         $possibleTransitions $this->getPossibleTransitions($baseEvent'order'$orderId);
  78.         if (!isset($possibleTransitions[$config['order']])) {
  79.             $fromStateId $this->getOrderStateFromId($orderId);
  80.             throw new IllegalTransitionException(
  81.                 $fromStateId,
  82.                 $config['order'],
  83.                 array_values($possibleTransitions)
  84.             );
  85.         }
  86.         $this->orderService->orderStateTransition(
  87.             $orderId,
  88.             $possibleTransitions[$config['order']],
  89.             new ParameterBag(),
  90.             $baseEvent->getContext()
  91.         );
  92.     }
  93.     /**
  94.      * @throws IllegalTransitionException
  95.      */
  96.     private function setOrderDeliveryState(OrderAware $baseEvent, array $config): void
  97.     {
  98.         $query $this->connection->createQueryBuilder();
  99.         $query->select('id');
  100.         $query->from('order_delivery');
  101.         $query->where('`order_id` = :id');
  102.         $query->setParameter('id'Uuid::fromHexToBytes($baseEvent->getOrderId()));
  103.         $orderDeliveryId $query->execute()->fetchColumn();
  104.         if (!$orderDeliveryId) {
  105.             throw new IllegalTransitionException(
  106.                 '',
  107.                 $config['order_delivery'],
  108.                 []
  109.             );
  110.         }
  111.         $orderDeliveryId Uuid::fromBytesToHex($orderDeliveryId);
  112.         $possibleTransitions $this->getPossibleTransitions($baseEvent'order_delivery'$orderDeliveryId);
  113.         if (!isset($possibleTransitions[$config['order_delivery']])) {
  114.             $fromStateId $this->getOrderDeliveryFromStateId($orderDeliveryId);
  115.             throw new IllegalTransitionException(
  116.                 $fromStateId,
  117.                 $config['order_delivery'],
  118.                 array_values($possibleTransitions)
  119.             );
  120.         }
  121.         $this->orderService->orderDeliveryStateTransition(
  122.             $orderDeliveryId,
  123.             $possibleTransitions[$config['order_delivery']],
  124.             new ParameterBag(),
  125.             $baseEvent->getContext()
  126.         );
  127.     }
  128.     /**
  129.      * @throws IllegalTransitionException
  130.      */
  131.     private function setOrderTransactionState(OrderAware $baseEvent, array $config): void
  132.     {
  133.         $query $this->connection->createQueryBuilder();
  134.         $query->select('id');
  135.         $query->from('order_transaction');
  136.         $query->where('`order_id` = :id');
  137.         $query->setParameter('id'Uuid::fromHexToBytes($baseEvent->getOrderId()));
  138.         $orderTransactionId $query->execute()->fetchColumn();
  139.         if (!$orderTransactionId) {
  140.             throw new IllegalTransitionException(
  141.                 '',
  142.                 $config['order_transaction'],
  143.                 []
  144.             );
  145.         }
  146.         $orderTransactionId Uuid::fromBytesToHex($orderTransactionId);
  147.         $possibleTransitions $this->getPossibleTransitions($baseEvent'order_transaction'$orderTransactionId);
  148.         if (!isset($possibleTransitions[$config['order_transaction']])) {
  149.             $fromStateId $this->getOrderTransactionFromStateId($orderTransactionId);
  150.             throw new IllegalTransitionException(
  151.                 $fromStateId,
  152.                 $config['order_transaction'],
  153.                 array_values($possibleTransitions)
  154.             );
  155.         }
  156.         $this->orderService->orderTransactionStateTransition(
  157.             $orderTransactionId,
  158.             $possibleTransitions[$config['order_transaction']],
  159.             new ParameterBag(),
  160.             $baseEvent->getContext()
  161.         );
  162.     }
  163.     private function getPossibleTransitions(OrderAware $baseEventstring $entityNamestring $entityId): array
  164.     {
  165.         $availableTransitions $this->stateMachineRegistry->getAvailableTransitions(
  166.             $entityName,
  167.             $entityId,
  168.             'stateId',
  169.             $baseEvent->getContext()
  170.         );
  171.         $possibleTransitions = [];
  172.         foreach ($availableTransitions as $availableTransition) {
  173.             $possibleTransitions[$availableTransition->getToStateMachineState()->getTechnicalName()] = $availableTransition->getActionName();
  174.         }
  175.         return $possibleTransitions;
  176.     }
  177.     private function getOrderTransactionFromStateId(string $orderTransactionId): string
  178.     {
  179.         $query $this->connection->createQueryBuilder();
  180.         $query->select('sms.id');
  181.         $query->from('order_transaction''ot');
  182.         $query->join('ot''state_machine_state''sms''ot.state_id = sms.id');
  183.         $query->where('ot.id = :id');
  184.         $query->setParameter('id'Uuid::fromHexToBytes($orderTransactionId));
  185.         if (!$id $query->execute()->fetchColumn()) {
  186.             return '';
  187.         }
  188.         return UUID::fromBytesToHex($id);
  189.     }
  190.     private function getOrderDeliveryFromStateId(string $orderDeliveryId): string
  191.     {
  192.         $query $this->connection->createQueryBuilder();
  193.         $query->select('sms.id');
  194.         $query->from('order_delivery''od');
  195.         $query->join('od''state_machine_state''sms''sms.id = od.state_id');
  196.         $query->where('od.id = :id');
  197.         $query->setParameter('id'Uuid::fromHexToBytes($orderDeliveryId));
  198.         if (!$id $query->execute()->fetchColumn()) {
  199.             return '';
  200.         }
  201.         return UUID::fromBytesToHex($id);
  202.     }
  203.     private function getOrderStateFromId(string $orderId): string
  204.     {
  205.         $query $this->connection->createQueryBuilder();
  206.         $query->select('state_id');
  207.         $query->from('`order`');
  208.         $query->where('`order`.id = :id');
  209.         $query->setParameter('id'Uuid::fromHexToBytes($orderId));
  210.         if (!$id $query->execute()->fetchColumn()) {
  211.             return '';
  212.         }
  213.         return UUID::fromBytesToHex($id);
  214.     }
  215. }