UnitsItemOrderRefund::hasUnitRefunded()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
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\DTO\PartialRefundItem;
15
use BitBag\SyliusMolliePlugin\DTO\PartialRefundItems;
16
use Sylius\Component\Core\Model\OrderInterface;
17
use Sylius\Component\Resource\Repository\RepositoryInterface;
18
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
19
use Sylius\RefundPlugin\Model\RefundType;
20
21
final class UnitsItemOrderRefund implements UnitsItemOrderRefundInterface
22
{
23
    /** @var RepositoryInterface */
24
    private $refundUnitsRepository;
25
26
    public function __construct(RepositoryInterface $refundUnitsRepository)
27
    {
28
        $this->refundUnitsRepository = $refundUnitsRepository;
29
    }
30
31
    public function refund(OrderInterface $order, PartialRefundItems $partialRefundItems): array
32
    {
33
        $units = $order->getItemUnits();
34
35
        $unitsToRefund = [];
36
        /** @var PartialRefundItem $item */
37
        foreach ($units as $unit) {
38
            if (true === $this->hasUnitRefunded($order, $unit->getId())) {
39
                continue;
40
            }
41
42
            $partialItem = $partialRefundItems->findById($unit->getOrderItem()->getId());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $partialItem is correct as $partialRefundItems->fin...etOrderItem()->getId()) targeting BitBag\SyliusMolliePlugi...RefundItems::findById() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
43
            if (null !== $partialItem) {
44
                $unitsToRefund[] = new OrderItemUnitRefund(
45
                    $unit->getId(),
46
                    $unit->getOrderItem()->getUnitPrice()
47
                );
48
49
                $partialRefundItems->removeItem($partialItem);
0 ignored issues
show
Bug introduced by
$partialItem of type void is incompatible with the type BitBag\SyliusMolliePlugin\DTO\PartialRefundItem expected by parameter $partialRefundItem of BitBag\SyliusMolliePlugi...fundItems::removeItem(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
                $partialRefundItems->removeItem(/** @scrutinizer ignore-type */ $partialItem);
Loading history...
50
            }
51
        }
52
53
        return $unitsToRefund;
54
    }
55
56
    public function getActualRefundedQuantity(OrderInterface $order, int $itemId): int
57
    {
58
        $allItems = array_filter($this->getActualRefunded($order, $itemId));
59
60
        return  count($allItems);
61
    }
62
63
    private function getActualRefunded(OrderInterface $order, int $itemId): array
64
    {
65
        $units = $order->getItemUnits();
66
67
        foreach ($units as $unit) {
68
            if ($itemId === $unit->getOrderItem()->getId()) {
69
                $refundedUnits[] = $this->refundUnitsRepository->findOneBy([
70
                    'orderNumber' => $order->getNumber(),
71
                    'refundedUnitId' => $unit->getId(),
72
                    'type' => RefundType::orderItemUnit(),
73
                ]);
74
            }
75
        }
76
77
        return $refundedUnits ?? [];
78
    }
79
80
    private function hasUnitRefunded(OrderInterface $order, int $unitId): bool
81
    {
82
        $unitRefunded = $this->refundUnitsRepository->findOneBy([
83
            'orderNumber' => $order->getNumber(),
84
            'refundedUnitId' => $unitId,
85
            'type' => RefundType::orderItemUnit(),
86
        ]);
87
88
        return $unitRefunded ? true : false;
89
    }
90
}
91