1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Handler; |
4
|
|
|
|
5
|
|
|
use SM\Factory\FactoryInterface as StateMachineFactory; |
6
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
7
|
|
|
use Sylius\Component\Core\OrderCheckoutTransitions; |
8
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
9
|
|
|
use Sylius\ShopApiPlugin\Command\CompleteOrder; |
10
|
|
|
use Sylius\ShopApiPlugin\Provider\CustomerProviderInterface; |
11
|
|
|
use Webmozart\Assert\Assert; |
12
|
|
|
|
13
|
|
|
final class CompleteOrderHandler |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var OrderRepositoryInterface |
17
|
|
|
*/ |
18
|
|
|
private $orderRepository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var CustomerProviderInterface |
22
|
|
|
*/ |
23
|
|
|
private $customerProvider; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var StateMachineFactory |
27
|
|
|
*/ |
28
|
|
|
private $stateMachineFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param OrderRepositoryInterface $orderRepository |
32
|
|
|
* @param CustomerProviderInterface $customerProvider |
33
|
|
|
* @param StateMachineFactory $stateMachineFactory |
34
|
|
|
*/ |
35
|
|
|
public function __construct( |
36
|
|
|
OrderRepositoryInterface $orderRepository, |
37
|
|
|
CustomerProviderInterface $customerProvider, |
38
|
|
|
StateMachineFactory $stateMachineFactory |
39
|
|
|
) { |
40
|
|
|
$this->orderRepository = $orderRepository; |
41
|
|
|
$this->customerProvider = $customerProvider; |
42
|
|
|
$this->stateMachineFactory = $stateMachineFactory; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function handle(CompleteOrder $completeOrder) |
46
|
|
|
{ |
47
|
|
|
/** @var OrderInterface $order */ |
48
|
|
|
$order = $this->orderRepository->findOneBy(['tokenValue' => $completeOrder->orderToken()]); |
49
|
|
|
|
50
|
|
|
Assert::notNull($order, sprintf('Order with %s token has not been found.', $completeOrder->orderToken())); |
51
|
|
|
|
52
|
|
|
$stateMachine = $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH); |
53
|
|
|
|
54
|
|
|
Assert::true($stateMachine->can(OrderCheckoutTransitions::TRANSITION_COMPLETE), sprintf('Order with %s token cannot be completed.', $completeOrder->orderToken())); |
55
|
|
|
|
56
|
|
|
$customer = $this->customerProvider->provide($completeOrder->email()); |
57
|
|
|
|
58
|
|
|
$order->setNotes($completeOrder->notes()); |
59
|
|
|
$order->setCustomer($customer); |
60
|
|
|
|
61
|
|
|
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|