1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sylius\ShopApiPlugin\Handler; |
5
|
|
|
|
6
|
|
|
use SM\Factory\FactoryInterface as StateMachineFactory; |
7
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
8
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
9
|
|
|
use Sylius\Component\Core\OrderCheckoutTransitions; |
10
|
|
|
use Sylius\Component\Core\Repository\CustomerRepositoryInterface; |
11
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
12
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
13
|
|
|
use Sylius\ShopApiPlugin\Command\CompleteOrder; |
14
|
|
|
use Webmozart\Assert\Assert; |
15
|
|
|
|
16
|
|
|
final class CompleteOrderHandler |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var OrderRepositoryInterface |
20
|
|
|
*/ |
21
|
|
|
private $orderRepository; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var CustomerRepositoryInterface |
25
|
|
|
*/ |
26
|
|
|
private $customerRepository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var FactoryInterface |
30
|
|
|
*/ |
31
|
|
|
private $customerFactory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var StateMachineFactory |
35
|
|
|
*/ |
36
|
|
|
private $stateMachineFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param OrderRepositoryInterface $orderRepository |
40
|
|
|
* @param CustomerRepositoryInterface $customerRepository |
41
|
|
|
* @param FactoryInterface $customerFactory |
42
|
|
|
* @param StateMachineFactory $stateMachineFactory |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
OrderRepositoryInterface $orderRepository, |
46
|
|
|
CustomerRepositoryInterface $customerRepository, |
47
|
|
|
FactoryInterface $customerFactory, |
48
|
|
|
StateMachineFactory $stateMachineFactory |
49
|
|
|
) { |
50
|
|
|
$this->orderRepository = $orderRepository; |
51
|
|
|
$this->customerRepository = $customerRepository; |
52
|
|
|
$this->customerFactory = $customerFactory; |
53
|
|
|
$this->stateMachineFactory = $stateMachineFactory; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function handle(CompleteOrder $completeOrder) |
57
|
|
|
{ |
58
|
|
|
/** @var OrderInterface $order */ |
59
|
|
|
$order = $this->orderRepository->findOneBy(['tokenValue' => $completeOrder->orderToken()]); |
60
|
|
|
|
61
|
|
|
Assert::notNull($order, sprintf('Order with %s token has not been found.', $completeOrder->orderToken())); |
62
|
|
|
|
63
|
|
|
$stateMachine = $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH); |
64
|
|
|
|
65
|
|
|
Assert::true($stateMachine->can(OrderCheckoutTransitions::TRANSITION_COMPLETE), sprintf('Order with %s token cannot be completed.', $completeOrder->orderToken())); |
66
|
|
|
|
67
|
|
|
$customer = $this->provideCustometer($completeOrder); |
68
|
|
|
|
69
|
|
|
$order->setNotes($completeOrder->notes()); |
70
|
|
|
$order->setCustomer($customer); |
71
|
|
|
|
72
|
|
|
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param CompleteOrder $completeOrder |
77
|
|
|
* |
78
|
|
|
* @return CustomerInterface |
79
|
|
|
*/ |
80
|
|
|
private function provideCustometer(CompleteOrder $completeOrder) |
81
|
|
|
{ |
82
|
|
|
$customer = $this->customerRepository->findOneBy(['email' => $completeOrder->email()]); |
83
|
|
|
|
84
|
|
|
if (null === $customer) { |
85
|
|
|
/** @var CustomerInterface $customer */ |
86
|
|
|
$customer = $this->customerFactory->createNew(); |
87
|
|
|
$customer->setEmail($completeOrder->email()); |
88
|
|
|
|
89
|
|
|
$this->customerRepository->add($customer); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $customer; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|