Passed
Branch master (0d8fc3)
by Tomáš
12:26
created

DefaultServiceFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 48
ccs 20
cts 20
cp 1
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 4
A createCollection() 0 7 1
A createOptionCollection() 0 4 1
A createOption() 0 5 1
A __construct() 0 3 1
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
11
final class DefaultServiceFactory implements ServiceFactory
12
{
13 20
    public function __construct(
14
        private CountryFactory $countryFactory,
15
    ) {
16
    }
17
18
    /** @inheritDoc */
19 13
    public function create(string $carrier, array $data): Service
20
    {
21 13
        return new DefaultService(
22 13
            (string) $data['service_type'],
23 13
            $data['service_type_name'] ?? ($data['name'] ?? null),
24 13
            array_key_exists('services', $data) ? $this->createOptionCollection($carrier, $data) : null,
25 13
            array_key_exists('countries', $data) ? $this->countryFactory->createCodeCollection($data) : null,
26 13
            array_key_exists('cod_countries', $data) ? $this->countryFactory->createCodCountryCollection($data) : null,
27
        );
28
    }
29
30
    /** @inheritDoc */
31 12
    public function createCollection(string $carrier, array $data): ServiceCollection
32
    {
33 12
        return new DefaultServiceCollection(
34
            $carrier,
35 12
            array_map(fn(array $service): Service => $this->create($carrier, $service), $data['service_types'] ?? []),
36 12
            $data['active_parcel'] ?? null,
37 12
            $data['active_cargo'] ?? null,
38
        );
39
    }
40
41
    /**
42
     * @param array<string,mixed> $data
43
     */
44 3
    public function createOption(array $data): ServiceOption
45
    {
46 3
        return new DefaultServiceOption(
47 3
            $data['code'],
48 3
            $data['name'],
49
        );
50
    }
51
52
    /**
53
     * @param array<string,mixed> $data
54
     */
55 3
    private function createOptionCollection(string $carrier, array $data): ServiceOptionCollection
0 ignored issues
show
Unused Code introduced by
The parameter $carrier is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
    private function createOptionCollection(/** @scrutinizer ignore-unused */ string $carrier, array $data): ServiceOptionCollection

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57 3
        return new DefaultServiceOptionCollection(
58 3
            array_map(fn(array $data): ServiceOption => $this->createOption($data), $data['services'] ?? [])
59
        );
60
    }
61
}
62