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\Bundle\PromotionBundle\Form\Type\Action\FixedDiscountConfigurationType; |
15
|
|
|
use Sylius\Component\Core\Distributor\ProportionalIntegerDistributorInterface; |
16
|
|
|
use Sylius\Component\Core\Promotion\Applicator\UnitsPromotionAdjustmentsApplicatorInterface; |
17
|
|
|
use Sylius\Component\Currency\Converter\CurrencyConverterInterface; |
18
|
|
|
use Sylius\Component\Promotion\Model\PromotionInterface; |
19
|
|
|
use Sylius\Component\Promotion\Model\PromotionSubjectInterface; |
20
|
|
|
use Webmozart\Assert\Assert; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
24
|
|
|
* @author Saša Stamenković <[email protected]> |
25
|
|
|
* @author Mateusz Zalewski <[email protected]> |
26
|
|
|
* @author Grzegorz Sadowski <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class FixedDiscountPromotionActionCommand extends DiscountPromotionActionCommand implements ChannelBasedPromotionActionCommandInterface |
29
|
|
|
{ |
30
|
|
|
const TYPE = 'order_fixed_discount'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ProportionalIntegerDistributorInterface |
34
|
|
|
*/ |
35
|
|
|
private $proportionalDistributor; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var UnitsPromotionAdjustmentsApplicatorInterface |
39
|
|
|
*/ |
40
|
|
|
private $unitsPromotionAdjustmentsApplicator; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ProportionalIntegerDistributorInterface $proportionalIntegerDistributor |
44
|
|
|
* @param UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
ProportionalIntegerDistributorInterface $proportionalIntegerDistributor, |
|
|
|
|
48
|
|
|
UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator |
|
|
|
|
49
|
|
|
) { |
50
|
|
|
$this->proportionalDistributor = $proportionalIntegerDistributor; |
51
|
|
|
$this->unitsPromotionAdjustmentsApplicator = $unitsPromotionAdjustmentsApplicator; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function execute(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion) |
58
|
|
|
{ |
59
|
|
|
if (!$this->isSubjectValid($subject)) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$channelCode = $subject->getChannel()->getCode(); |
64
|
|
|
if (!isset($configuration[$channelCode])) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->isConfigurationValid($configuration[$channelCode]); |
69
|
|
|
|
70
|
|
|
$promotionAmount = $this->calculateAdjustmentAmount( |
71
|
|
|
$subject->getPromotionSubjectTotal(), |
72
|
|
|
$configuration[$channelCode]['amount'] |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
if (0 === $promotionAmount) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$itemsTotals = []; |
80
|
|
|
foreach ($subject->getItems() as $item) { |
81
|
|
|
$itemsTotals[] = $item->getTotal(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$splitPromotion = $this->proportionalDistributor->distribute($itemsTotals, $promotionAmount); |
85
|
|
|
$this->unitsPromotionAdjustmentsApplicator->apply($subject, $promotion, $splitPromotion); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function getConfigurationFormType() |
92
|
|
|
{ |
93
|
|
|
return FixedDiscountConfigurationType::class; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
protected function isConfigurationValid(array $configuration) |
100
|
|
|
{ |
101
|
|
|
Assert::keyExists($configuration, 'amount'); |
102
|
|
|
Assert::integer($configuration['amount']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param int $promotionSubjectTotal |
107
|
|
|
* @param int $targetPromotionAmount |
108
|
|
|
* |
109
|
|
|
* @return int |
110
|
|
|
*/ |
111
|
|
|
private function calculateAdjustmentAmount($promotionSubjectTotal, $targetPromotionAmount) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
return -1 * min($promotionSubjectTotal, $targetPromotionAmount); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.