1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Component\Core\Promotion\Action; |
13
|
|
|
|
14
|
|
|
use Sylius\Component\Core\Distributor\ProportionalIntegerDistributorInterface; |
15
|
|
|
use Sylius\Component\Core\Promotion\Applicator\UnitsPromotionAdjustmentsApplicatorInterface; |
16
|
|
|
use Sylius\Component\Currency\Converter\CurrencyConverterInterface; |
17
|
|
|
use Sylius\Component\Promotion\Model\PromotionInterface; |
18
|
|
|
use Sylius\Component\Promotion\Model\PromotionSubjectInterface; |
19
|
|
|
use Webmozart\Assert\Assert; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
23
|
|
|
* @author Saša Stamenković <[email protected]> |
24
|
|
|
* @author Mateusz Zalewski <[email protected]> |
25
|
|
|
* @author Grzegorz Sadowski <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class FixedDiscountPromotionActionCommand extends DiscountPromotionActionCommand |
28
|
|
|
{ |
29
|
|
|
const TYPE = 'order_fixed_discount'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ProportionalIntegerDistributorInterface |
33
|
|
|
*/ |
34
|
|
|
private $proportionalDistributor; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var UnitsPromotionAdjustmentsApplicatorInterface |
38
|
|
|
*/ |
39
|
|
|
private $unitsPromotionAdjustmentsApplicator; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var CurrencyConverterInterface |
43
|
|
|
*/ |
44
|
|
|
private $currencyConverter; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ProportionalIntegerDistributorInterface $proportionalIntegerDistributor |
48
|
|
|
* @param UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator |
49
|
|
|
* @param CurrencyConverterInterface $currencyConverter |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
ProportionalIntegerDistributorInterface $proportionalIntegerDistributor, |
|
|
|
|
53
|
|
|
UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator, |
|
|
|
|
54
|
|
|
CurrencyConverterInterface $currencyConverter |
55
|
|
|
) { |
56
|
|
|
$this->proportionalDistributor = $proportionalIntegerDistributor; |
57
|
|
|
$this->unitsPromotionAdjustmentsApplicator = $unitsPromotionAdjustmentsApplicator; |
58
|
|
|
$this->currencyConverter = $currencyConverter; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function execute(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion) |
65
|
|
|
{ |
66
|
|
|
if (!$this->isSubjectValid($subject)) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->isConfigurationValid($configuration); |
71
|
|
|
|
72
|
|
|
$promotionAmount = $this->calculateAdjustmentAmount( |
73
|
|
|
$subject->getPromotionSubjectTotal(), |
74
|
|
|
$this->getAmountByCurrencyCode($configuration, $subject->getCurrencyCode()) |
75
|
|
|
); |
76
|
|
|
if (0 === $promotionAmount) { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$itemsTotals = []; |
81
|
|
|
foreach ($subject->getItems() as $item) { |
82
|
|
|
$itemsTotals[] = $item->getTotal(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$splitPromotion = $this->proportionalDistributor->distribute($itemsTotals, $promotionAmount); |
86
|
|
|
$this->unitsPromotionAdjustmentsApplicator->apply($subject, $promotion, $splitPromotion); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function getConfigurationFormType() |
93
|
|
|
{ |
94
|
|
|
return 'sylius_promotion_action_fixed_discount_configuration'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
protected function isConfigurationValid(array $configuration) |
101
|
|
|
{ |
102
|
|
|
Assert::keyExists($configuration, 'base_amount'); |
103
|
|
|
Assert::integer($configuration['base_amount']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param int $promotionSubjectTotal |
108
|
|
|
* @param int $targetPromotionAmount |
109
|
|
|
* |
110
|
|
|
* @return int |
111
|
|
|
*/ |
112
|
|
|
private function calculateAdjustmentAmount($promotionSubjectTotal, $targetPromotionAmount) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
return -1 * min($promotionSubjectTotal, $targetPromotionAmount); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param array $configuration |
119
|
|
|
* @param string $currencyCode |
120
|
|
|
* |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
|
|
private function getAmountByCurrencyCode(array $configuration, $currencyCode) |
124
|
|
|
{ |
125
|
|
|
if (!isset($configuration['amounts'][$currencyCode])) { |
126
|
|
|
return $configuration['base_amount']; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->currencyConverter->convertToBase( |
130
|
|
|
$configuration['amounts'][$currencyCode], |
131
|
|
|
$currencyCode |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.