Completed
Pull Request — master (#8)
by Tomáš
02:25
created

Formatter::normalizeResponseItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.9332
ccs 5
cts 5
cp 1
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
namespace Inspirum\Balikobot\Services;
4
5
class Formatter
6
{
7
    /**
8
     * Response validator
9
     *
10
     * @var \Inspirum\Balikobot\Services\Validator
11
     */
12
    private $validator;
13
14
    /**
15
     * Formatter constructor.
16
     *
17
     * @param \Inspirum\Balikobot\Services\Validator $validator
18
     */
19 322
    public function __construct(Validator $validator)
20
    {
21 322
        $this->validator = $validator;
22 322
    }
23
24
    /**
25
     * Normalize "zipcodes" request response
26
     *
27
     * @param array<mixed,mixed> $response
28
     * @param string|null        $country
29
     *
30
     * @return array<array<string,mixed>>
31
     */
32 10
    public function normalizePostCodesResponse(array $response, string $country = null): array
33
    {
34 10
        $country = $response['country'] ?? $country;
35
36 10
        $formattedResponse = [];
37
38 10
        foreach ($response['zip_codes'] ?? [] as $responseItem) {
39 6
            $formattedResponse[] = [
40 6
                'postcode'     => $responseItem['zip'] ?? ($responseItem['zip_start'] ?? null),
41 6
                'postcode_end' => $responseItem['zip_end'] ?? null,
42 6
                'city'         => $responseItem['city'] ?? null,
43 6
                'country'      => $responseItem['country'] ?? $country,
44 6
                '1B'           => (bool) ($responseItem['1B'] ?? false),
45
            ];
46
        }
47
48 10
        return $formattedResponse;
49
    }
50
51
    /**
52
     * Normalize "trackstatus" request response
53
     *
54
     * @param array<mixed,mixed> $response
55
     *
56
     * @return array<array<string,float|string|null>>
57
     *
58
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
59
     */
60 13
    public function normalizeTrackPackagesLastStatusResponse(array $response): array
61
    {
62 13
        $formattedResponse = [];
63
64 13
        foreach ($response as $responseItem) {
65 13
            $this->validator->validateResponseStatus($responseItem, $response);
66
67 12
            $formattedResponse[] = [
68 12
                'name'          => $responseItem['status_text'],
69 12
                'name_internal' => $responseItem['status_text'],
70 12
                'type'          => 'event',
71 12
                'status_id'     => (float) $responseItem['status_id'],
72
                'date'          => null,
73
            ];
74
        }
75
76 11
        return $formattedResponse;
77
    }
78
79
    /**
80
     * Normalize "track" request response
81
     *
82
     * @param array<mixed,mixed> $response
83
     *
84
     * @return array<array<array<string,float|string>>>
85
     *
86
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
87
     */
88 13
    public function normalizeTrackPackagesResponse(array $response): array
89
    {
90 13
        $formattedResponse = [];
91
92 13
        foreach ($response ?? [] as $i => $responseItems) {
93 13
            $this->validator->validateResponseStatus($responseItems, $response);
94
95 12
            $formattedResponse[$i] = [];
96
97 12
            foreach ($responseItems['states'] ?? [] as $responseItem) {
98 12
                $formattedResponse[$i][] = [
99 12
                    'date'          => $responseItem['date'],
100 12
                    'name'          => $responseItem['name'],
101 12
                    'status_id'     => (float) ($responseItem['status_id_v2'] ?? $responseItem['status_id']),
102 12
                    'type'          => $responseItem['type'] ?? 'event',
103 12
                    'name_internal' => $responseItem['name_balikobot'] ?? $responseItem['name'],
104
                ];
105
            }
106
        }
107
108 11
        return $formattedResponse;
109
    }
110
111
    /**
112
     * Normalize "pod" request response
113
     *
114
     * @param array<mixed,mixed> $response
115
     *
116
     * @return array<string>
117
     *
118
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
119
     */
120 13
    public function normalizeProofOfDeliveriesResponse(array $response): array
121
    {
122 13
        $formattedResponse = [];
123
124 13
        foreach ($response as $responseItem) {
125 13
            $this->validator->validateResponseStatus($responseItem, $response);
126
127 12
            $formattedResponse[] = $responseItem['file_url'];
128
        }
129
130 11
        return $formattedResponse;
131
    }
132
133
    /**
134
     * Unset "status" attribute
135
     *
136
     * @param array<mixed,mixed> $response
137
     *
138
     * @return array<mixed,mixed>
139
     */
140 43
    public function withoutStatus(array $response): array
141
    {
142 43
        unset($response['status']);
143
144 43
        return $response;
145
    }
146
147
    /**
148
     * Normalize response items
149
     *
150
     * @param array<array<string,string>> $items
151
     * @param string                      $keyName
152
     * @param string|null                 $valueName
153
     *
154
     * @return array<string,mixed>
155
     */
156 54
    public static function normalizeResponseItems(array $items, string $keyName, ?string $valueName): array
157
    {
158 54
        $formattedItems = [];
159
160 54
        foreach ($items as $item) {
161 30
            $formattedItems[$item[$keyName]] = $valueName !== null ? $item[$valueName] : $item;
162
        }
163
164 54
        return $formattedItems;
165
    }
166
167
    /**
168
     * Encapsulate ids with key
169
     *
170
     * @param array<int|string> $ids
171
     * @param string            $keyName
172
     *
173
     * @return array<array<int|string>>
174
     */
175 21
    public function encapsulateIds(array $ids, string $keyName): array
176
    {
177 21
        $formattedItems = [];
178
179 21
        foreach ($ids as $id) {
180 21
            $formattedItems[] = [$keyName => $id];
181
        }
182
183 21
        return $formattedItems;
184
    }
185
}
186