Completed
Push — master ( 8d10db...a4c05f )
by Łukasz
10s
created

AddressOrderHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 0
loc 75
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B handle() 0 42 3
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Handler;
4
5
use SM\Factory\FactoryInterface;
6
use Sylius\Component\Core\Factory\AddressFactoryInterface;
7
use Sylius\Component\Core\Model\AddressInterface;
8
use Sylius\Component\Core\Model\OrderInterface;
9
use Sylius\Component\Core\OrderCheckoutTransitions;
10
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
11
use Sylius\ShopApiPlugin\Command\AddressOrder;
12
13
final class AddressOrderHandler
14
{
15
    /**
16
     * @var OrderRepositoryInterface
17
     */
18
    private $orderRepository;
19
20
    /**
21
     * @var AddressFactoryInterface
22
     */
23
    private $addressFactory;
24
25
    /**
26
     * @var FactoryInterface
27
     */
28
    private $stateMachineFactory;
29
30
    /**
31
     * @param OrderRepositoryInterface $orderRepository
32
     * @param AddressFactoryInterface $addressFactory
33
     * @param FactoryInterface $stateMachineFactory
34
     */
35
    public function __construct(
36
        OrderRepositoryInterface $orderRepository,
37
        AddressFactoryInterface $addressFactory,
38
        FactoryInterface $stateMachineFactory
39
    ) {
40
        $this->orderRepository = $orderRepository;
41
        $this->addressFactory = $addressFactory;
42
        $this->stateMachineFactory = $stateMachineFactory;
43
    }
44
45
    public function handle(AddressOrder $addressOrder)
46
    {
47
        /** @var OrderInterface $order */
48
        $order = $this->orderRepository->findOneBy(['tokenValue' => $addressOrder->orderToken()]);
49
50
        if (null === $order) {
51
            throw new \LogicException(sprintf('Order with %s token has not been found.', $addressOrder->orderToken()));
52
        }
53
54
        $stateMachine = $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH);
55
56
        if (!$stateMachine->can(OrderCheckoutTransitions::TRANSITION_ADDRESS)) {
57
            throw new \LogicException(sprintf('Order with %s token cannot be addressed.', $addressOrder->orderToken()));
58
        }
59
60
        /** @var AddressInterface $shippingAddress */
61
        $shippingAddress = $this->addressFactory->createNew();
62
63
        $shippingAddress->setFirstName($addressOrder->shippingAddress()->firstName());
64
        $shippingAddress->setLastName($addressOrder->shippingAddress()->lastName());
65
        $shippingAddress->setCity($addressOrder->shippingAddress()->city());
66
        $shippingAddress->setStreet($addressOrder->shippingAddress()->street());
67
        $shippingAddress->setCountryCode($addressOrder->shippingAddress()->countryCode());
68
        $shippingAddress->setPostcode($addressOrder->shippingAddress()->postcode());
69
        $shippingAddress->setProvinceName($addressOrder->shippingAddress()->provinceName());
70
71
        /** @var AddressInterface $billingAddress */
72
        $billingAddress = $this->addressFactory->createNew();
73
74
        $billingAddress->setFirstName($addressOrder->billingAddress()->firstName());
75
        $billingAddress->setLastName($addressOrder->billingAddress()->lastName());
76
        $billingAddress->setCity($addressOrder->billingAddress()->city());
77
        $billingAddress->setStreet($addressOrder->billingAddress()->street());
78
        $billingAddress->setCountryCode($addressOrder->billingAddress()->countryCode());
79
        $billingAddress->setPostcode($addressOrder->billingAddress()->postcode());
80
        $billingAddress->setProvinceName($addressOrder->billingAddress()->provinceName());
81
82
        $order->setShippingAddress($shippingAddress);
83
        $order->setBillingAddress($billingAddress);
84
85
        $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS);
86
    }
87
}
88