Passed
Push — master ( aa19b1...aa1d1b )
by Tomáš
10:29
created

DefaultBranchFactory::resolveStreet()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

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