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

DefaultZipCodeFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 35
ccs 14
cts 14
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 8 2
A create() 0 10 1
A createIterator() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Balikobot\Model\ZipCode;
6
7
use Generator;
8
9
final class DefaultZipCodeFactory implements ZipCodeFactory
10
{
11
    /** @inheritDoc */
12 5
    public function create(string $carrier, ?string $service, array $data): ZipCode
13
    {
14 5
        return new DefaultZipCode(
15
            $carrier,
16
            $service,
17 5
            $data['zip'] ?? ($data['zip_start'] ?? null),
18 5
            $data['zip_end'] ?? null,
19 5
            $data['city'] ?? null,
20 5
            $data['country'] ?? null,
21 5
            (bool) ($data['1B'] ?? false),
22
        );
23
    }
24
25
    /** @inheritDoc */
26 5
    public function createIterator(string $carrier, ?string $service, ?string $country, array $data): ZipCodeIterator
27
    {
28 5
        return new DefaultZipCodeIterator($carrier, $service, $this->generate($carrier, $service, $country, $data));
29
    }
30
31
    /**
32
     * @param array<string,mixed> $data
33
     *
34
     * @return \Generator<\Inspirum\Balikobot\Model\ZipCode\ZipCode>
35
     */
36 5
    private function generate(string $carrier, ?string $service, ?string $country, array $data): Generator
37
    {
38 5
        $country = $data['country'] ?? $country;
39
40 5
        foreach ($data['zip_codes'] ?? [] as $item) {
41 5
            $item['country'] ??= $country;
42
43 5
            yield $this->create($carrier, $service, $item);
44
        }
45
    }
46
}
47