Completed
Push — master ( d81c19...f57266 )
by Kamil
20s
created

src/Sylius/Component/Order/Model/OrderItemUnit.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
declare(strict_types=1);
13
14
namespace Sylius\Component\Order\Model;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
19
class OrderItemUnit implements OrderItemUnitInterface
20
{
21
    /**
22
     * @var int
23
     */
24
    protected $id;
25
26
    /**
27
     * @var OrderItemInterface
28
     */
29
    protected $orderItem;
30
31
    /**
32
     * @var Collection|AdjustmentInterface[]
33
     */
34
    protected $adjustments;
35
36
    /**
37
     * @var int
38
     */
39
    protected $adjustmentsTotal = 0;
40
41
    /**
42
     * @param OrderItemInterface $orderItem
43
     */
44
    public function __construct(OrderItemInterface $orderItem)
45
    {
46
        $this->orderItem = $orderItem;
47
        $this->orderItem->addUnit($this);
48
49
        $this->adjustments = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type object<Doctrine\Common\C...l\AdjustmentInterface>> of property $adjustments.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getId()
56
    {
57
        return $this->id;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getTotal(): int
64
    {
65
        $total = $this->orderItem->getUnitPrice() + $this->adjustmentsTotal;
66
67
        if ($total < 0) {
68
            return 0;
69
        }
70
71
        return $total;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getOrderItem(): OrderItemInterface
78
    {
79
        return $this->orderItem;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getAdjustments(?string $type = null): Collection
86
    {
87
        if (null === $type) {
88
            return $this->adjustments;
89
        }
90
91
        return $this->adjustments->filter(function (AdjustmentInterface $adjustment) use ($type): bool {
92
            return $type === $adjustment->getType();
93
        });
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function addAdjustment(AdjustmentInterface $adjustment): void
100
    {
101
        if ($this->hasAdjustment($adjustment)) {
102
            return;
103
        }
104
105
        $this->adjustments->add($adjustment);
106
        $this->addToAdjustmentsTotal($adjustment);
107
        $this->orderItem->recalculateUnitsTotal();
108
        $adjustment->setAdjustable($this);
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function removeAdjustment(AdjustmentInterface $adjustment): void
115
    {
116
        if ($adjustment->isLocked() || !$this->hasAdjustment($adjustment)) {
117
            return;
118
        }
119
120
        $this->adjustments->removeElement($adjustment);
121
        $this->subtractFromAdjustmentsTotal($adjustment);
122
        $this->orderItem->recalculateUnitsTotal();
123
        $adjustment->setAdjustable(null);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function hasAdjustment(AdjustmentInterface $adjustment): bool
130
    {
131
        return $this->adjustments->contains($adjustment);
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getAdjustmentsTotal(?string $type = null): int
138
    {
139
        if (null === $type) {
140
            return $this->adjustmentsTotal;
141
        }
142
143
        $total = 0;
144
        foreach ($this->getAdjustments($type) as $adjustment) {
145
            if (!$adjustment->isNeutral()) {
146
                $total += $adjustment->getAmount();
147
            }
148
        }
149
150
        return $total;
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function removeAdjustments(?string $type = null): void
157
    {
158
        foreach ($this->getAdjustments($type) as $adjustment) {
159
            $this->removeAdjustment($adjustment);
160
        }
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function recalculateAdjustmentsTotal(): void
167
    {
168
        $this->adjustmentsTotal = 0;
169
170
        foreach ($this->adjustments as $adjustment) {
171
            if (!$adjustment->isNeutral()) {
172
                $this->adjustmentsTotal += $adjustment->getAmount();
173
            }
174
        }
175
    }
176
177
    /**
178
     * @param AdjustmentInterface $adjustment
179
     */
180
    protected function addToAdjustmentsTotal(AdjustmentInterface $adjustment): void
181
    {
182
        if (!$adjustment->isNeutral()) {
183
            $this->adjustmentsTotal += $adjustment->getAmount();
184
        }
185
    }
186
187
    /**
188
     * @param AdjustmentInterface $adjustment
189
     */
190
    protected function subtractFromAdjustmentsTotal(AdjustmentInterface $adjustment): void
191
    {
192
        if (!$adjustment->isNeutral()) {
193
            $this->adjustmentsTotal -= $adjustment->getAmount();
194
        }
195
    }
196
}
197