<?php
/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on hello@bitbag.io.
*/
declare(strict_types=1);
namespace BitBag\SyliusB2BPlugin\EventListener;
use SM\Factory\FactoryInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Order\Processor\OrderProcessorInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Webmozart\Assert\Assert;
final class OrderCreationListener
{
private OrderProcessorInterface $orderProcessor;
private FactoryInterface $stateMachineFactory;
public function __construct(OrderProcessorInterface $orderProcessor, FactoryInterface $stateMachineFactory)
{
$this->orderProcessor = $orderProcessor;
$this->stateMachineFactory = $stateMachineFactory;
}
public function processOrderBeforeCreation(GenericEvent $event): void
{
$order = $event->getSubject();
Assert::isInstanceOf($order, OrderInterface::class);
$order->recalculateAdjustmentsTotal();
$this->orderProcessor->process($order);
}
public function completeOrderBeforeCreation(GenericEvent $event): void
{
$order = $event->getSubject();
Assert::isInstanceOf($order, OrderInterface::class);
$stateMachine = $this->stateMachineFactory->get($order, 'sylius_order_checkout');
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);
if ($order->getCheckoutState() !== OrderCheckoutStates::STATE_PAYMENT_SKIPPED) {
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
}
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE);
}
}