Passed
Push — master ( aad1d3...713f8f )
by
unknown
07:45
created

DuplicateRefundTheSameAmountChecker   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTotalAmount() 0 13 3
A __construct() 0 6 1
A check() 0 16 1
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\Checker\Refund;
13
14
use BitBag\SyliusMolliePlugin\Entity\OrderInterface;
15
use BitBag\SyliusMolliePlugin\Repository\CreditMemoRepositoryInterface;
16
use BitBag\SyliusMolliePlugin\Repository\OrderRepositoryInterface;
17
use Sylius\RefundPlugin\Command\RefundUnits;
18
19
final class DuplicateRefundTheSameAmountChecker implements DuplicateRefundTheSameAmountCheckerInterface
20
{
21
    /** @var CreditMemoRepositoryInterface */
22
    private $creditMemoRepository;
23
24
    /** @var OrderRepositoryInterface */
25
    private $orderRepository;
26
27
    public function __construct(
28
        CreditMemoRepositoryInterface $creditMemoRepository,
29
        OrderRepositoryInterface $orderRepository
30
    ) {
31
        $this->creditMemoRepository = $creditMemoRepository;
32
        $this->orderRepository = $orderRepository;
33
    }
34
35
    public function check(RefundUnits $command): bool
36
    {
37
        $dateTimeInterval = new \DateInterval(self::ONE_HOUR_INTERVAL);
38
        $now = new \DateTime('now');
39
        $now->sub($dateTimeInterval);
40
41
        /** @var OrderInterface $order */
42
        $order = $this->orderRepository->findOneBy(['number' => $command->orderNumber()]);
43
44
        $creditMemos = $this->creditMemoRepository->findByOrderNumberAndDateTime(
45
            $order->getId(),
46
            $now,
47
            $this->getTotalAmount($command)
48
        );
49
50
        return !empty($creditMemos);
51
    }
52
53
    private function getTotalAmount(RefundUnits $command): int
54
    {
55
        $total = 0;
56
57
        foreach ($command->units() as $unit) {
58
            $total += $unit->total();
59
        }
60
61
        foreach ($command->shipments() as $shipment) {
62
            $total += $shipment->total();
63
        }
64
65
        return $total;
66
    }
67
}
68