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

UnitFixedDiscountPromotionActionCommand::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\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 implements ChannelBasedPromotionActionCommandInterface
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
        $channelCode = $subject->getChannel()->getCode();
76
        if (!isset($configuration[$channelCode])) {
77
            return;
78
        }
79
80
        $amount = $configuration[$channelCode]['amount'];
81
82
        if (0 === $amount) {
83
            return;
84
        }
85
86
        $filteredItems = $this->priceRangeFilter->filter(
87
            $subject->getItems()->toArray(),
88
            array_merge(['channel' => $subject->getChannel()], $configuration[$channelCode])
89
        );
90
        $filteredItems = $this->taxonFilter->filter($filteredItems, $configuration[$channelCode]);
91
        $filteredItems = $this->productFilter->filter($filteredItems, $configuration[$channelCode]);
92
93
        foreach ($filteredItems as $item) {
94
            $this->setUnitsAdjustments($item, $amount, $promotion);
95
        }
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getConfigurationFormType()
102
    {
103
        return UnitFixedDiscountConfigurationType::class;
104
    }
105
106
    /**
107
     * @param OrderItemInterface $item
108
     * @param int $amount
109
     * @param PromotionInterface $promotion
110
     */
111
    private function setUnitsAdjustments(OrderItemInterface $item, $amount, PromotionInterface $promotion)
112
    {
113
        foreach ($item->getUnits() as $unit) {
114
            $this->addAdjustmentToUnit(
115
                $unit,
116
                min($unit->getTotal(), $amount),
117
                $promotion
118
            );
119
        }
120
    }
121
}
122