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