FromMollieToSyliusResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 17 3
A getShippedItemQuantity() 0 12 1
A __construct() 0 6 1
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\Resolver\PartialShip;
13
14
use BitBag\SyliusMolliePlugin\DTO\PartialShipItem;
15
use BitBag\SyliusMolliePlugin\DTO\PartialShipItems;
16
use BitBag\SyliusMolliePlugin\Remover\PartialShip\OldShipmentItemsRemoverInterface;
17
use Mollie\Api\Resources\Order;
18
use Sylius\Component\Core\Model\OrderInterface;
19
use Sylius\Component\Core\Model\OrderItemInterface;
20
use Sylius\Component\Resource\Repository\RepositoryInterface;
21
22
final class FromMollieToSyliusResolver implements FromMollieToSyliusResolverInterface
23
{
24
    /** @var RepositoryInterface */
25
    private $unitsItemRepository;
26
27
    /** @var OldShipmentItemsRemoverInterface */
28
    private $shipmentItemsRemover;
29
30
    public function __construct(
31
        RepositoryInterface $unitsItemRepository,
32
        OldShipmentItemsRemoverInterface $shipmentItemsRemover
33
    ) {
34
        $this->unitsItemRepository = $unitsItemRepository;
35
        $this->shipmentItemsRemover = $shipmentItemsRemover;
36
    }
37
38
    public function resolve(OrderInterface $order, Order $mollieOrder): OrderInterface
39
    {
40
        $shipItems = new PartialShipItems();
41
42
        foreach ($mollieOrder->lines as $line) {
43
            if ($line->status === self::SHIPPING_STATUS) {
44
                $itemShippedQuantity = $this->getShippedItemQuantity($order, $line->metadata->item_id);
45
                $shipItem = new PartialShipItem();
46
47
                $shipItem->setId($line->metadata->item_id);
48
                $shipItem->setQuantity($line->quantityShipped - $itemShippedQuantity);
49
50
                $shipItems->setPartialShipItem($shipItem);
51
            }
52
        }
53
54
        return $this->shipmentItemsRemover->remove($order, $shipItems);
55
    }
56
57
    private function getShippedItemQuantity(OrderInterface $order, int $itemId): int
58
    {
59
        $itemCollection = $order->getItems()->filter(function (OrderItemInterface $item) use ($itemId) {
60
            return $item->getId() === $itemId;
61
        });
62
63
        $refundedUnits = $this->unitsItemRepository->findBy([
64
            'orderItem' => $itemCollection->first(),
65
            'shipment' => null,
66
        ]);
67
68
        return count($refundedUnits);
69
    }
70
}
71