Completed
Push — symfony3-wololo-packages ( fecf70...6bf04d )
by Kamil
28:46 queued 11:35
created

UnitFixedDiscountPromotionActionCommand::execute()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 13
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\Bundle\PromotionBundle\Form\Type\Action\UnitFixedDiscountConfigurationType;
15
use Sylius\Component\Core\Model\OrderInterface;
16
use Sylius\Component\Core\Model\OrderItemInterface;
17
use Sylius\Component\Core\Promotion\Filter\FilterInterface;
18
use Sylius\Component\Currency\Converter\CurrencyConverterInterface;
19
use Sylius\Component\Promotion\Model\PromotionInterface;
20
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
21
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
22
use Sylius\Component\Resource\Factory\FactoryInterface;
23
24
/**
25
 * @author Mateusz Zalewski <[email protected]>
26
 * @author Grzegorz Sadowski <[email protected]>
27
 */
28
final class UnitFixedDiscountPromotionActionCommand extends UnitDiscountPromotionActionCommand
29
{
30
    const TYPE = 'unit_fixed_discount';
31
32
    /**
33
     * @var FilterInterface
34
     */
35
    private $priceRangeFilter;
36
37
    /**
38
     * @var FilterInterface
39
     */
40
    private $taxonFilter;
41
42
    /**
43
     * @var FilterInterface
44
     */
45
    private $productFilter;
46
47
    /**
48
     * @param FactoryInterface $adjustmentFactory
49
     * @param FilterInterface $priceRangeFilter
50
     * @param FilterInterface $taxonFilter
51
     * @param FilterInterface $productFilter
52
     */
53
    public function __construct(
54
        FactoryInterface $adjustmentFactory,
55
        FilterInterface $priceRangeFilter,
56
        FilterInterface $taxonFilter,
57
        FilterInterface $productFilter
58
    ) {
59
        parent::__construct($adjustmentFactory);
60
61
        $this->priceRangeFilter = $priceRangeFilter;
62
        $this->taxonFilter = $taxonFilter;
63
        $this->productFilter = $productFilter;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function execute(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion)
70
    {
71
        if (!$subject instanceof OrderInterface) {
72
            throw new UnexpectedTypeException($subject, OrderInterface::class);
73
        }
74
75
        $amount = $this->getAmountByCurrencyCode($configuration, $subject->getCurrencyCode());
76
        if (0 === $amount) {
77
            return;
78
        }
79
80
        $filteredItems = $this->priceRangeFilter->filter(
81
            $subject->getItems()->toArray(),
82
            array_merge($configuration, ['channel' => $subject->getChannel()])
83
        );
84
        $filteredItems = $this->taxonFilter->filter($filteredItems, $configuration);
85
        $filteredItems = $this->productFilter->filter($filteredItems, $configuration);
86
87
        foreach ($filteredItems as $item) {
88
            $this->setUnitsAdjustments($item, $amount, $promotion);
89
        }
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getConfigurationFormType()
96
    {
97
        return UnitFixedDiscountConfigurationType::class;
98
    }
99
100
    /**
101
     * @param OrderItemInterface $item
102
     * @param int $amount
103
     * @param PromotionInterface $promotion
104
     */
105
    private function setUnitsAdjustments(OrderItemInterface $item, $amount, PromotionInterface $promotion)
106
    {
107
        foreach ($item->getUnits() as $unit) {
108
            $this->addAdjustmentToUnit(
109
                $unit,
110
                min($unit->getTotal(), $amount),
111
                $promotion
112
            );
113
        }
114
    }
115
116
    /**
117
     * @param array $configuration
118
     * @param string $currencyCode
119
     *
120
     * @return int
121
     */
122
    private function getAmountByCurrencyCode(array $configuration, $currencyCode)
123
    {
124
        if (!isset($configuration['amounts'][$currencyCode])) {
125
            return $configuration['base_amount'];
126
        }
127
128
        return $configuration['amounts'][$currencyCode];
129
    }
130
}
131