DefaultPackageDataCollection::offsetSet()   A
last analyzed

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 readonly 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 1
    public function offsetSet(mixed $key, mixed $value): void
49
    {
50 1
        parent::offsetSet($key, $this->clonePackageWithEid($value));
51
    }
52
53 25
    public function offsetAdd(mixed $value): void
54
    {
55 25
        parent::offsetAdd($this->clonePackageWithEid($value));
56
    }
57
58 26
    private function clonePackageWithEid(PackageData $package): PackageData
59
    {
60 26
        $package = clone $package;
61
62 26
        if ($package->hasEID() === false) {
63 24
            $package->setEID($this->newEID());
64
        }
65
66 26
        return $package;
67
    }
68
69 24
    private function newEID(): string
70
    {
71 24
        return substr(sprintf('%s%s', time(), uniqid()), -20, 20);
72
    }
73
}
74