CreateOrderHandler::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 9.1563
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusVueStorefrontPlugin\CommandHandler\Order;
14
15
use BitBag\SyliusVueStorefrontPlugin\Command\Order\CreateOrder;
16
use BitBag\SyliusVueStorefrontPlugin\Sylius\Provider\AddressProviderInterface;
17
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
18
use Sylius\Component\Core\Model\CustomerInterface;
19
use Sylius\Component\Core\Model\OrderInterface;
20
use Sylius\Component\Core\OrderCheckoutTransitions;
21
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
22
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
23
use Webmozart\Assert\Assert;
24
25
final class CreateOrderHandler implements MessageHandlerInterface
26
{
27
    /** @var OrderRepositoryInterface */
28
    private $cartRepository;
29
30
    /** @var AddressProviderInterface */
31
    private $addressProvider;
32
33
    /** @var StateMachineFactoryInterface */
34
    private $stateMachineFactory;
35
36
    public function __construct(
37
        OrderRepositoryInterface $cartRepository,
38
        AddressProviderInterface $addressProvider,
39
        StateMachineFactoryInterface $stateMachineFactory
40
    ) {
41
        $this->cartRepository = $cartRepository;
42
        $this->addressProvider = $addressProvider;
43
        $this->stateMachineFactory = $stateMachineFactory;
44
    }
45
46
    public function __invoke(CreateOrder $createOrder): void
47
    {
48
        /** @var OrderInterface $cart */
49
        $cart = $this->cartRepository->findOneBy([
50
            'tokenValue' => $createOrder->cartId(),
51
            'shippingState' => OrderInterface::STATE_CART,
52
        ]);
53
        Assert::notNull($cart, sprintf('Cart with token value of %s has not been found.', $createOrder->cartId()));
54
55
        /** @var CustomerInterface $customer */
56
        $customer = $cart->getCustomer();
57
        Assert::notNull($customer, sprintf('Cart `%s` has no valid customer assigned.', $cart->getTokenValue()));
58
59
        $shippingAddress = $this->addressProvider->provide($createOrder->addressInformation()->getShippingAddress());
60
        $cart->setShippingAddress($shippingAddress);
61
62
        $billingAddress = $this->addressProvider->provide($createOrder->addressInformation()->getBillingAddress());
63
        $cart->setBillingAddress($billingAddress);
64
65
        $stateMachine = $this->stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH);
66
67
        Assert::true(
68
            $stateMachine->can(OrderCheckoutTransitions::TRANSITION_ADDRESS),
69
            sprintf('Order with %s token cannot be addressed.', $createOrder->cartId())
70
        );
71
        $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS);
72
73
        Assert::true($stateMachine->can(
74
            OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING),
75
            'Order cannot have shipment method assigned.'
76
        );
77
        $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);
78
79
        Assert::true($stateMachine->can(
80
            OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT),
81
            'Order cannot have payment method assigned.'
82
        );
83
        $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
84
85
        Assert::true($stateMachine->can(
86
            OrderCheckoutTransitions::TRANSITION_COMPLETE),
87
            sprintf('Order with %s token cannot be completed.', $createOrder->cartId())
88
        );
89
        $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE);
90
91
        $cart->setCreatedAt(new \DateTime());
92
    }
93
}
94