Completed
Push — master ( 26132b...c9e982 )
by Kamil
31:04 queued 12:47
created

FixedDiscountPromotionActionCommand::getAmountByCurrencyCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
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,
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...
48
        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...
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);
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...
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)
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...
112
    {
113
        return -1 * min($promotionSubjectTotal, $targetPromotionAmount);
114
    }
115
}
116