|
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
|
|
|
|