UnitsShipmentOrderRefund::hasShipmentRefund()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
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 BitBag\SyliusMolliePlugin\Helper\ConvertOrderInterface;
15
use Mollie\Api\Resources\Order;
16
use Sylius\Component\Core\Model\OrderInterface;
17
use Sylius\Component\Order\Model\Adjustment;
18
use Sylius\Component\Resource\Repository\RepositoryInterface;
19
use Sylius\RefundPlugin\Model\RefundType;
20
use Sylius\RefundPlugin\Model\ShipmentRefund;
21
22
final class UnitsShipmentOrderRefund implements UnitsShipmentOrderRefundInterface
23
{
24
    /** @var RepositoryInterface */
25
    private $refundUnitsRepository;
26
27
    public function __construct(RepositoryInterface $refundUnitsRepository)
28
    {
29
        $this->refundUnitsRepository = $refundUnitsRepository;
30
    }
31
32
    public function refund(Order $order, OrderInterface $syliusOrder): array
33
    {
34
        if ($this->hasShipmentRefund($syliusOrder)) {
35
            return [];
36
        }
37
38
        foreach ($order->lines as $line) {
39
            if ($line->type === ConvertOrderInterface::SHIPPING_TYPE && $line->quantityRefunded > 0) {
40
                /** @var Adjustment $refundedShipment */
41
                $refundedShipment = $syliusOrder->getAdjustments('shipping')->first();
42
43
                return [
44
                    new ShipmentRefund(
45
                        $refundedShipment->getId(),
46
                        $refundedShipment->getAmount()
47
                    ),
48
                ];
49
            }
50
        }
51
52
        return [];
53
    }
54
55
    private function hasShipmentRefund(OrderInterface $order): bool
56
    {
57
        $unitRefunded = $this->refundUnitsRepository->findOneBy([
58
            'orderNumber' => $order->getNumber(),
59
            'type' => RefundType::shipment(),
60
        ]);
61
62
        return $unitRefunded ? true : false;
63
    }
64
}
65