PaymentUnitsItemRefund   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A refund() 0 17 3
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\Calculator\Refund\PaymentRefundCalculatorInterface;
15
use BitBag\SyliusMolliePlugin\DTO\PartialRefundItem;
16
use BitBag\SyliusMolliePlugin\Refund\Generator\PaymentNewUnitRefundGeneratorInterface;
17
use BitBag\SyliusMolliePlugin\Refund\Generator\PaymentRefundedGeneratorInterface;
18
use Sylius\Component\Core\Model\OrderInterface;
19
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
20
21
final class PaymentUnitsItemRefund implements PaymentUnitsItemRefundInterface
22
{
23
    /** @var PaymentRefundedGeneratorInterface */
24
    private $paymentRefundedGenerator;
25
26
    /** @var PaymentNewUnitRefundGeneratorInterface */
27
    private $paymentNewUnitRefundGenerator;
28
29
    /** @var PaymentRefundCalculatorInterface */
30
    private $paymentRefundCalculator;
31
32
    public function __construct(
33
        PaymentRefundedGeneratorInterface $paymentRefundedGenerator,
34
        PaymentNewUnitRefundGeneratorInterface $paymentNewUnitRefundGenerator,
35
        PaymentRefundCalculatorInterface $paymentRefundCalculator
36
    ) {
37
        $this->paymentRefundedGenerator = $paymentRefundedGenerator;
38
        $this->paymentNewUnitRefundGenerator = $paymentNewUnitRefundGenerator;
39
        $this->paymentRefundCalculator = $paymentRefundCalculator;
40
    }
41
42
    public function refund(OrderInterface $order, int $totalToRefund): array
43
    {
44
        $partialRefundItems = $this->paymentRefundedGenerator->generate($order);
45
        $partialRefundItems = $this->paymentNewUnitRefundGenerator->generate($order, $partialRefundItems);
46
        $partialRefundItems = $this->paymentRefundCalculator->calculate($partialRefundItems, $totalToRefund);
47
48
        /** @var PartialRefundItem $partialRefundItem */
49
        foreach ($partialRefundItems->getPartialRefundItems() as $partialRefundItem) {
50
            if ($partialRefundItem->getAmountToRefund() > 0) {
51
                $unitsToRefund[] = new OrderItemUnitRefund(
52
                    $partialRefundItem->getId(),
53
                    $partialRefundItem->getAmountToRefund()
54
                );
55
            }
56
        }
57
58
        return $unitsToRefund ?? [];
59
    }
60
}
61