Completed
Push — master ( 047014...cb17ac )
by Tomáš
04:02
created

Shipper::resolveAddRequestVersion()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.9332
cc 3
nc 2
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
     * DHL Parcel Slovensko
32
     *
33
     * @var string
34
     */
35
    public const DHLSK = 'dhlsk';
36
37
    /**
38
     * Geis CZ s.r.o.
39
     *
40
     * @var string
41
     */
42
    public const GEIS = 'geis';
43
44
    /**
45
     * General Logistics Systems Czech Republic s.r.o.
46
     *
47
     * @var string
48
     */
49
    public const GLS = 'gls';
50
51
    /**
52
     * IN TIME SPEDICE s.r.o.
53
     *
54
     * @var string
55
     */
56
    public const INTIME = 'intime';
57
58
    /**
59
     * Pošta bez hranic (Frogman s.r.o.)
60
     *
61
     * @var string
62
     */
63
    public const PBH = 'pbh';
64
65
    /**
66
     * PPL CZ s.r.o.
67
     *
68
     * @var string
69
     */
70
    public const PPL = 'ppl';
71
72
    /**
73
     * Slovenská pošta a.s.,
74
     *
75
     * @var string
76
     */
77
    public const SP = 'sp';
78
79
    /**
80
     * Slovak Parcel Service s.r.o.
81
     *
82
     * @var string
83
     */
84
    public const SPS = 'sps';
85
86
    /**
87
     * TOPTRANS EU a.s.
88
     *
89
     * @var string
90
     */
91
    public const TOP_TRANS = 'toptrans';
92
93
    /**
94
     * Uloženka s.r.o.
95
     *
96
     * @var string
97
     */
98
    public const ULOZENKA = 'ulozenka';
99
100
    /**
101
     * UPS SCS Czech Republic s.r.o.
102
     *
103
     * @var string
104
     */
105
    public const UPS = 'ups';
106
107
    /**
108
     * Zásilkovna s.r.o.
109
     *
110
     * @var string
111
     */
112
    public const ZASILKOVNA = 'zasilkovna';
113
114
    /**
115
     * TNT
116
     *
117
     * @var string
118
     */
119
    public const TNT = 'tnt';
120
121
    /**
122
     * Gebrüder Weiss
123
     *
124
     * @var string
125
     */
126
    public const GW = 'gw';
127
128
    /**
129
     * All supported shipper services.
130
     *
131
     * @return array
132
     */
133
    public static function all(): array
134
    {
135
        return [
136
            self::CP,
137
            self::DHL,
138
            self::DHLSK,
139
            self::DPD,
140
            self::GEIS,
141
            self::GLS,
142
            self::INTIME,
143
            self::PBH,
144
            self::PPL,
145
            self::SP,
146
            self::SPS,
147
            self::TOP_TRANS,
148
            self::ULOZENKA,
149
            self::UPS,
150
            self::ZASILKOVNA,
151
            self::TNT,
152
            self::GW,
153
        ];
154
    }
155
156
    /**
157
     * Validate shipper code.
158
     *
159
     * @param string $code
160
     *
161
     * @return void
162
     *
163
     * @throws \InvalidArgumentException
164
     */
165
    public static function validateCode(string $code): void
166
    {
167
        if (in_array($code, self::all()) === false) {
168
            throw new InvalidArgumentException('Unknown shipper "' . $code . '".');
169
        }
170
    }
171
172
    /**
173
     * Determine if shipper service support full branch API
174
     *
175
     * @param string      $shipperCode
176
     * @param string|null $serviceCode
177
     *
178
     * @return bool
179
     */
180
    public static function hasFullBranchesSupport(string $shipperCode, ?string $serviceCode): bool
181
    {
182
        if ($shipperCode == Shipper::ZASILKOVNA) {
183
            return true;
184
        }
185
186
        if ($shipperCode == Shipper::CP && $serviceCode === ServiceType::CP_NP) {
187
            return true;
188
        }
189
190
        $services = [ServiceType::PBH_MP, ServiceType::PBH_FAN_KURIER];
191
        if ($shipperCode == Shipper::PBH && in_array($serviceCode, $services)) {
192
            return true;
193
        }
194
195
        return false;
196
    }
197
198
    /**
199
     * Resolve ADD request version
200
     *
201
     * @param string $shipperCode
202
     * @param array  $data
203
     *
204
     * @return string
205
     */
206
    public static function resolveAddRequestVersion(string $shipperCode, array $data): string
207
    {
208
        $supportedShippers = [Shipper::UPS, Shipper::DHL, Shipper::TNT];
209
210
        if (in_array($shipperCode, $supportedShippers) && isset($data[0]['order_number'])) {
211
            return API::V2;
212
        }
213
214
        return API::V1;
215
    }
216
}
217