-
Notifications
You must be signed in to change notification settings - Fork 70
/
RemainingTotalProvider.php
101 lines (80 loc) · 3.71 KB
/
RemainingTotalProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\RefundPlugin\Provider;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderItemUnitInterface;
use Sylius\Component\Order\Model\AdjustableInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\RefundPlugin\Entity\RefundInterface;
use Sylius\RefundPlugin\Model\RefundTypeInterface;
use Symfony\Contracts\Service\ServiceProviderInterface;
use Webmozart\Assert\Assert;
final class RemainingTotalProvider implements RemainingTotalProviderInterface
{
private ?RepositoryInterface $orderItemUnitRepository = null;
private ?RepositoryInterface $adjustmentRepository = null;
/** @param ServiceProviderInterface<RepositoryInterface>|RepositoryInterface $refundUnitTotalProvider */
public function __construct(
private readonly ServiceProviderInterface|RepositoryInterface $refundUnitTotalProvider,
private RepositoryInterface $refundRepository,
) {
$args = func_get_args();
if ($refundUnitTotalProvider instanceof RepositoryInterface) {
if (!isset($args[2])) {
throw new \InvalidArgumentException('The 3th argument must be present.');
}
$this->orderItemUnitRepository = $refundUnitTotalProvider;
$this->adjustmentRepository = $this->refundRepository;
$this->refundRepository = $args[2];
trigger_deprecation('sylius/refund-plugin', '1.4', sprintf('Passing a "%s" as a 1st argument of "%s" constructor is deprecated and will be removed in 2.0.', RepositoryInterface::class, self::class));
}
}
public function getTotalLeftToRefund(int $id, RefundTypeInterface $type): int
{
if (null !== $this->orderItemUnitRepository) {
$unitTotal = $this->getRefundUnitTotal($id, $type);
} else {
Assert::isInstanceOf($this->refundUnitTotalProvider, ServiceProviderInterface::class);
$unitTotal = $this->refundUnitTotalProvider->get($type->getValue())->getRefundUnitTotal($id);
}
$refunds = $this->refundRepository->findBy(['refundedUnitId' => $id, 'type' => $type]);
if (count($refunds) === 0) {
return $unitTotal;
}
$refundedTotal = 0;
/** @var RefundInterface $refund */
foreach ($refunds as $refund) {
$refundedTotal += $refund->getAmount();
}
return $unitTotal - $refundedTotal;
}
private function getRefundUnitTotal(int $id, RefundTypeInterface $refundType): int
{
Assert::isInstanceOf($this->orderItemUnitRepository, RepositoryInterface::class);
Assert::isInstanceOf($this->adjustmentRepository, RepositoryInterface::class);
if ($refundType->getValue() === RefundTypeInterface::ORDER_ITEM_UNIT) {
/** @var OrderItemUnitInterface $orderItemUnit */
$orderItemUnit = $this->orderItemUnitRepository->find($id);
Assert::notNull($orderItemUnit);
return $orderItemUnit->getTotal();
}
/** @var AdjustmentInterface $shippingAdjustment */
$shippingAdjustment = $this->adjustmentRepository->findOneBy([
'id' => $id,
'type' => AdjustmentInterface::SHIPPING_ADJUSTMENT,
]);
Assert::notNull($shippingAdjustment);
$shipment = $shippingAdjustment->getShipment();
Assert::notNull($shipment);
Assert::isInstanceOf($shipment, AdjustableInterface::class);
return $shipment->getAdjustmentsTotal();
}
}