Completed
Push — master ( bf2eb3...b959e3 )
by Paweł
215:08 queued 200:46
created

FixedDiscountAction::isConfigurationValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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\Originator\Originator\OriginatorInterface;
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
 */
26
class FixedDiscountAction extends DiscountAction
27
{
28
    const TYPE = 'order_fixed_discount';
29
30
    /**
31
     * @var ProportionalIntegerDistributorInterface
32
     */
33
    private $proportionalDistributor;
34
35
    /**
36
     * @var UnitsPromotionAdjustmentsApplicatorInterface
37
     */
38
    private $unitsPromotionAdjustmentsApplicator;
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * @param ProportionalIntegerDistributorInterface $proportionalIntegerDistributor
44
     * @param UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator
45
     */
46
    public function __construct(
47
        OriginatorInterface $originator,
48
        ProportionalIntegerDistributorInterface $proportionalIntegerDistributor,
49
        UnitsPromotionAdjustmentsApplicatorInterface $unitsPromotionAdjustmentsApplicator
50
    ) {
51
        parent::__construct($originator);
52
53
        $this->proportionalDistributor = $proportionalIntegerDistributor;
54
        $this->unitsPromotionAdjustmentsApplicator = $unitsPromotionAdjustmentsApplicator;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function execute(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion)
61
    {
62
        if (!$this->isSubjectValid($subject)) {
63
            return;
64
        }
65
66
        $this->isConfigurationValid($configuration);
67
68
        $promotionAmount = $this->calculateAdjustmentAmount($subject->getPromotionSubjectTotal(), $configuration['amount']);
69
        if (0 === $promotionAmount) {
70
            return;
71
        }
72
73
        $itemsTotals = [];
74
        foreach ($subject->getItems() as $item) {
75
            $itemsTotals[] = $item->getTotal();
76
        }
77
78
        $splitPromotion = $this->proportionalDistributor->distribute($itemsTotals, $promotionAmount);
79
        $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...
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getConfigurationFormType()
86
    {
87
        return 'sylius_promotion_action_fixed_discount_configuration';
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected function isConfigurationValid(array $configuration)
94
    {
95
        Assert::keyExists($configuration, 'amount');
96
        Assert::integer($configuration['amount']);
97
    }
98
99
    /**
100
     * @param int $promotionSubjectTotal
101
     * @param int $targetPromotionAmount
102
     *
103
     * @return int
104
     */
105
    private function calculateAdjustmentAmount($promotionSubjectTotal, $targetPromotionAmount)
106
    {
107
        return -1 * min($promotionSubjectTotal, $targetPromotionAmount);
108
    }
109
}
110