DefaultServiceFactory::create()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Balikobot\Model\Service;
6
7
use Inspirum\Balikobot\Model\Country\CountryFactory;
8
use function array_key_exists;
9
use function array_map;
10
use function array_values;
11
12
final class DefaultServiceFactory implements ServiceFactory
13
{
14 24
    public function __construct(
15
        private readonly CountryFactory $countryFactory,
16
    ) {
17 24
    }
18
19
    /** @inheritDoc */
20 13
    public function create(string $carrier, array $data): Service
21
    {
22 13
        return new DefaultService(
23 13
            (string) $data['service_type'],
24 13
            $data['service_type_name'] ?? ($data['name'] ?? null),
25 13
            array_key_exists('services', $data) ? $this->createOptionCollection($data) : null,
26 13
            array_key_exists('countries', $data) ? $this->countryFactory->createCodeCollection($data) : null,
27 13
            array_key_exists('cod_countries', $data) ? $this->countryFactory->createCodCountryCollection($data) : null,
28 13
        );
29
    }
30
31
    /** @inheritDoc */
32 12
    public function createCollection(string $carrier, array $data): ServiceCollection
33
    {
34 12
        return new DefaultServiceCollection(
35 12
            $carrier,
36 12
            array_values(array_map(fn (array $service): Service => $this->create($carrier, $service), $data['service_types'] ?? [])),
37 12
            $data['active_parcel'] ?? null,
38 12
            $data['active_cargo'] ?? null,
39 12
        );
40
    }
41
42
    /**
43
     * @param array<string,mixed> $data
44
     */
45 3
    public function createOption(array $data): ServiceOption
46
    {
47 3
        return new DefaultServiceOption(
48 3
            $data['code'],
49 3
            $data['name'],
50 3
        );
51
    }
52
53
    /**
54
     * @param array<string,mixed> $data
55
     */
56 3
    private function createOptionCollection(array $data): ServiceOptionCollection
57
    {
58 3
        return new DefaultServiceOptionCollection(
59 3
            array_map(fn (array $data): ServiceOption => $this->createOption($data), $data['services'] ?? []),
60 3
        );
61
    }
62
}
63