Passed
Branch master (0d8fc3)
by Tomáš
12:26
created

DefaultPackageDataCollection::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Balikobot\Model\PackageData;
6
7
use Inspirum\Arrayable\BaseCollection;
8
use function sprintf;
9
use function substr;
10
use function time;
11
use function uniqid;
12
13
/**
14
 * @extends \Inspirum\Arrayable\BaseCollection<string,mixed,int,\Inspirum\Balikobot\Model\PackageData\PackageData>
15
 */
16
final class DefaultPackageDataCollection extends BaseCollection implements PackageDataCollection
17
{
18
    /**
19
     * @param array<\Inspirum\Balikobot\Model\PackageData\PackageData> $items
20
     */
21 26
    public function __construct(
22
        private string $carrier,
23
        array $items = [],
24
    ) {
25 26
        parent::__construct([]);
26
27 26
        foreach ($items as $package) {
28 21
            $this->add($package);
29
        }
30
    }
31
32 21
    public function getCarrier(): string
33
    {
34 21
        return $this->carrier;
35
    }
36
37
    /** @inheritDoc */
38 1
    public function getPackages(): array
39
    {
40 1
        return $this->getItems();
41
    }
42
43 24
    public function add(PackageData $item): void
44
    {
45 24
        $this->offsetAdd($item);
46
    }
47
48
    /** @inheritDoc */
49 1
    public function offsetSet(mixed $key, mixed $value): void
50
    {
51 1
        parent::offsetSet($key, $this->clonePackageWithEid($value));
52
    }
53
54
    /** @inheritDoc */
55 25
    public function offsetAdd(mixed $value): void
56
    {
57 25
        parent::offsetAdd($this->clonePackageWithEid($value));
58
    }
59
60 26
    private function clonePackageWithEid(PackageData $package): PackageData
61
    {
62 26
        $package = clone $package;
63
64 26
        if ($package->hasEID() === false) {
65 24
            $package->setEID($this->newEID());
66
        }
67
68 26
        return $package;
69
    }
70
71 24
    private function newEID(): string
72
    {
73 24
        return substr(sprintf('%s%s', time(), uniqid()), -20, 20);
74
    }
75
}
76