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

ItemDiscountAction::removeUnitsAdjustment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 3
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\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\Originator\Originator\OriginatorInterface;
19
use Sylius\Component\Promotion\Action\PromotionActionInterface;
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
abstract class ItemDiscountAction implements PromotionActionInterface
29
{
30
    /**
31
     * @var FactoryInterface
32
     */
33
    protected $adjustmentFactory;
34
35
    /**
36
     * @var OriginatorInterface
37
     */
38
    protected $originator;
39
40
    /**
41
     * @param FactoryInterface $adjustmentFactory
42
     * @param OriginatorInterface $originator
43
     */
44
    public function __construct(FactoryInterface $adjustmentFactory, OriginatorInterface $originator)
45
    {
46
        $this->adjustmentFactory = $adjustmentFactory;
47
        $this->originator = $originator;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    abstract public function execute(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion);
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    abstract public function getConfigurationFormType();
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function revert(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion)
64
    {
65
        if (!$subject instanceof OrderInterface) {
66
            throw new UnexpectedTypeException($subject, OrderInterface::class);
67
        }
68
69
        foreach ($subject->getItems() as $item) {
70
            $this->removeUnitsAdjustment($item, $promotion);
71
        }
72
    }
73
74
    /**
75
     * @param OrderItemInterface $item
76
     * @param PromotionInterface $promotion
77
     */
78
    protected function removeUnitsAdjustment(OrderItemInterface $item, PromotionInterface $promotion)
79
    {
80
        foreach ($item->getUnits() as $unit) {
81
            $this->removeUnitOrderItemAdjustments($unit, $promotion);
82
        }
83
    }
84
85
    /**
86
     * @param OrderItemUnitInterface $unit
87
     * @param PromotionInterface $promotion
88
     */
89
    protected function removeUnitOrderItemAdjustments(OrderItemUnitInterface $unit, PromotionInterface $promotion)
90
    {
91
        foreach ($unit->getAdjustments(AdjustmentInterface::ORDER_ITEM_PROMOTION_ADJUSTMENT) as $adjustment) {
92
            if ($promotion === $this->originator->getOrigin($adjustment)) {
93
                $unit->removeAdjustment($adjustment);
94
            }
95
        }
96
    }
97
98
    /**
99
     * @param OrderItemUnitInterface $unit
100
     * @param int $amount
101
     * @param PromotionInterface $promotion
102
     */
103
    protected function addAdjustmentToUnit(OrderItemUnitInterface $unit, $amount, PromotionInterface $promotion)
104
    {
105
        $adjustment = $this->createAdjustment($promotion, AdjustmentInterface::ORDER_ITEM_PROMOTION_ADJUSTMENT);
106
        $adjustment->setAmount(-$amount);
107
108
        $unit->addAdjustment($adjustment);
109
    }
110
111
    /**
112
     * @param PromotionInterface $promotion
113
     * @param string $type
114
     *
115
     * @return AdjustmentInterface
116
     */
117
    protected function createAdjustment(PromotionInterface $promotion, $type = AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT)
118
    {
119
        $adjustment = $this->adjustmentFactory->createNew();
120
        $adjustment->setType($type);
121
        $adjustment->setLabel($promotion->getDescription());
122
123
        $this->originator->setOrigin($adjustment, $promotion);
124
125
        return $adjustment;
126
    }
127
}
128