src/EventListener/OrderCreationListener.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener;
  4. use SM\Factory\FactoryInterface;
  5. use Sylius\Component\Core\Model\OrderInterface;
  6. use Sylius\Component\Core\OrderCheckoutTransitions;
  7. use Sylius\Component\Order\Processor\OrderProcessorInterface;
  8. use Symfony\Component\EventDispatcher\GenericEvent;
  9. use Webmozart\Assert\Assert;
  10. final class OrderCreationListener
  11. {
  12.     /** @var OrderProcessorInterface */
  13.     private $orderProcessor;
  14.     /** @var FactoryInterface */
  15.     private $stateMachineFactory;
  16.     public function __construct(OrderProcessorInterface $orderProcessorFactoryInterface $stateMachineFactory)
  17.     {
  18.         $this->orderProcessor $orderProcessor;
  19.         $this->stateMachineFactory $stateMachineFactory;
  20.     }
  21.     public function processOrderBeforeCreation(GenericEvent $event): void
  22.     {
  23.         $order $event->getSubject();
  24.         Assert::isInstanceOf($orderOrderInterface::class);
  25.         $order->recalculateAdjustmentsTotal();
  26.         $this->orderProcessor->process($order);
  27.     }
  28.     public function completeOrderBeforeCreation(GenericEvent $event): void
  29.     {
  30.         $order $event->getSubject();
  31.         Assert::isInstanceOf($orderOrderInterface::class);
  32.         $stateMachine $this->stateMachineFactory->get($order'sylius_order_checkout');
  33.         $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS);
  34.         if ($stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)) {
  35.             $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);
  36.         }
  37.         if ($stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)) {
  38.             $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
  39.         }
  40.         if ($stateMachine->can(OrderCheckoutTransitions::TRANSITION_COMPLETE)) {
  41.             $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE);
  42.         }
  43.     }
  44. }