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