Completed
Push — master ( 20a9ba...cdeb22 )
by Paweł
23:39 queued 09:59
created

Shipment::addUnit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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\Shipping\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Sylius\Component\Resource\Model\TimestampableTrait;
17
18
/**
19
 * @author Paweł Jędrzejewski <[email protected]>
20
 */
21
class Shipment implements ShipmentInterface
22
{
23
    use TimestampableTrait;
24
25
    /**
26
     * @var mixed
27
     */
28
    protected $id;
29
30
    /**
31
     * @var string
32
     */
33
    protected $state = ShipmentInterface::STATE_CHECKOUT;
34
35
    /**
36
     * @var ShippingMethodInterface
37
     */
38
    protected $method;
39
40
    /**
41
     * @var Collection|ShipmentUnitInterface[]
42
     */
43
    protected $units;
44
45
    /**
46
     * @var string
47
     */
48
    protected $tracking;
49
50
    public function __construct()
51
    {
52
        $this->units = new ArrayCollection();
53
        $this->createdAt = new \DateTime();
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function __toString()
60
    {
61
        return (string) $this->id;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getId()
68
    {
69
        return $this->id;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getState()
76
    {
77
        return $this->state;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function setState($state)
84
    {
85
        $this->state = $state;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getMethod()
92
    {
93
        return $this->method;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function setMethod(ShippingMethodInterface $method)
100
    {
101
        $this->method = $method;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function getUnits()
108
    {
109
        return $this->units;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function hasUnit(ShipmentUnitInterface $unit)
116
    {
117
        return $this->units->contains($unit);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function addUnit(ShipmentUnitInterface $unit)
124
    {
125
        if (!$this->hasUnit($unit)) {
126
            $unit->setShipment($this);
127
            $this->units->add($unit);
128
        }
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function removeUnit(ShipmentUnitInterface $unit)
135
    {
136
        if ($this->hasUnit($unit)) {
137
            $unit->setShipment(null);
138
            $this->units->removeElement($unit);
139
        }
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function getShippables()
146
    {
147
        $shippables = new ArrayCollection();
148
149
        foreach ($this->units as $unit) {
150
            $shippable = $unit->getShippable();
151
            if (!$shippables->contains($shippable)) {
152
                $shippables->add($shippable);
153
            }
154
        }
155
156
        return $shippables;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function getTracking()
163
    {
164
        return $this->tracking;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function setTracking($tracking)
171
    {
172
        $this->tracking = $tracking;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function isTracked()
179
    {
180
        return null !== $this->tracking;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function getShippingWeight()
187
    {
188
        $weight = 0;
189
190
        foreach ($this->units as $unit) {
191
            $weight += $unit->getShippable()->getShippingWeight();
192
        }
193
194
        return $weight;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    public function getShippingVolume()
201
    {
202
        $volume = 0;
203
204
        foreach ($this->units as $unit) {
205
            $volume += $unit->getShippable()->getShippingVolume();
206
        }
207
208
        return $volume;
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214
    public function getShippingUnitCount()
215
    {
216
        return $this->units->count();
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222
    public function getShippingUnitTotal()
223
    {
224
        return 0;
225
    }
226
}
227