src/EventSubscriber/Order/OrderStateUpdaterSubscriber.php line 31

  1. <?php
  2. /*
  3.  * This file is part of the Pellipop project.
  4.  *
  5.  * (c) Mobizel
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\EventSubscriber\Order;
  12. use App\Entity\Order\Order;
  13. use App\OrderProductionStates;
  14. use App\OrderStates;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Workflow\Event\Event;
  17. use Symfony\Component\Workflow\WorkflowInterface;
  18. use Webmozart\Assert\Assert;
  19. class OrderStateUpdaterSubscriber implements EventSubscriberInterface
  20. {
  21.     public function __construct(
  22.         private WorkflowInterface $orderStateMachine,
  23.     ) {
  24.     }
  25.     public function onCompleted(Event $event)
  26.     {
  27.         /** @var Order $order */
  28.         $order $event->getSubject();
  29.         Assert::isInstanceOf($orderOrder::class);
  30.         switch ($order->getProductionState()) {
  31.             case OrderProductionStates::STATE_ARCHIVED:
  32.                 $this->orderStateMachine->apply($orderOrderStates::TRANSITION_FUFILL);
  33.                 break;
  34.             case OrderProductionStates::STATE_CANCELED:
  35.                 $this->orderStateMachine->apply($orderOrderStates::TRANSITION_CANCEL);
  36.                 break;
  37.             case OrderProductionStates::STATE_ERRORED:
  38.                 $this->orderStateMachine->apply($orderOrderStates::TRANSITION_CANCEL);
  39.                 break;
  40.         }
  41.     }
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             'workflow.order_production.completed' => 'onCompleted',
  46.         ];
  47.     }
  48. }