Completed
Push — master ( da2f6e...d3b8cb )
by Kamil
20:06
created

ItemFixedDiscountAction::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
dl 0
loc 16
rs 9.2
c 2
b 1
f 1
cc 4
eloc 8
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\Component\Core\Model\AdjustmentInterface;
15
use Sylius\Component\Core\Model\OrderInterface;
16
use Sylius\Component\Core\Model\OrderItemInterface;
17
use Sylius\Component\Core\Model\OrderItemUnitInterface;
18
use Sylius\Component\Core\Promotion\Filter\TaxonFilterInterface;
19
use Sylius\Component\Originator\Originator\OriginatorInterface;
20
use Sylius\Component\Promotion\Model\PromotionInterface;
21
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
22
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
23
use Sylius\Component\Resource\Factory\FactoryInterface;
24
25
/**
26
 * @author Mateusz Zalewski <[email protected]>
27
 */
28
class ItemFixedDiscountAction extends ItemDiscountAction
29
{
30
    const TYPE = 'item_fixed_discount';
31
32
    /**
33
     * @var TaxonFilterInterface
34
     */
35
    private $taxonFilter;
36
37
    /**
38
     * @param FactoryInterface $adjustmentFactory
39
     * @param OriginatorInterface $originator
40
     * @param TaxonFilterInterface $taxonFilter
41
     */
42
    public function __construct(
43
        FactoryInterface $adjustmentFactory,
44
        OriginatorInterface $originator,
45
        TaxonFilterInterface $taxonFilter
46
    ) {
47
        parent::__construct($adjustmentFactory, $originator);
48
49
        $this->taxonFilter = $taxonFilter;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function execute(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion)
56
    {
57
        if (!$subject instanceof OrderInterface) {
58
            throw new UnexpectedTypeException($subject, OrderInterface::class);
59
        }
60
61
        if (0 === $configuration['amount']) {
62
            return;
63
        }
64
65
        $filteredItems = $this->taxonFilter->filter($subject->getItems()->toArray(), $configuration);
66
67
        foreach ($filteredItems as $item) {
68
            $this->setUnitsAdjustments($item, $configuration['amount'], $promotion);
69
        }
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getConfigurationFormType()
76
    {
77
        return 'sylius_promotion_action_fixed_discount_configuration';
78
    }
79
80
    /**
81
     * @param OrderItemInterface $item
82
     * @param int $amount
83
     * @param PromotionInterface $promotion
84
     */
85
    private function setUnitsAdjustments(OrderItemInterface $item, $amount, PromotionInterface $promotion)
86
    {
87
        foreach ($item->getUnits() as $unit) {
88
            $this->addAdjustmentToUnit(
89
                $unit,
90
                min($unit->getTotal(), $amount),
91
                $promotion
92
            );
93
        }
94
    }
95
}
96