DefaultTransportCostFactory::createCollection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Balikobot\Model\TransportCost;
6
7
use Inspirum\Balikobot\Client\Response\Validator;
8
use function array_map;
9
use function array_values;
10
use function count;
11
12
final class DefaultTransportCostFactory implements TransportCostFactory
13
{
14 32
    public function __construct(
15
        private readonly Validator $validator,
16
    ) {
17 32
    }
18
19
    /** @inheritDoc */
20 2
    public function create(string $carrier, array $data): TransportCost
21
    {
22 2
        return new DefaultTransportCost(
23 2
            $data['eid'],
24 2
            $carrier,
25 2
            $data['costs_total'],
26 2
            $data['currency'],
27 2
            array_map(static fn (array $part) => new DefaultTransportCostPart($part['name'], $part['cost'], $data['currency']), $data['costs_breakdown'] ?? []),
28 2
        );
29
    }
30
31
    /** @inheritDoc */
32 4
    public function createCollection(string $carrier, ?array $packages, array $data): TransportCostCollection
33
    {
34 4
        $packagesResponse = $data['packages'] ?? $data;
35
36 4
        if ($packages !== null) {
0 ignored issues
show
introduced by
The condition $packages !== null is always true.
Loading history...
37 4
            $this->validator->validateIndexes($packagesResponse, count($packages));
38
        }
39
40 3
        $this->validator->validateResponseItemsHasAttribute($packagesResponse, 'eid', $data);
41
42 2
        return new DefaultTransportCostCollection($carrier, array_values(array_map(fn (array $package): TransportCost => $this->create($carrier, $package), $packagesResponse)));
43
    }
44
}
45