Completed
Push — symfony3 ( 19c63d...99c17a )
by Kamil
18:50
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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