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 218
    public function create(string $carrier, ?string $service, array $data): Branch
20
    {
21 218
        $data = $this->normalizeData($carrier, $service, $data);
22
23 218
        return new DefaultBranch(
24 218
            $carrier,
25 218
            $service,
26 218
            $data['branch_id'],
27 218
            $data['id'],
28 218
            $data['branch_uid'] ?? null,
29 218
            $data['type'],
30 218
            $data['name'],
31 218
            $data['city'],
32 218
            $data['street'],
33 218
            $data['zip'],
34 218
            $data['country'] ?? null,
35 218
            $data['city_part'] ?? null,
36 218
            $data['district'] ?? null,
37 218
            $data['region'] ?? null,
38 218
            $data['currency'] ?? null,
39 218
            $data['photo_small'] ?? null,
40 218
            $data['photo_big'] ?? null,
41 218
            $data['url'] ?? null,
42 218
            $data['latitude'] ?? null,
43 218
            $data['longitude'] ?? null,
44 218
            $data['directions_global'] ?? null,
45 218
            $data['directions_car'] ?? null,
46 218
            $data['directions_public'] ?? null,
47 218
            $data['wheelchair_accessible'] ?? null,
48 218
            $data['claim_assistant'] ?? null,
49 218
            $data['dressing_room'] ?? null,
50 218
            $data['opening_monday'] ?? null,
51 218
            $data['opening_tuesday'] ?? null,
52 218
            $data['opening_wednesday'] ?? null,
53 218
            $data['opening_thursday'] ?? null,
54 218
            $data['opening_friday'] ?? null,
55 218
            $data['opening_saturday'] ?? null,
56 218
            $data['opening_sunday'] ?? null,
57 218
            $data['max_weight'] ?? null,
58 218
        );
59
    }
60
61
    /** @inheritDoc */
62 211
    public function createIterator(string $carrier, ?string $service, ?array $countries, array $data): BranchIterator
63
    {
64 211
        return new DefaultBranchIterator($carrier, $service, $countries, $this->generate($carrier, $service, $data));
65
    }
66
67
    /** @inheritDoc */
68 106
    public function wrapIterator(?string $carrier, ?string $service, ?array $countries, Traversable $iterator): BranchIterator
69
    {
70 106
        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 211
    private function generate(string $carrier, ?string $service, array $data): Generator
79
    {
80 211
        foreach ($data['branches'] ?? [] as $branch) {
81 211
            yield $this->create($carrier, $service, $branch);
82
        }
83
    }
84
85
    /**
86
     * @param array<string,mixed> $data
87
     *
88
     * @return  array<string,mixed>
89
     */
90 218
    private function normalizeData(string $carrier, ?string $service, array $data): array
91
    {
92 218
        $data['country'] = $this->resolveCountry($carrier, $service, $data);
93 218
        $data['type'] ??= 'branch';
94 218
        $data['city'] ??= '';
95 218
        $data['zip'] ??= '00000';
96 218
        $data['street'] = $this->resolveStreet($data);
97 218
        $data['id'] = $data['branch_id'] ?? (isset($data['id']) ? (string) $data['id'] : null);
98 218
        $data['name'] ??= $data['zip'];
99 218
        $data['latitude'] = $this->castFloat($data, 'latitude');
100 218
        $data['longitude'] = $this->castFloat($data, 'longitude');
101 218
        $data['wheelchair_accessible'] = $this->castBool($data, 'wheelchair_accessible');
102 218
        $data['claim_assistant'] = $this->castBool($data, 'claim_assistant');
103 218
        $data['dressing_room'] = $this->castBool($data, 'dressing_room');
104 218
        $data['max_weight'] = $this->castFloat($data, 'max_weight');
105 218
        $data['branch_id'] = $this->resolveBranchId($carrier, $service, $data);
106
107 218
        return $data;
108
    }
109
110
    /**
111
     * @param array<string,mixed> $data
112
     */
113 218
    private function castBool(array $data, string $key): ?bool
114
    {
115 218
        return isset($data[$key]) ? (bool) $data[$key] : null;
116
    }
117
118
    /**
119
     * @param array<string,mixed> $data
120
     */
121 218
    private function castFloat(array $data, string $key): ?float
122
    {
123 218
        return isset($data[$key]) ? (float) trim((string) $data[$key]) : null;
124
    }
125
126
    /**
127
     * @param array<string,mixed> $data
128
     */
129 218
    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 218
            $carrier === Carrier::CP
134 210
            || $carrier === Carrier::SP
135 218
            || ($carrier === Carrier::ULOZENKA && $service === Service::ULOZENKA_CP_NP)
0 ignored issues
show
Deprecated Code introduced by
The constant Inspirum\Balikobot\Definitions\Carrier::ULOZENKA has been deprecated: Replaced by One by Allegro ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

135
            || ($carrier === /** @scrutinizer ignore-deprecated */ Carrier::ULOZENKA && $service === Service::ULOZENKA_CP_NP)

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
Deprecated Code introduced by
The constant Inspirum\Balikobot\Defin...Service::ULOZENKA_CP_NP has been deprecated: Renamed to ONEBYALLEGRO ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

135
            || ($carrier === Carrier::ULOZENKA && $service === /** @scrutinizer ignore-deprecated */ Service::ULOZENKA_CP_NP)

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
136
        ) {
137 17
            return str_replace(' ', '', $data['zip']);
138
        }
139
140 202
        if ($carrier === Carrier::PPL) {
141 11
            return (string) preg_replace('/^KM/', '', (string) $data['id']);
142
        }
143
144 192
        if ($carrier === Carrier::INTIME) {
145 5
            return $data['name'];
146
        }
147
148 188
        return (string) $data['id'];
149
    }
150
151
    /**
152
     * @param array<string,mixed> $data
153
     */
154 218
    private function resolveCountry(string $carrier, ?string $service, array $data): ?string
155
    {
156 218
        if ($carrier === Carrier::CP && $service === Service::CP_NP) {
157 5
            $data['country'] ??= 'CZ';
158
        }
159
160 218
        return $data['country'] ?? null;
161
    }
162
163
    /**
164
     * @param array<string,mixed> $data
165
     */
166 218
    private function resolveStreet(array $data): string
167
    {
168 218
        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 218
        $data['street'] ??= $data['address'] ?? '';
184
185 218
        return $data['street'];
186
    }
187
}
188