Completed
Push — remove-specs ( 8265db )
by Kamil
18:10
created

FixedDiscountPromotionActionCommand::execute()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 3
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,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $proportionalIntegerDistributor exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
53
        UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $unitsPromotionAdjustmentsApplicator exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
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);
0 ignored issues
show
Compatibility introduced by
$subject of type object<Sylius\Component\...motionSubjectInterface> is not a sub-type of object<Sylius\Component\...e\Model\OrderInterface>. It seems like you assume a child interface of the interface Sylius\Component\Promoti...omotionSubjectInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
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)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $promotionSubjectTotal exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
Comprehensibility Naming introduced by
The variable name $targetPromotionAmount exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
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