Completed
Push — master ( b7e254...bcf326 )
by
unknown
09:07
created

SetStatusOrderAction   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
eloc 37
c 1
b 0
f 0
dl 0
loc 91
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A applyStateMachineOrderTransition() 0 9 2
A isShippingAllItems() 0 3 1
C execute() 0 28 12
A __construct() 0 9 1
A isConfirmNotify() 0 11 3
A applyStateMachineShipmentsTransition() 0 9 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusMolliePlugin\Action\StateMachine;
14
15
use BitBag\SyliusMolliePlugin\PartialShip\CreatePartialShipFromMollieInterface;
16
use Mollie\Api\Resources\Order;
17
use SM\Factory\FactoryInterface;
18
use Sylius\Component\Core\Model\OrderInterface;
19
use Sylius\Component\Core\Model\ShipmentInterface;
20
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
21
use Sylius\Component\Order\OrderTransitions;
22
use Sylius\Component\Shipping\ShipmentTransitions;
23
use BitBag\SyliusMolliePlugin\Transitions\PartialShip\ShipmentTransitions as ShipmentTransitionsPartial;
24
25
final class SetStatusOrderAction
26
{
27
    /** @var FactoryInterface */
28
    private $factory;
29
30
    /** @var OrderRepositoryInterface */
31
    private $orderRepository;
32
33
    /** @var CreatePartialShipFromMollieInterface */
34
    private $createPartialShipFromMollie;
35
36
    public function __construct
37
    (
38
        FactoryInterface $factory,
39
        OrderRepositoryInterface $orderRepository,
40
        CreatePartialShipFromMollieInterface $createPartialShipFromMollie
41
    ) {
42
        $this->factory = $factory;
43
        $this->orderRepository = $orderRepository;
44
        $this->createPartialShipFromMollie = $createPartialShipFromMollie;
45
    }
46
47
    public function execute(Order $order): void
48
    {
49
        if (!$order->orderNumber) {
50
            return;
51
        }
52
        /** @var OrderInterface $orderSylius */
53
        $orderSylius = $this->orderRepository->findOneBy(['id' => $order->orderNumber]);
54
55
        /** @var ShipmentInterface $shipment */
56
        $shipment = $orderSylius->getShipments()->first();
57
58
        if ($order->isCompleted()) {
59
            $this->applyStateMachineOrderTransition($orderSylius, OrderTransitions::TRANSITION_FULFILL);
60
            $this->applyStateMachineShipmentsTransition($orderSylius->getShipments()->first(), ShipmentTransitions::TRANSITION_SHIP);
61
        }
62
        if ($order->isCanceled() || $order->isExpired()) {
63
            $this->applyStateMachineOrderTransition($orderSylius, OrderTransitions::TRANSITION_CANCEL);
64
        }
65
66
        if ($order->isShipping() && $this->isConfirmNotify($order, $shipment) && false === $this->isShippingAllItems($shipment)) {
67
            return;
68
        }
69
        if ($order->isShipping() && false === $this->isShippingAllItems($shipment)) {
70
            $this->createPartialShipFromMollie->create($orderSylius, $order);
71
            $this->applyStateMachineShipmentsTransition($orderSylius->getShipments()->last(), ShipmentTransitionsPartial::TRANSITION_CREATE_AND_SHIP);
72
        }
73
        if ($order->isShipping() && true === $this->isShippingAllItems($shipment)) {
74
            $this->applyStateMachineShipmentsTransition($orderSylius->getShipments()->last(), ShipmentTransitions::TRANSITION_SHIP);
75
        }
76
    }
77
78
    private function applyStateMachineOrderTransition(OrderInterface $orderSylius, string $transitions): void
79
    {
80
        $stateMachine = $this->factory->get($orderSylius, OrderTransitions::GRAPH);
81
82
        if (!$stateMachine->can($transitions)) {
83
            return;
84
        }
85
86
        $stateMachine->apply($transitions);
87
    }
88
89
    private function applyStateMachineShipmentsTransition(ShipmentInterface $orderSylius, string $transitions): void
90
    {
91
        $stateMachine = $this->factory->get($orderSylius, ShipmentTransitions::GRAPH);
92
93
        if (!$stateMachine->can($transitions)) {
94
            return;
95
        }
96
97
        $stateMachine->apply($transitions);
98
    }
99
100
    private function isShippingAllItems(ShipmentInterface $shipment)
101
    {
102
        return $shipment->getUnits()->isEmpty();
103
    }
104
105
    private function isConfirmNotify(Order $order, ShipmentInterface $shipment): bool
106
    {
107
        // check if in mollie and sylius is the same shipped items
108
        $shippableQuantity = 0;
109
        foreach ($order->lines as $line) {
110
            if ($line->type === 'physical') {
111
                $shippableQuantity = $shippableQuantity+ $line->shippableQuantity;
112
            }
113
        }
114
115
        return $shippableQuantity === count($shipment->getUnits()->toArray());
116
    }
117
}
118