OrderShipmentPurifier   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 27
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A purify() 0 17 4
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
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\Purifier\PartialShip;
13
14
use Doctrine\Common\Collections\Collection;
15
use Sylius\Component\Core\Model\OrderInterface;
16
use Sylius\Component\Core\Model\ShipmentInterface;
17
18
final class OrderShipmentPurifier implements OrderShipmentPurifierInterface
19
{
20
    /** @var OrderMolliePartialShipInterface */
21
    private $molliePartialShip;
22
23
    public function __construct(OrderMolliePartialShipInterface $molliePartialShip)
24
    {
25
        $this->molliePartialShip = $molliePartialShip;
26
    }
27
28
    public function purify(OrderInterface $order): void
29
    {
30
        /** @var Collection $shipments */
31
        $shipments = $order->getShipments();
32
        $shipmentsToRemove = $shipments->filter(static function (ShipmentInterface $shipment): bool {
33
            return $shipment->getState() === ShipmentInterface::STATE_READY && $shipment->getUnits()->isEmpty();
34
        });
35
36
        if (count($shipmentsToRemove) === 0) {
37
            return;
38
        }
39
40
        foreach ($shipmentsToRemove as $shipmentToRemove) {
41
            $order->removeShipment($shipmentToRemove);
42
        }
43
44
        $this->molliePartialShip->partialShip($order);
45
    }
46
}
47