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

RefundUnitsCommandValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
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\Validator\Refund;
13
14
use BitBag\SyliusMolliePlugin\Checker\Refund\DuplicateRefundTheSameAmountCheckerInterface;
15
use Sylius\RefundPlugin\Checker\OrderRefundingAvailabilityCheckerInterface;
16
use Sylius\RefundPlugin\Command\RefundUnits;
17
use Sylius\RefundPlugin\Exception\InvalidRefundAmountException;
18
use Sylius\RefundPlugin\Exception\OrderNotAvailableForRefundingException;
19
use Sylius\RefundPlugin\Model\RefundType;
20
use Sylius\RefundPlugin\Validator\RefundAmountValidatorInterface;
21
use Sylius\RefundPlugin\Validator\RefundUnitsCommandValidatorInterface;
22
23
final class RefundUnitsCommandValidator implements RefundUnitsCommandValidatorInterface
24
{
25
    /** @var OrderRefundingAvailabilityCheckerInterface */
26
    private $orderRefundingAvailabilityChecker;
27
28
    /** @var RefundAmountValidatorInterface */
29
    private $refundAmountValidator;
30
31
    /** @var DuplicateRefundTheSameAmountCheckerInterface */
32
    private $duplicateRefundTheSameAmountChecker;
33
34
    public function __construct(
35
        OrderRefundingAvailabilityCheckerInterface $orderRefundingAvailabilityChecker,
36
        RefundAmountValidatorInterface $refundAmountValidator,
37
        DuplicateRefundTheSameAmountCheckerInterface $duplicateRefundTheSameAmountChecker
38
    ) {
39
        $this->orderRefundingAvailabilityChecker = $orderRefundingAvailabilityChecker;
40
        $this->refundAmountValidator = $refundAmountValidator;
41
        $this->duplicateRefundTheSameAmountChecker = $duplicateRefundTheSameAmountChecker;
42
    }
43
44
    public function validate(RefundUnits $command): void
45
    {
46
        if (!$this->orderRefundingAvailabilityChecker->__invoke($command->orderNumber())) {
47
            throw OrderNotAvailableForRefundingException::withOrderNumber($command->orderNumber());
48
        }
49
50
        $this->refundAmountValidator->validateUnits($command->units(), RefundType::orderItemUnit());
51
        $this->refundAmountValidator->validateUnits($command->shipments(), RefundType::shipment());
52
53
        if (true === $this->duplicateRefundTheSameAmountChecker->check($command)) {
54
            throw new InvalidRefundAmountException('A duplicate refund has been detected');
55
        }
56
    }
57
}
58