ChooseShippingMethodHandler::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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