DefaultBranchFactory::resolveBranchId()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 10
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 20
ccs 10
cts 10
cp 1
crap 7
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Balikobot\Model\Branch;
6
7
use Generator;
8
use Inspirum\Balikobot\Definitions\Carrier;
9
use Inspirum\Balikobot\Definitions\Service;
10
use Traversable;
11
use function preg_replace;
12
use function sprintf;
13
use function str_replace;
14
use function trim;
15
16
final class DefaultBranchFactory implements BranchFactory
17
{
18
    /** @inheritDoc */
19 224
    public function create(string $carrier, ?string $service, array $data): Branch
20
    {
21 224
        $data = $this->normalizeData($carrier, $service, $data);
22
23 224
        return new DefaultBranch(
24 224
            $carrier,
25 224
            $service,
26 224
            $data['branch_id'],
27 224
            $data['id'],
28 224
            $data['branch_uid'] ?? null,
29 224
            $data['type'],
30 224
            $data['name'],
31 224
            $data['city'],
32 224
            $data['street'],
33 224
            $data['zip'],
34 224
            $data['country'] ?? null,
35 224
            $data['city_part'] ?? null,
36 224
            $data['district'] ?? null,
37 224
            $data['region'] ?? null,
38 224
            $data['currency'] ?? null,
39 224
            $data['photo_small'] ?? null,
40 224
            $data['photo_big'] ?? null,
41 224
            $data['url'] ?? null,
42 224
            $data['latitude'] ?? null,
43 224
            $data['longitude'] ?? null,
44 224
            $data['directions_global'] ?? null,
45 224
            $data['directions_car'] ?? null,
46 224
            $data['directions_public'] ?? null,
47 224
            $data['wheelchair_accessible'] ?? null,
48 224
            $data['claim_assistant'] ?? null,
49 224
            $data['dressing_room'] ?? null,
50 224
            $data['opening_monday'] ?? null,
51 224
            $data['opening_tuesday'] ?? null,
52 224
            $data['opening_wednesday'] ?? null,
53 224
            $data['opening_thursday'] ?? null,
54 224
            $data['opening_friday'] ?? null,
55 224
            $data['opening_saturday'] ?? null,
56 224
            $data['opening_sunday'] ?? null,
57 224
            $data['max_weight'] ?? null,
58 224
        );
59
    }
60
61
    /** @inheritDoc */
62 217
    public function createIterator(string $carrier, ?string $service, ?array $countries, array $data): BranchIterator
63
    {
64 217
        return new DefaultBranchIterator($carrier, $service, $countries, $this->generate($carrier, $service, $data));
65
    }
66
67
    /** @inheritDoc */
68 108
    public function wrapIterator(?string $carrier, ?string $service, ?array $countries, Traversable $iterator): BranchIterator
69
    {
70 108
        return new DefaultBranchIterator($carrier, $service, $countries, $iterator);
71
    }
72
73
    /**
74
     * @param array<string,mixed> $data
75
     *
76
     * @return \Generator<int,\Inspirum\Balikobot\Model\Branch\Branch>
77
     */
78 217
    private function generate(string $carrier, ?string $service, array $data): Generator
79
    {
80 217
        foreach ($data['branches'] ?? [] as $branch) {
81 217
            yield $this->create($carrier, $service, $branch);
82
        }
83
    }
84
85
    /**
86
     * @param array<string,mixed> $data
87
     *
88
     * @return  array<string,mixed>
89
     */
90 224
    private function normalizeData(string $carrier, ?string $service, array $data): array
91
    {
92 224
        $data['country'] = $this->resolveCountry($carrier, $service, $data);
93 224
        $data['type'] ??= 'branch';
94 224
        $data['city'] ??= '';
95 224
        $data['zip'] ??= '00000';
96 224
        $data['street'] = $this->resolveStreet($data);
97 224
        $data['id'] = $data['branch_id'] ?? (isset($data['id']) ? (string) $data['id'] : null);
98 224
        $data['name'] ??= $data['zip'];
99 224
        $data['latitude'] = $this->castFloat($data, 'latitude');
100 224
        $data['longitude'] = $this->castFloat($data, 'longitude');
101 224
        $data['wheelchair_accessible'] = $this->castBool($data, 'wheelchair_accessible');
102 224
        $data['claim_assistant'] = $this->castBool($data, 'claim_assistant');
103 224
        $data['dressing_room'] = $this->castBool($data, 'dressing_room');
104 224
        $data['max_weight'] = $this->castFloat($data, 'max_weight');
105 224
        $data['branch_id'] = $this->resolveBranchId($carrier, $service, $data);
106
107 224
        return $data;
108
    }
109
110
    /**
111
     * @param array<string,mixed> $data
112
     */
113 224
    private function castBool(array $data, string $key): ?bool
114
    {
115 224
        return isset($data[$key]) ? (bool) $data[$key] : null;
116
    }
117
118
    /**
119
     * @param array<string,mixed> $data
120
     */
121 224
    private function castFloat(array $data, string $key): ?float
122
    {
123 224
        return isset($data[$key]) ? (float) trim((string) $data[$key]) : null;
124
    }
125
126
    /**
127
     * @param array<string,mixed> $data
128
     */
129 224
    private function resolveBranchId(string $carrier, ?string $service, array $data): string
130
    {
131
        // get key used in branch_id when calling add request
132
        if (
133 224
            $carrier === Carrier::CP
134 216
            || $carrier === Carrier::SP
135 224
            || ($carrier === Carrier::ULOZENKA && $service === Service::ULOZENKA_CP_NP)
136
        ) {
137 19
            return str_replace(' ', '', $data['zip']);
138
        }
139
140 206
        if ($carrier === Carrier::PPL) {
141 11
            return (string) preg_replace('/^KM/', '', (string) $data['id']);
142
        }
143
144 196
        if ($carrier === Carrier::INTIME) {
145 5
            return $data['name'];
146
        }
147
148 192
        return (string) $data['id'];
149
    }
150
151
    /**
152
     * @param array<string,mixed> $data
153
     */
154 224
    private function resolveCountry(string $carrier, ?string $service, array $data): ?string
155
    {
156 224
        if ($carrier === Carrier::CP && $service === Service::CP_NP) {
157 5
            $data['country'] ??= 'CZ';
158
        }
159
160 224
        return $data['country'] ?? null;
161
    }
162
163
    /**
164
     * @param array<string,mixed> $data
165
     */
166 224
    private function resolveStreet(array $data): string
167
    {
168 224
        if (isset($data['street']) && (isset($data['house_number']) || isset($data['orientation_number']))) {
169 1
            $houseNumber = (int) ($data['house_number'] ?? 0);
170 1
            $orientationNumber = (int) ($data['orientation_number'] ?? 0);
171 1
            $streetNumber = trim(
172 1
                sprintf(
173 1
                    '%s/%s',
174 1
                    $houseNumber > 0 ? $houseNumber : '',
175 1
                    $orientationNumber > 0 ? $orientationNumber : '',
176 1
                ),
177 1
                '/',
178 1
            );
179
180 1
            $data['street'] = trim(sprintf('%s %s', $data['street'] ?: ($data['city'] ?? ''), $streetNumber));
181
        }
182
183 224
        $data['street'] ??= $data['address'] ?? '';
184
185 224
        return $data['street'];
186
    }
187
}
188