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

DefaultZipCodeFactory::createIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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