Completed
Push — master ( bf2eb3...b959e3 )
by Paweł
215:08 queued 200:46
created

DiscountAction::revert()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 6
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\OrderItemUnitInterface;
17
use Sylius\Component\Originator\Originator\OriginatorInterface;
18
use Sylius\Component\Promotion\Action\PromotionActionInterface;
19
use Sylius\Component\Promotion\Model\PromotionInterface;
20
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
21
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
22
use Webmozart\Assert\Assert;
23
24
/**
25
 * @author Saša Stamenković <[email protected]>
26
 * @author Mateusz Zalewski <[email protected]>
27
 */
28
abstract class DiscountAction implements PromotionActionInterface
29
{
30
    /**
31
     * @var OriginatorInterface
32
     */
33
    protected $originator;
34
35
    /**
36
     * @param OriginatorInterface $originator
37
     */
38
    public function __construct(OriginatorInterface $originator)
39
    {
40
        $this->originator = $originator;
41
    }
42
43
    /**
44
     * @param array $configuration
45
     */
46
    abstract protected function isConfigurationValid(array $configuration);
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function revert(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion)
52
    {
53
        if (!$this->isSubjectValid($subject)) {
54
            return;
55
        }
56
57
        foreach ($subject->getItems() as $item) {
58
            foreach ($item->getUnits() as $unit) {
59
                $this->removeUnitOrderPromotionAdjustmentsByOrigin($unit, $promotion);
60
            }
61
        }
62
    }
63
64
    /**
65
     * @param PromotionSubjectInterface $subject
66
     *
67
     * @return bool
68
     */
69
    protected function isSubjectValid(PromotionSubjectInterface $subject)
70
    {
71
        Assert::implementsInterface($subject, OrderInterface::class);
72
73
        return 0 !== $subject->countItems();
74
    }
75
76
    /**
77
     * @param OrderItemUnitInterface $unit
78
     * @param PromotionInterface $promotion
79
     */
80
    private function removeUnitOrderPromotionAdjustmentsByOrigin(OrderItemUnitInterface $unit, PromotionInterface $promotion)
81
    {
82
        foreach ($unit->getAdjustments(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT) as $adjustment) {
83
            if ($promotion === $this->originator->getOrigin($adjustment)) {
84
                $unit->removeAdjustment($adjustment);
85
            }
86
        }
87
    }
88
}
89