ShipmentUnitRefund   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A refund() 0 27 5
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\Refund\Units;
13
14
use Sylius\Component\Core\Model\OrderInterface;
15
use Sylius\Component\Order\Model\AdjustmentInterface;
16
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
17
use Sylius\RefundPlugin\Model\ShipmentRefund;
18
19
final class ShipmentUnitRefund implements ShipmentUnitRefundInterface
20
{
21
    public function refund(OrderInterface $order, array $orderItemUnitRefund, int $totalToRefund): array
22
    {
23
        /** @var AdjustmentInterface $refundedShipment */
24
        $refundedShipment = $order->getAdjustments('shipping')->first();
25
26
        $totalRefunded = 0;
27
        if (!empty($orderItemUnitRefund)) {
28
            /** @var OrderItemUnitRefund $item */
29
            foreach ($orderItemUnitRefund as $item) {
30
                $totalRefunded += $item->total();
31
            }
32
33
            $totalToRefund -= $totalRefunded;
34
        }
35
36
        if ($totalToRefund <= 0) {
37
            return [];
38
        }
39
40
        if ($totalToRefund > $refundedShipment->getAmount()) {
41
            $totalToRefund = $refundedShipment->getAmount();
42
        }
43
44
        return [
45
            new ShipmentRefund(
46
                $refundedShipment->getId(),
47
                $totalToRefund
48
            ),
49
        ];
50
    }
51
}
52