DefaultPackageFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 47
ccs 29
cts 29
cp 1
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createCollection() 0 21 3
A create() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Balikobot\Model\Package;
6
7
use Inspirum\Balikobot\Client\Response\Validator;
8
use Inspirum\Balikobot\Definitions\Attribute;
9
use function count;
10
11
final class DefaultPackageFactory implements PackageFactory
12
{
13 34
    public function __construct(
14
        private readonly Validator $validator,
15
    ) {
16 34
    }
17
18
    /** @inheritDoc */
19 12
    public function create(string $carrier, array $data): Package
20
    {
21 12
        return new DefaultPackage(
22 12
            $carrier,
23 12
            (string) $data['package_id'],
24 12
            $data['eid'],
25 12
            (string) ($data['carrier_id'] ?? ''),
26 12
            $data['track_url'] ?? null,
27 12
            $data['label_url'] ?? null,
28 12
            $data['carrier_id_swap'] ?? null,
29 12
            $data['pieces'] ?? [],
30 12
            $data['carrier_id_final'] ?? null,
31 12
            $data['track_url_final'] ?? null,
32 12
            $data['barcode'] ?? null,
33 12
        );
34
    }
35
36
    /** @inheritDoc */
37 15
    public function createCollection(string $carrier, ?array $packages, array $data): PackageCollection
38
    {
39 15
        $packagesResponse = $data['packages'];
40 15
        if ($packages !== null) {
0 ignored issues
show
introduced by
The condition $packages !== null is always true.
Loading history...
41 14
            $this->validator->validateIndexes($packagesResponse, count($packages));
42
        }
43
44 13
        $orderedPackages = new DefaultPackageCollection(
45 13
            $carrier,
46 13
            labelsUrl: $data['labels_url'] ?? null,
47 13
        );
48
49 13
        foreach ($packagesResponse as $i => $package) {
50 13
            $this->validator->validateResponseStatus($package, $data, false);
51
52 12
            $package[Attribute::EID] ??= $packages[$i][Attribute::EID] ?? null;
53
54 12
            $orderedPackages->add($this->create($carrier, $package));
55
        }
56
57 12
        return $orderedPackages;
58
    }
59
}
60