DefaultPackageDataCollection   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 56
ccs 21
cts 21
cp 1
rs 10
c 1
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getCarrier() 0 3 1
A getPackages() 0 3 1
A add() 0 3 1
A __construct() 0 8 2
A offsetSet() 0 3 1
A newEID() 0 3 1
A offsetAdd() 0 3 1
A clonePackageWithEid() 0 9 2
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