|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use SM\Factory\FactoryInterface; |
|
6
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
7
|
|
|
use Sylius\Component\Core\Model\ShippingMethodInterface; |
|
8
|
|
|
use Sylius\Component\Core\OrderCheckoutTransitions; |
|
9
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
|
10
|
|
|
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface; |
|
11
|
|
|
use Sylius\Component\Shipping\Checker\ShippingMethodEligibilityCheckerInterface; |
|
12
|
|
|
use Sylius\ShopApiPlugin\Command\ChooseShippingMethod; |
|
13
|
|
|
use Webmozart\Assert\Assert; |
|
14
|
|
|
|
|
15
|
|
|
final class ChooseShippingMethodHandler |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var OrderRepositoryInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $orderRepository; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ShippingMethodRepositoryInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $shippingMethodRepository; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ShippingMethodEligibilityCheckerInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $eligibilityChecker; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var FactoryInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $stateMachineFactory; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param OrderRepositoryInterface $orderRepository |
|
39
|
|
|
* @param ShippingMethodRepositoryInterface $shippingMethodRepository |
|
40
|
|
|
* @param ShippingMethodEligibilityCheckerInterface $eligibilityChecker |
|
41
|
|
|
* @param FactoryInterface $stateMachineFactory |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
OrderRepositoryInterface $orderRepository, |
|
45
|
|
|
ShippingMethodRepositoryInterface $shippingMethodRepository, |
|
46
|
|
|
ShippingMethodEligibilityCheckerInterface $eligibilityChecker, |
|
47
|
|
|
FactoryInterface $stateMachineFactory |
|
48
|
|
|
) { |
|
49
|
|
|
$this->orderRepository = $orderRepository; |
|
50
|
|
|
$this->shippingMethodRepository = $shippingMethodRepository; |
|
51
|
|
|
$this->eligibilityChecker = $eligibilityChecker; |
|
52
|
|
|
$this->stateMachineFactory = $stateMachineFactory; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param ChooseShippingMethod $chooseShippingMethod |
|
57
|
|
|
*/ |
|
58
|
|
|
public function handle(ChooseShippingMethod $chooseShippingMethod) |
|
59
|
|
|
{ |
|
60
|
|
|
/** @var OrderInterface $cart */ |
|
61
|
|
|
$cart = $this->orderRepository->findOneBy(['tokenValue' => $chooseShippingMethod->orderToken()]); |
|
62
|
|
|
|
|
63
|
|
|
Assert::notNull($cart, 'Cart has not been found.'); |
|
64
|
|
|
|
|
65
|
|
|
$stateMachine = $this->stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH); |
|
66
|
|
|
|
|
67
|
|
|
Assert::true($stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING), 'Order cannot have shipment method assigned.'); |
|
68
|
|
|
|
|
69
|
|
|
/** @var ShippingMethodInterface $shippingMethod */ |
|
70
|
|
|
$shippingMethod = $this->shippingMethodRepository->findOneBy(['code' => $chooseShippingMethod->shippingMethod()]); |
|
71
|
|
|
|
|
72
|
|
|
Assert::notNull($shippingMethod, 'Shipping method has not been found'); |
|
73
|
|
|
Assert::true(isset($cart->getShipments()[$chooseShippingMethod->shipmentIdentifier()]), 'Shipping method has not been found.'); |
|
74
|
|
|
|
|
75
|
|
|
$shipment = $cart->getShipments()[$chooseShippingMethod->shipmentIdentifier()]; |
|
76
|
|
|
|
|
77
|
|
|
Assert::true($this->eligibilityChecker->isEligible($shipment, $shippingMethod), 'Given shipment is not eligible for provided shipping method.'); |
|
78
|
|
|
|
|
79
|
|
|
$shipment->setMethod($shippingMethod); |
|
80
|
|
|
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|