Passed
Branch master (837a03)
by Tomáš
02:48
created

Shipper::hasFullBranchesSupport()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 16
rs 9.2222
1
<?php
2
3
namespace Inspirum\Balikobot\Definitions;
4
5
use InvalidArgumentException;
6
7
final class Shipper
8
{
9
    /**
10
     * Česká pošta
11
     *
12
     * @var string
13
     */
14
    public const CP = 'cp';
15
16
    /**
17
     * DPD
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 Cargo - paletová přeprava
32
     *
33
     * @var string
34
     */
35
    public const GEIS = 'geis';
36
37
    /**
38
     * GLS
39
     *
40
     * @var string
41
     */
42
    public const GLS = 'gls';
43
44
    /**
45
     * WE DO
46
     *
47
     * @var string
48
     */
49
    public const INTIME = 'intime';
50
51
    /**
52
     * Pošta bez hranic
53
     *
54
     * @var string
55
     */
56
    public const PBH = 'pbh';
57
58
    /**
59
     * PPL + DHL Freight
60
     *
61
     * @var string
62
     */
63
    public const PPL = 'ppl';
64
65
    /**
66
     * Slovenská pošta
67
     *
68
     * @var string
69
     */
70
    public const SP = 'sp';
71
72
    /**
73
     * Slovak Parcel Service
74
     *
75
     * @var string
76
     */
77
    public const SPS = 'sps';
78
79
    /**
80
     * Toptrans
81
     *
82
     * @var string
83
     */
84
    public const TOPTRANS = 'toptrans';
85
86
    /**
87
     * WE DO - Uloženka
88
     *
89
     * @var string
90
     */
91
    public const ULOZENKA = 'ulozenka';
92
93
    /**
94
     * UPS
95
     *
96
     * @var string
97
     */
98
    public const UPS = 'ups';
99
100
    /**
101
     * Zásilkovna
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 Slovensko
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
     * DHL DE
137
     *
138
     * @var string
139
     */
140
    public const DHLDE = 'dhlde';
141
142
    /**
143
     * FedEx
144
     *
145
     * @var string
146
     */
147
    public const FEDEX = 'fedex';
148
149
    /**
150
     * Fofr
151
     *
152
     * @var string
153
     */
154
    public const FOFR = 'fofr';
155
156
    /**
157
     * Dachser
158
     *
159
     * @var string
160
     */
161
    public const DACHSER = 'dachser';
162
163
    /**
164
     * DHL Parcel Europe - PPL Parcel Connect EU
165
     *
166
     * @var string
167
     */
168
    public const DHLPARCEL = 'dhlparcel';
169
170
    /**
171
     * Raben
172
     *
173
     * @var string
174
     */
175
    public const RABEN = 'raben';
176
177
    /**
178
     * Spring
179
     *
180
     * @var string
181
     */
182
    public const SPRING = 'spring';
183
184
    /**
185
     * Spring
186
     *
187
     * @var string
188
     */
189
    public const DSV = 'dsv';
190
191
    /**
192
     * DHL Freight Euroconnect
193
     *
194
     * @var string
195
     */
196
    public const DHLFREIGHTEC = 'dhlfreightec';
197
198
    /**
199
     * All supported shipper services.
200
     *
201
     * @return array<string>
202
     */
203
    public static function all(): array
204
    {
205
        return [
206
            self::CP,
207
            self::DHL,
208
            self::DPD,
209
            self::GEIS,
210
            self::GLS,
211
            self::INTIME,
212
            self::PBH,
213
            self::PPL,
214
            self::SP,
215
            self::SPS,
216
            self::TOPTRANS,
217
            self::ULOZENKA,
218
            self::UPS,
219
            self::ZASILKOVNA,
220
            self::TNT,
221
            self::GW,
222
            self::GWCZ,
223
            self::MESSENGER,
224
            self::DHLDE,
225
            self::FEDEX,
226
            self::FOFR,
227
            self::DACHSER,
228
            self::DHLPARCEL,
229
            self::RABEN,
230
            self::SPRING,
231
            self::DSV,
232
            self::DHLFREIGHTEC,
233
        ];
234
    }
235
236
    /**
237
     * Validate shipper code.
238
     *
239
     * @param string $code
240
     *
241
     * @return void
242
     *
243
     * @throws \InvalidArgumentException
244
     */
245
    public static function validateCode(string $code): void
246
    {
247
        if (in_array($code, self::all()) === false) {
248
            throw new InvalidArgumentException('Unknown shipper "' . $code . '".');
249
        }
250
    }
251
252
    /**
253
     * Determine if shipper service support full branch API
254
     *
255
     * @param string      $shipperCode
256
     * @param string|null $serviceCode
257
     *
258
     * @return bool
259
     */
260
    public static function hasFullBranchesSupport(string $shipperCode, ?string $serviceCode): bool
261
    {
262
        if ($shipperCode == Shipper::ZASILKOVNA) {
263
            return true;
264
        }
265
266
        if ($shipperCode == Shipper::CP && $serviceCode === ServiceType::CP_NP) {
267
            return true;
268
        }
269
270
        $services = [ServiceType::PBH_MP, ServiceType::PBH_FAN_KURIER];
271
        if ($shipperCode == Shipper::PBH && in_array($serviceCode, $services)) {
272
            return true;
273
        }
274
275
        return false;
276
    }
277
278
    /**
279
     * Determine if shipper has support to filter branches by country code.
280
     *
281
     * @param string      $shipperCode
282
     * @param string|null $serviceCode
283
     *
284
     * @return bool
285
     */
286
    public static function hasBranchCountryFilterSupport(string $shipperCode, ?string $serviceCode): bool
287
    {
288
        if ($serviceCode === null) {
289
            return true;
290
        }
291
292
        $supportedShippers = [
293
            Shipper::PPL,
294
            Shipper::DPD,
295
            Shipper::GEIS,
296
            Shipper::GLS,
297
        ];
298
299
        return in_array($shipperCode, $supportedShippers);
300
    }
301
}
302