vendor/bitbag/sylius-b2b-plugin/src/AdminOrderManagement/EventListener/OrderCreationListener.php line 43

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file has been created by developers from BitBag.
  4.  * Feel free to contact us once you face any issues or want to start
  5.  * You can find more information about us on https://bitbag.io and write us
  6.  * an email on hello@bitbag.io.
  7.  */
  8. declare(strict_types=1);
  9. namespace BitBag\SyliusB2BPlugin\EventListener;
  10. use SM\Factory\FactoryInterface;
  11. use Sylius\Component\Core\Model\OrderInterface;
  12. use Sylius\Component\Core\OrderCheckoutStates;
  13. use Sylius\Component\Core\OrderCheckoutTransitions;
  14. use Sylius\Component\Order\Processor\OrderProcessorInterface;
  15. use Symfony\Component\EventDispatcher\GenericEvent;
  16. use Webmozart\Assert\Assert;
  17. final class OrderCreationListener
  18. {
  19.     private OrderProcessorInterface $orderProcessor;
  20.     private FactoryInterface $stateMachineFactory;
  21.     public function __construct(OrderProcessorInterface $orderProcessorFactoryInterface $stateMachineFactory)
  22.     {
  23.         $this->orderProcessor $orderProcessor;
  24.         $this->stateMachineFactory $stateMachineFactory;
  25.     }
  26.     public function processOrderBeforeCreation(GenericEvent $event): void
  27.     {
  28.         $order $event->getSubject();
  29.         Assert::isInstanceOf($orderOrderInterface::class);
  30.         $order->recalculateAdjustmentsTotal();
  31.         $this->orderProcessor->process($order);
  32.     }
  33.     public function completeOrderBeforeCreation(GenericEvent $event): void
  34.     {
  35.         $order $event->getSubject();
  36.         Assert::isInstanceOf($orderOrderInterface::class);
  37.         $stateMachine $this->stateMachineFactory->get($order'sylius_order_checkout');
  38.         $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS);
  39.         $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);
  40.         if ($order->getCheckoutState() !== OrderCheckoutStates::STATE_PAYMENT_SKIPPED) {
  41.             $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
  42.         }
  43.         $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE);
  44.     }
  45. }