Completed
Push — master ( 0512a1...7c8694 )
by Tomáš
03:46
created

Shipper::resolveAddRequestVersion()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.4888
cc 5
nc 3
nop 2
1
<?php
2
3
namespace Inspirum\Balikobot\Definitions;
4
5
use InvalidArgumentException;
6
7
final class Shipper
8
{
9
    /**
10
     * Česká pošta s.p.
11
     *
12
     * @var string
13
     */
14
    public const CP = 'cp';
15
16
    /**
17
     * Direct Parcel Distribution CZ s.r.o.
18
     *
19
     * @var string
20
     */
21
    public const DPD = 'dpd';
22
23
    /**
24
     * DHL Express
25
     *
26
     * @var string
27
     */
28
    public const DHL = 'dhl';
29
30
    /**
31
     * Geis CZ s.r.o.
32
     *
33
     * @var string
34
     */
35
    public const GEIS = 'geis';
36
37
    /**
38
     * General Logistics Systems Czech Republic s.r.o.
39
     *
40
     * @var string
41
     */
42
    public const GLS = 'gls';
43
44
    /**
45
     * IN TIME SPEDICE s.r.o.
46
     *
47
     * @var string
48
     */
49
    public const INTIME = 'intime';
50
51
    /**
52
     * Pošta bez hranic (Frogman s.r.o.)
53
     *
54
     * @var string
55
     */
56
    public const PBH = 'pbh';
57
58
    /**
59
     * PPL CZ s.r.o.
60
     *
61
     * @var string
62
     */
63
    public const PPL = 'ppl';
64
65
    /**
66
     * Slovenská pošta a.s.,
67
     *
68
     * @var string
69
     */
70
    public const SP = 'sp';
71
72
    /**
73
     * Slovak Parcel Service s.r.o.
74
     *
75
     * @var string
76
     */
77
    public const SPS = 'sps';
78
79
    /**
80
     * TOPTRANS EU a.s.
81
     *
82
     * @var string
83
     */
84
    public const TOPTRANS = 'toptrans';
85
86
    /**
87
     * Uloženka s.r.o.
88
     *
89
     * @var string
90
     */
91
    public const ULOZENKA = 'ulozenka';
92
93
    /**
94
     * UPS SCS Czech Republic s.r.o.
95
     *
96
     * @var string
97
     */
98
    public const UPS = 'ups';
99
100
    /**
101
     * Zásilkovna s.r.o.
102
     *
103
     * @var string
104
     */
105
    public const ZASILKOVNA = 'zasilkovna';
106
107
    /**
108
     * TNT
109
     *
110
     * @var string
111
     */
112
    public const TNT = 'tnt';
113
114
    /**
115
     * Gebrüder Weiss
116
     *
117
     * @var string
118
     */
119
    public const GW = 'gw';
120
121
    /**
122
     * Gebrüder Weiss Česká republika
123
     *
124
     * @var string
125
     */
126
    public const GWCZ = 'gwcz';
127
128
    /**
129
     * Messenger
130
     *
131
     * @var string
132
     */
133
    public const MESSENGER = 'messenger';
134
135
    /**
136
     * All supported shipper services.
137
     *
138
     * @return array<string>
139
     */
140
    public static function all(): array
141
    {
142
        return [
143
            self::CP,
144
            self::DHL,
145
            self::DPD,
146
            self::GEIS,
147
            self::GLS,
148
            self::INTIME,
149
            self::PBH,
150
            self::PPL,
151
            self::SP,
152
            self::SPS,
153
            self::TOPTRANS,
154
            self::ULOZENKA,
155
            self::UPS,
156
            self::ZASILKOVNA,
157
            self::TNT,
158
            self::GW,
159
            self::GWCZ,
160
            self::MESSENGER,
161
        ];
162
    }
163
164
    /**
165
     * Validate shipper code.
166
     *
167
     * @param string $code
168
     *
169
     * @return void
170
     *
171
     * @throws \InvalidArgumentException
172
     */
173
    public static function validateCode(string $code): void
174
    {
175
        if (in_array($code, self::all()) === false) {
176
            throw new InvalidArgumentException('Unknown shipper "' . $code . '".');
177
        }
178
    }
179
180
    /**
181
     * Determine if shipper service support full branch API
182
     *
183
     * @param string      $shipperCode
184
     * @param string|null $serviceCode
185
     *
186
     * @return bool
187
     */
188
    public static function hasFullBranchesSupport(string $shipperCode, ?string $serviceCode): bool
189
    {
190
        if ($shipperCode == Shipper::ZASILKOVNA) {
191
            return true;
192
        }
193
194
        if ($shipperCode == Shipper::CP && $serviceCode === ServiceType::CP_NP) {
195
            return true;
196
        }
197
198
        $services = [ServiceType::PBH_MP, ServiceType::PBH_FAN_KURIER];
199
        if ($shipperCode == Shipper::PBH && in_array($serviceCode, $services)) {
200
            return true;
201
        }
202
203
        return false;
204
    }
205
206
    /**
207
     * Resolve ADD request version
208
     *
209
     * @param string                     $shipperCode
210
     * @param array<array<string,mixed>> $packagesData
211
     *
212
     * @return string
213
     */
214
    public static function resolveAddRequestVersion(string $shipperCode, array $packagesData): string
215
    {
216
        $supportedShippers = [Shipper::ZASILKOVNA];
217
        if (in_array($shipperCode, $supportedShippers) && isset($packagesData[0][Option::SERVICE_TYPE])) {
218
            return API::V2;
219
        }
220
221
        $supportedShippers = [Shipper::UPS, Shipper::DHL, Shipper::TNT, Shipper::TOPTRANS];
222
        if (in_array($shipperCode, $supportedShippers) && isset($packagesData[0][Option::ORDER_NUMBER])) {
223
            return API::V2;
224
        }
225
226
        return API::V1;
227
    }
228
229
    /**
230
     * Resolve SERVICES request version
231
     *
232
     * @param string $shipperCode
233
     *
234
     * @return string
235
     */
236
    public static function resolveServicesRequestVersion(string $shipperCode): string
237
    {
238
        $supportedShippers = [Shipper::ZASILKOVNA];
239
240
        if (in_array($shipperCode, $supportedShippers)) {
241
            return API::V2;
242
        }
243
244
        return API::V1;
245
    }
246
247
    /**
248
     * Resolve BRANCHES request version
249
     *
250
     * @param string      $shipperCode
251
     * @param string|null $serviceCode
252
     *
253
     * @return string
254
     */
255
    public static function resolveBranchesRequestVersion(string $shipperCode, ?string $serviceCode): string
256
    {
257
        $supportedShippers = [Shipper::ZASILKOVNA];
258
259
        if (in_array($shipperCode, $supportedShippers) && $serviceCode !== null) {
260
            return API::V2;
261
        }
262
263
        return API::V1;
264
    }
265
266
    /**
267
     * Determine if shipper has support to filter branches by country code.
268
     *
269
     * @param string $shipperCode
270
     *
271
     * @return bool
272
     */
273
    public static function hasBranchCountryFilterSupport(string $shipperCode): bool
274
    {
275
        $supportedShippers = [
276
            Shipper::DPD,
277
            Shipper::GLS,
278
            Shipper::PPL,
279
        ];
280
281
        return in_array($shipperCode, $supportedShippers);
282
    }
283
}
284