DefaultZipCodeFactory::generate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
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 6
    public function create(string $carrier, ?string $service, array $data): ZipCode
13
    {
14 6
        return new DefaultZipCode(
15 6
            $carrier,
16 6
            $service,
17 6
            $data['zip'] ?? ($data['zip_start'] ?? null),
18 6
            $data['zip_end'] ?? null,
19 6
            $data['city'] ?? null,
20 6
            $data['region'] ?? null,
21 6
            $data['country'] ?? null,
22 6
            (bool) ($data['1B'] ?? false),
23 6
        );
24
    }
25
26
    /** @inheritDoc */
27 6
    public function createIterator(string $carrier, ?string $service, ?string $country, array $data): ZipCodeIterator
28
    {
29 6
        return new DefaultZipCodeIterator($carrier, $service, $this->generate($carrier, $service, $country, $data));
30
    }
31
32
    /**
33
     * @param array<string,mixed> $data
34
     *
35
     * @return \Generator<\Inspirum\Balikobot\Model\ZipCode\ZipCode>
36
     */
37 6
    private function generate(string $carrier, ?string $service, ?string $country, array $data): Generator
38
    {
39 6
        $country = $data['country'] ?? $country;
40
41 6
        foreach ($data['zip_codes'] ?? [] as $item) {
42 6
            $item['country'] ??= $country;
43
44 6
            yield $this->create($carrier, $service, $item);
45
        }
46
    }
47
}
48