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

src/Sylius/Component/Shipping/Model/Shipment.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\Shipping\Model;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Sylius\Component\Resource\Model\TimestampableTrait;
19
20
class Shipment implements ShipmentInterface
21
{
22
    use TimestampableTrait;
23
24
    /**
25
     * @var mixed
26
     */
27
    protected $id;
28
29
    /**
30
     * @var string
31
     */
32
    protected $state = ShipmentInterface::STATE_CART;
33
34
    /**
35
     * @var ShippingMethodInterface
36
     */
37
    protected $method;
38
39
    /**
40
     * @var Collection|ShipmentUnitInterface[]
41
     */
42
    protected $units;
43
44
    /**
45
     * @var string
46
     */
47
    protected $tracking;
48
49
    public function __construct()
50
    {
51
        $this->units = 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...ShipmentUnitInterface>> of property $units.

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...
52
        $this->createdAt = new \DateTime();
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function __toString(): string
59
    {
60
        return (string) $this->getId();
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getId()
67
    {
68
        return $this->id;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getState(): ?string
75
    {
76
        return $this->state;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function setState(?string $state): void
83
    {
84
        $this->state = $state;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getMethod(): ?ShippingMethodInterface
91
    {
92
        return $this->method;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function setMethod(?ShippingMethodInterface $method): void
99
    {
100
        $this->method = $method;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getUnits(): Collection
107
    {
108
        return $this->units;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function hasUnit(ShipmentUnitInterface $unit): bool
115
    {
116
        return $this->units->contains($unit);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function addUnit(ShipmentUnitInterface $unit): void
123
    {
124
        if (!$this->hasUnit($unit)) {
125
            $unit->setShipment($this);
126
            $this->units->add($unit);
127
        }
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function removeUnit(ShipmentUnitInterface $unit): void
134
    {
135
        if ($this->hasUnit($unit)) {
136
            $unit->setShipment(null);
137
            $this->units->removeElement($unit);
138
        }
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getTracking(): ?string
145
    {
146
        return $this->tracking;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function setTracking(?string $tracking): void
153
    {
154
        $this->tracking = $tracking;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function isTracked(): bool
161
    {
162
        return null !== $this->tracking;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function getShippables(): Collection
169
    {
170
        $shippables = new ArrayCollection();
171
172
        foreach ($this->units as $unit) {
173
            $shippable = $unit->getShippable();
174
            if (!$shippables->contains($shippable)) {
175
                $shippables->add($shippable);
176
            }
177
        }
178
179
        return $shippables;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function getShippingWeight(): float
186
    {
187
        $weight = 0;
188
189
        foreach ($this->units as $unit) {
190
            $weight += $unit->getShippable()->getShippingWeight();
191
        }
192
193
        return $weight;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function getShippingVolume(): float
200
    {
201
        $volume = 0;
202
203
        foreach ($this->units as $unit) {
204
            $volume += $unit->getShippable()->getShippingVolume();
205
        }
206
207
        return $volume;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public function getShippingUnitCount(): int
214
    {
215
        return $this->units->count();
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221
    public function getShippingUnitTotal(): int
222
    {
223
        return 0;
224
    }
225
}
226