ShipmentUnitRefund::refund()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 6
nop 3
dl 0
loc 27
rs 9.4888
c 0
b 0
f 0
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