Completed
Push — master ( 890fb6...27e14b )
by Tomáš
03:37
created

PackageTransportCostCollection::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
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\PackageTransportCost;
9
use InvalidArgumentException;
10
use IteratorAggregate;
11
use RuntimeException;
12
13
/**
14
 * @implements \ArrayAccess<int,\Inspirum\Balikobot\Model\Values\PackageTransportCost>
15
 * @implements \IteratorAggregate<int,\Inspirum\Balikobot\Model\Values\PackageTransportCost>
16
 */
17
class PackageTransportCostCollection implements ArrayAccess, Countable, IteratorAggregate
18
{
19
    /**
20
     * Package costs
21
     *
22
     * @var array<int,\Inspirum\Balikobot\Model\Values\PackageTransportCost>|\Inspirum\Balikobot\Model\Values\PackageTransportCost[]
23
     */
24
    private $costs = [];
25
26
    /**
27
     * Shipper code
28
     *
29
     * @var string|null
30
     */
31
    private $shipper;
32
33
    /**
34
     * OrderedPackageCollection constructor
35
     *
36
     * @param string|null $shipper
37
     */
38 11
    public function __construct(string $shipper = null)
39
    {
40 11
        $this->shipper = $shipper;
41 11
    }
42
43
    /**
44
     * Add package cost
45
     *
46
     * @param \Inspirum\Balikobot\Model\Values\PackageTransportCost $package
47
     *
48
     * @return void
49
     *
50
     * @throws \InvalidArgumentException
51
     */
52 7
    public function add(PackageTransportCost $package): void
53
    {
54
        // validate package cost shipper
55 7
        $this->validateShipper($package);
56
57
        // add package cost to collection
58 7
        $this->costs[] = $package;
59 7
    }
60
61
    /**
62
     * Get shipper
63
     *
64
     * @return string
65
     */
66 3
    public function getShipper(): string
67
    {
68 3
        if ($this->shipper === null) {
69 1
            throw new RuntimeException('Collection is empty');
70
        }
71
72 2
        return $this->shipper;
73
    }
74
75
    /**
76
     * Get EIDs
77
     *
78
     * @return array<int>
79
     */
80 2
    public function getBatchIds(): array
81
    {
82
        return array_map(function (PackageTransportCost $transportCost) {
83 2
            return $transportCost->getBatchId();
84 2
        }, $this->costs);
85
    }
86
87
    /**
88
     * Get total cost for all packages
89
     *
90
     * @return float
91
     */
92 3
    public function getTotalCost(): float
93
    {
94 3
        $totalCost    = 0.0;
95 3
        $currencyCode = $this->getCurrencyCode();
96
97 3
        foreach ($this->costs as $cost) {
98 3
            if ($cost->getCurrencyCode() !== $currencyCode) {
99 1
                throw new RuntimeException('Package cost currency codes are not the same');
100
            }
101
102 3
            $totalCost += $cost->getTotalCost();
103
        }
104
105 2
        return $totalCost;
106
    }
107
108
    /**
109
     * Get currency code
110
     *
111
     * @return string
112
     */
113 4
    public function getCurrencyCode(): string
114
    {
115 4
        if (empty($this->costs)) {
116 1
            throw new RuntimeException('Collection is empty');
117
        }
118
119 3
        foreach ($this->costs as $cost) {
120 3
            return $cost->getCurrencyCode();
121
        }
122
    }
123
124
    /**
125
     * Validate shipper
126
     *
127
     * @param \Inspirum\Balikobot\Model\Values\PackageTransportCost $package
128
     *
129
     * @return void
130
     *
131
     * @throws \InvalidArgumentException
132
     */
133 9
    private function validateShipper(PackageTransportCost $package): void
134
    {
135
        // set shipper if first package in collection
136 9
        if ($this->shipper === null) {
137 1
            $this->shipper = $package->getShipper();
138
        }
139
140
        // validate shipper
141 9
        if ($this->shipper !== $package->getShipper()) {
142 2
            throw new InvalidArgumentException(
143
                sprintf(
144 2
                    'Package is from different shipper ("%s" instead of "%s")',
145 2
                    $package->getShipper(),
146 2
                    $this->shipper
147
                )
148
            );
149
        }
150 9
    }
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->costs);
162
    }
163
164
    /**
165
     * Get an item at a given offset
166
     *
167
     * @param int $key
168
     *
169
     * @return \Inspirum\Balikobot\Model\Values\PackageTransportCost
170
     */
171 2
    public function offsetGet($key)
172
    {
173 2
        return $this->costs[$key];
174
    }
175
176
    /**
177
     * Set the item at a given offset
178
     *
179
     * @param int                                                   $key
180
     * @param \Inspirum\Balikobot\Model\Values\PackageTransportCost $value
181
     *
182
     * @return void
183
     */
184 2
    public function offsetSet($key, $value)
185
    {
186 2
        $this->validateShipper($value);
187
188 2
        $this->costs[$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->costs[$key]);
201 1
    }
202
203
    /**
204
     * Count elements of an object
205
     *
206
     * @return int
207
     */
208 3
    public function count(): int
209
    {
210 3
        return count($this->costs);
211
    }
212
213
    /**
214
     * Get an iterator for the items
215
     *
216
     * @return \ArrayIterator<int,\Inspirum\Balikobot\Model\Values\PackageTransportCost>
1 ignored issue
show
Documentation introduced by
The doc-type \ArrayIterator<int,\Insp...s\PackageTransportCost> could not be parsed: Expected "|" or "end of type", but got "<" at position 14. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
217
     */
218 1
    public function getIterator(): ArrayIterator
219
    {
220 1
        return new ArrayIterator($this->costs);
221
    }
222
}
223