Passed
Branch master (837a03)
by Tomáš
02:48
created

OrderedPackageCollection::getPackageIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Inspirum\Balikobot\Model\Aggregates;
4
5
use ArrayAccess;
6
use ArrayIterator;
7
use Countable;
8
use Inspirum\Balikobot\Model\Values\OrderedPackage;
9
use InvalidArgumentException;
10
use IteratorAggregate;
11
use RuntimeException;
12
13
/**
14
 * @implements \ArrayAccess<int,\Inspirum\Balikobot\Model\Values\OrderedPackage>
15
 * @implements \IteratorAggregate<int,\Inspirum\Balikobot\Model\Values\OrderedPackage>
16
 */
17
class OrderedPackageCollection implements ArrayAccess, Countable, IteratorAggregate
18
{
19
    /**
20
     * Packages
21
     *
22
     * @var array<int,\Inspirum\Balikobot\Model\Values\OrderedPackage>
23
     */
24
    private $packages = [];
25
26
    /**
27
     * Shipper code
28
     *
29
     * @var string|null
30
     */
31
    private $shipper;
32
33
    /**
34
     * Labels URL
35
     *
36
     * @var string|null
37
     */
38
    private $labelsUrl;
39
40
    /**
41
     * OrderedPackageCollection constructor
42
     *
43
     * @param string|null $shipper
44
     */
45 49
    public function __construct(string $shipper = null)
46
    {
47 49
        $this->shipper = $shipper;
48 49
    }
49
50
    /**
51
     * Add package
52
     *
53
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
54
     *
55
     * @return void
56
     *
57
     * @throws \InvalidArgumentException
58
     */
59 44
    public function add(OrderedPackage $package): void
60
    {
61
        // validate package shipper
62 44
        $this->validateShipper($package);
63
64
        // add package to collection
65 44
        $this->packages[] = $package;
66 44
    }
67
68
    /**
69
     * Get shipper
70
     *
71
     * @return string
72
     */
73 34
    public function getShipper(): string
74
    {
75 34
        if ($this->shipper === null) {
76 1
            throw new RuntimeException('Collection is empty');
77
        }
78
79 33
        return $this->shipper;
80
    }
81
82
    /**
83
     * Get package IDs
84
     *
85
     * @return array<string>
86
     */
87 19
    public function getPackageIds(): array
88
    {
89 19
        return array_map(function (OrderedPackage $package) {
90 19
            return $package->getPackageId();
91 19
        }, $this->packages);
92
    }
93
94
    /**
95
     * Get carrier IDs
96
     *
97
     * @return array<string>
98
     */
99 18
    public function getCarrierIds(): array
100
    {
101 18
        return array_map(function (OrderedPackage $package) {
102 18
            return $package->getCarrierId();
103 18
        }, $this->packages);
104
    }
105
106
    /**
107
     * @param string|null $labelsUrl
108
     *
109
     * @return void
110
     */
111 11
    public function setLabelsUrl(?string $labelsUrl): void
112
    {
113 11
        $this->labelsUrl = $labelsUrl;
114 11
    }
115
116
    /**
117
     * @return string|null
118
     */
119 2
    public function getLabelsUrl(): ?string
120
    {
121 2
        return $this->labelsUrl;
122
    }
123
124
    /**
125
     * Validate shipper
126
     *
127
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
128
     *
129
     * @return void
130
     *
131
     * @throws \InvalidArgumentException
132
     */
133 46
    private function validateShipper(OrderedPackage $package): void
134
    {
135
        // set shipper if first package in collection
136 46
        if ($this->shipper === null) {
137 41
            $this->shipper = $package->getShipper();
138
        }
139
140
        // validate shipper
141 46
        if ($this->shipper !== $package->getShipper()) {
142 2
            throw new InvalidArgumentException(
143 2
                sprintf(
144 2
                    'Package is from different shipper ("%s" instead of "%s")',
145 2
                    $package->getShipper(),
146 2
                    $this->shipper
147
                )
148
            );
149
        }
150 46
    }
151
152
    /**
153
     * Determine if an item exists at an offset
154
     *
155
     * @param int $key
156
     *
157
     * @return bool
158
     */
159 1
    public function offsetExists($key)
160
    {
161 1
        return array_key_exists($key, $this->packages);
162
    }
163
164
    /**
165
     * Get an item at a given offset
166
     *
167
     * @param int $key
168
     *
169
     * @return \Inspirum\Balikobot\Model\Values\OrderedPackage
170
     */
171 7
    public function offsetGet($key)
172
    {
173 7
        return $this->packages[$key];
174
    }
175
176
    /**
177
     * Set the item at a given offset
178
     *
179
     * @param int                                             $key
180
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $value
181
     *
182
     * @return void
183
     */
184 2
    public function offsetSet($key, $value)
185
    {
186 2
        $this->validateShipper($value);
187
188 2
        $this->packages[$key] = $value;
189 2
    }
190
191
    /**
192
     * Unset the item at a given offset
193
     *
194
     * @param int $key
195
     *
196
     * @return void
197
     */
198 1
    public function offsetUnset($key)
199
    {
200 1
        unset($this->packages[$key]);
201 1
    }
202
203
    /**
204
     * Count elements of an object
205
     *
206
     * @return int
207
     */
208 7
    public function count(): int
209
    {
210 7
        return count($this->packages);
211
    }
212
213
    /**
214
     * Get an iterator for the items
215
     *
216
     * @return \ArrayIterator<int,\Inspirum\Balikobot\Model\Values\OrderedPackage>
217
     */
218 1
    public function getIterator(): ArrayIterator
219
    {
220 1
        return new ArrayIterator($this->packages);
221
    }
222
}
223