Completed
Pull Request — master (#8)
by Tomáš
02:25
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
     * 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
     * All supported shipper services.
158
     *
159
     * @return array<string>
160
     */
161
    public static function all(): array
162
    {
163
        return [
164
            self::CP,
165
            self::DHL,
166
            self::DPD,
167
            self::GEIS,
168
            self::GLS,
169
            self::INTIME,
170
            self::PBH,
171
            self::PPL,
172
            self::SP,
173
            self::SPS,
174
            self::TOPTRANS,
175
            self::ULOZENKA,
176
            self::UPS,
177
            self::ZASILKOVNA,
178
            self::TNT,
179
            self::GW,
180
            self::GWCZ,
181
            self::MESSENGER,
182
            self::DHLDE,
183
            self::FEDEX,
184
            self::FOFR,
185
        ];
186
    }
187
188
    /**
189
     * Validate shipper code.
190
     *
191
     * @param string $code
192
     *
193
     * @return void
194
     *
195
     * @throws \InvalidArgumentException
196
     */
197
    public static function validateCode(string $code): void
198
    {
199
        if (in_array($code, self::all()) === false) {
200
            throw new InvalidArgumentException('Unknown shipper "' . $code . '".');
201
        }
202
    }
203
204
    /**
205
     * Determine if shipper service support full branch API
206
     *
207
     * @param string      $shipperCode
208
     * @param string|null $serviceCode
209
     *
210
     * @return bool
211
     */
212
    public static function hasFullBranchesSupport(string $shipperCode, ?string $serviceCode): bool
213
    {
214
        if ($shipperCode == Shipper::ZASILKOVNA) {
215
            return true;
216
        }
217
218
        if ($shipperCode == Shipper::CP && $serviceCode === ServiceType::CP_NP) {
219
            return true;
220
        }
221
222
        $services = [ServiceType::PBH_MP, ServiceType::PBH_FAN_KURIER];
223
        if ($shipperCode == Shipper::PBH && in_array($serviceCode, $services)) {
224
            return true;
225
        }
226
227
        return false;
228
    }
229
230
    /**
231
     * Determine if shipper has support to filter branches by country code.
232
     *
233
     * @param string $shipperCode
234
     *
235
     * @return bool
236
     */
237
    public static function hasBranchCountryFilterSupport(string $shipperCode): bool
238
    {
239
        $supportedShippers = [
240
            Shipper::PPL,
241
            Shipper::DPD,
242
            Shipper::GEIS,
243
            Shipper::GLS,
244
        ];
245
246
        return in_array($shipperCode, $supportedShippers);
247
    }
248
}
249