Completed
Push — master ( 6b9952...b1578e )
by Tomáš
02:25
created

Client::getActivatedServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
ccs 4
cts 4
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Inspirum\Balikobot\Services;
4
5
use DateTime;
6
use Inspirum\Balikobot\Contracts\RequesterInterface;
7
use Inspirum\Balikobot\Definitions\Country;
8
use Inspirum\Balikobot\Definitions\Request;
9
use Inspirum\Balikobot\Exceptions\BadRequestException;
10
11
class Client
12
{
13
    /**
14
     * API requester.
15
     *
16
     * @var \Inspirum\Balikobot\Contracts\RequesterInterface
17
     */
18
    private $requester;
19
20
    /**
21
     * Balikobot API client.
22
     *
23
     * @param \Inspirum\Balikobot\Contracts\RequesterInterface $requester
24
     */
25 164
    public function __construct(RequesterInterface $requester)
26
    {
27 164
        $this->requester = $requester;
28 164
    }
29
30
    /**
31
     * Add package(s) to the Balikobot.
32
     *
33
     * @param string $shipper
34
     * @param array  $packages
35
     *
36
     * @return array[]
37
     *
38
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
39
     */
40 15
    public function addPackages(string $shipper, array $packages): array
41
    {
42 15
        $response = $this->requester->call('v1', $shipper, Request::ADD, $packages);
43
44 6
        if (isset($response[0]['package_id']) === false) {
45 1
            throw new BadRequestException($response);
46
        }
47
48 5
        unset($response['labels_url']);
49 5
        unset($response['status']);
50
51 5
        return $response;
52
    }
53
54
    /**
55
     * Drops a package from the Balikobot. The package must be not ordered.
56
     *
57
     * @param string $shipper
58
     * @param int    $packageId
59
     *
60
     * @return void
61
     *
62
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
63
     */
64 2
    public function dropPackage(string $shipper, int $packageId): void
65
    {
66 2
        $this->dropPackages($shipper, [$packageId]);
67 2
    }
68
69
    /**
70
     * Drops a package from the Balikobot. The package must be not ordered.
71
     *
72
     * @param string $shipper
73
     * @param array  $packageIds
74
     *
75
     * @return void
76
     *
77
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
78
     */
79 7
    public function dropPackages(string $shipper, array $packageIds): void
80
    {
81 7
        $data = [];
82
83 7
        foreach ($packageIds as $packageId) {
84 6
            $data[] = ['id' => $packageId];
85
        }
86
87 7
        if (count($data) === 0) {
88 1
            return;
89
        }
90
91 6
        $this->requester->call('v1', $shipper, Request::DROP, $data);
92 3
    }
93
94
    /**
95
     * Tracks a package
96
     *
97
     * @param string $shipper
98
     * @param string $carrierId
99
     *
100
     * @return array[]
101
     *
102
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
103
     */
104 8
    public function trackPackage(string $shipper, string $carrierId): array
105
    {
106
        $data = [
107
            0 => [
108 8
                'id' => $carrierId,
109
            ],
110
        ];
111
112 8
        $response = $this->requester->call('v2', $shipper, Request::TRACK, $data, false);
113
114 6
        if (empty($response[0])) {
115 1
            throw new BadRequestException($response);
116
        }
117
118 5
        return $response[0];
119
    }
120
121
    /**
122
     * Tracks a package, get the last info
123
     *
124
     * @param string $shipper
125
     * @param string $carrierId
126
     *
127
     * @return array
128
     *
129
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
130
     */
131 9
    public function trackPackageLastStatus(string $shipper, string $carrierId): array
132
    {
133
        $data = [
134
            0 => [
135 9
                'id' => $carrierId,
136
            ],
137
        ];
138
139 9
        $response = $this->requester->call('v1', $shipper, Request::TRACK_STATUS, $data, false);
140
141 7
        if (empty($response[0])) {
142 1
            throw new BadRequestException($response);
143
        }
144
145 6
        if (isset($response[0]['status']) && ((int) $response[0]['status']) !== 200) {
146 1
            throw new BadRequestException($response);
147
        }
148
149
        $status = [
150 5
            'name'      => $response[0]['status_text'],
151 5
            'status_id' => $response[0]['status_id'],
152
            'date'      => null,
153
        ];
154
155 5
        return $status;
156
    }
157
158
    /**
159
     * Returns packages from the front (not ordered) for given shipper
160
     *
161
     * @param string $shipper
162
     *
163
     * @return array[]
164
     *
165
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
166
     */
167 6
    public function getOverview(string $shipper): array
168
    {
169 6
        $response = $this->requester->call('v1', $shipper, Request::OVERVIEW, [], false);
170
171 4
        return $response;
172
    }
173
174
    /**
175
     * Gets labels
176
     *
177
     * @param string $shipper
178
     * @param array  $packageIds
179
     *
180
     * @return string
181
     *
182
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
183
     */
184 7
    public function getLabels(string $shipper, array $packageIds): string
185
    {
186
        $data = [
187 7
            'package_ids' => $packageIds,
188
        ];
189
190 7
        $response = $this->requester->call('v1', $shipper, Request::LABELS, $data);
191
192 4
        return $response['labels_url'];
193
    }
194
195
    /**
196
     * Gets complete information about a package
197
     *
198
     * @param string $shipper
199
     * @param int    $packageId
200
     *
201
     * @return array
202
     *
203
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
204
     */
205 6
    public function getPackageInfo(string $shipper, int $packageId): array
206
    {
207 6
        $response = $this->requester->call('v1', $shipper, Request::PACKAGE . '/' . $packageId, [], false);
208
209 4
        return $response;
210
    }
211
212
    /**
213
     * Order shipment for packages.
214
     *
215
     * @param string         $shipper
216
     * @param array          $packageIds
217
     * @param \DateTime|null $date
218
     * @param string|null    $note
219
     *
220
     * @return array
221
     *
222
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
223
     */
224 7
    public function orderShipment(string $shipper, array $packageIds, DateTime $date = null, string $note = null): array
225
    {
226
        $data = [
227 7
            'package_ids' => $packageIds,
228 7
            'date'        => $date ? $date->format('Y-m-d') : null,
229 7
            'note'        => $note,
230
        ];
231
232 7
        $response = $this->requester->call('v1', $shipper, Request::ORDER, $data);
233
234 4
        unset($response['status']);
235
236 4
        return $response;
237
    }
238
239
    /**
240
     * Get order details.
241
     *
242
     * @param string $shipper
243
     * @param int    $orderId
244
     *
245
     * @return array
246
     *
247
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
248
     */
249 7
    public function getOrder(string $shipper, int $orderId): array
250
    {
251 7
        $response = $this->requester->call('v1', $shipper, Request::ORDER_VIEW . '/' . $orderId, [], false);
252
253 5
        unset($response['status']);
254
255 5
        return $response;
256
    }
257
258
    /**
259
     * Order pickup for packages.
260
     *
261
     * @param string      $shipper
262
     * @param \DateTime   $dateFrom
263
     * @param \DateTime   $dateTo
264
     * @param float       $weight
265
     * @param int         $packageCount
266
     * @param string|null $message
267
     *
268
     * @return void
269
     *
270
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
271
     */
272 4
    public function orderPickup(
273
        string $shipper,
274
        DateTime $dateFrom,
275
        DateTime $dateTo,
276
        float $weight,
277
        int $packageCount,
278
        string $message = null
279
    ): void {
280
        $data = [
281 4
            'date'          => $dateFrom->format('Y-m-d'),
282 4
            'time_from'     => $dateFrom->format('H:s'),
283 4
            'time_to'       => $dateTo->format('H:s'),
284 4
            'weight'        => $weight,
285 4
            'package_count' => $packageCount,
286 4
            'message'       => $message,
287
        ];
288
289 4
        $this->requester->call('v1', $shipper, Request::ORDER_PICKUP, $data);
290 1
    }
291
292
    /**
293
     * Returns available services for the given shipper
294
     *
295
     * @param string $shipper
296
     *
297
     * @return string[]
298
     *
299
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
300
     */
301 10
    public function getServices(string $shipper): array
302
    {
303 10
        $response = $this->requester->call('v1', $shipper, Request::SERVICES);
304
305 6
        if (isset($response['service_types']) === false) {
306 3
            return [];
307
        }
308
309 3
        return $response['service_types'];
310
    }
311
312
    /**
313
     * Returns available manipulation units for the given shipper
314
     *
315
     * @param string $shipper
316
     *
317
     * @return string[]
318
     *
319
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
320
     */
321 8 View Code Duplication
    public function getManipulationUnits(string $shipper): array
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
322
    {
323 8
        $response = $this->requester->call('v1', $shipper, Request::MANIPULATION_UNITS);
324
325 5
        if ($response['units'] === null) {
326 1
            return [];
327
        }
328
329 4
        $units = [];
330
331 4
        foreach ($response['units'] as $item) {
332 2
            $units[$item['code']] = $item['name'];
333
        }
334
335 4
        return $units;
336
    }
337
338
    /**
339
     * Returns available branches for the given shipper and its service
340
     * Full branches instead branches request.
341
     *
342
     * @param string $shipper
343
     * @param string $service
344
     * @param bool   $fullData
345
     *
346
     * @return array[]
347
     *
348
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
349
     */
350 10
    public function getBranches(string $shipper, string $service = null, bool $fullData = false): array
351
    {
352 10
        $request = $fullData ? Request::FULL_BRANCHES : Request::BRANCHES;
353
354 10
        $response = $this->requester->call('v1', $shipper, $request . '/' . $service);
355
356 7
        if ($response['branches'] === null) {
357 1
            return [];
358
        }
359
360 6
        return $response['branches'];
361
    }
362
363
    /**
364
     * Returns available branches for the given shipper in given location
365
     *
366
     * @param string      $shipper
367
     * @param string      $country
368
     * @param string      $city
369
     * @param string|null $postcode
370
     * @param string|null $street
371
     * @param int|null    $maxResults
372
     * @param float|null  $radius
373
     *
374
     * @return array[]
375
     *
376
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
377
     */
378 8
    public function getBranchesForLocation(
379
        string $shipper,
380
        string $country,
381
        string $city,
382
        string $postcode = null,
383
        string $street = null,
384
        int $maxResults = null,
385
        float $radius = null
386
    ): array {
387 8
        Country::validateCode($country);
388
389
        $data = [
390 8
            'country'     => $country,
391 8
            'city'        => $city,
392 8
            'zip'         => $postcode,
393 8
            'street'      => $street,
394 8
            'max_results' => $maxResults,
395 8
            'radius'      => $radius,
396
        ];
397
398 8
        $data = array_filter($data);
399
400 8
        $response = $this->requester->call('v1', $shipper, Request::BRANCH_LOCATOR, $data);
401
402 5
        if ($response['branches'] === null) {
403 1
            return [];
404
        }
405
406 4
        return $response['branches'];
407
    }
408
409
    /**
410
     * Returns list of countries where service with cash-on-delivery payment type is available in
411
     *
412
     * @param string $shipper
413
     *
414
     * @return array[]
415
     *
416
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
417
     */
418 8 View Code Duplication
    public function getCodCountries(string $shipper): array
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
419
    {
420 8
        $response = $this->requester->call('v1', $shipper, Request::CASH_ON_DELIVERY_COUNTRIES);
421
422 5
        if ($response['service_types'] === null) {
423 1
            return [];
424
        }
425
426 4
        $services = [];
427
428 4
        foreach ($response['service_types'] as $item) {
429 2
            $services[$item['service_type']] = $item['cod_countries'];
430
        }
431
432 4
        return $services;
433
    }
434
435
    /**
436
     * Returns list of countries where service is available in
437
     *
438
     * @param string $shipper
439
     *
440
     * @return array[]
441
     *
442
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
443
     */
444 8 View Code Duplication
    public function getCountries(string $shipper): array
445
    {
446 8
        $response = $this->requester->call('v1', $shipper, Request::COUNTRIES);
447
448 5
        if ($response['service_types'] === null) {
449 1
            return [];
450
        }
451
452 4
        $services = [];
453
454 4
        foreach ($response['service_types'] as $item) {
455 2
            $services[$item['service_type']] = $item['countries'];
456
        }
457
458 4
        return $services;
459
    }
460
461
    /**
462
     * Returns available branches for the given shipper and its service
463
     *
464
     * @param string      $shipper
465
     * @param string      $service
466
     * @param string|null $country
467
     *
468
     * @return array[]
469
     *
470
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
471
     */
472 12
    public function getPostCodes(string $shipper, string $service, string $country = null): array
473
    {
474 12
        if ($country !== null) {
475 1
            Country::validateCode($country);
476
477 1
            $urlPath = $service . '/' . $country;
478
        } else {
479 11
            $urlPath = $service;
480
        }
481
482 12
        $response = $this->requester->call('v1', $shipper, Request::ZIP_CODES . '/' . $urlPath);
483
484 9
        if ($response['zip_codes'] === null) {
485 1
            return [];
486
        }
487
488 8
        $country   = $response['country'] ?? $country;
489 8
        $postCodes = [];
490
491 8
        foreach ($response['zip_codes'] as $postCode) {
492 5
            $postCodes[] = [
493 5
                'postcode'     => $postCode['zip'] ?? ($postCode['zip_start'] ?? null),
494 5
                'postcode_end' => $postCode['zip_end'] ?? null,
495 5
                'city'         => $postCode['city'] ?? null,
496 5
                'country'      => $postCode['country'] ?? $country,
497 5
                '1B'           => (bool) ($postCode['1B'] ?? false),
498
            ];
499
        }
500
501 8
        return $postCodes;
502
    }
503
504
    /**
505
     * Check package(s) data.
506
     *
507
     * @param string $shipper
508
     * @param array  $packages
509
     *
510
     * @return void
511
     *
512
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
513
     */
514 5
    public function checkPackages(string $shipper, array $packages): void
515
    {
516 5
        $this->requester->call('v1', $shipper, Request::CHECK, $packages);
517 2
    }
518
519
    /**
520
     * Returns available manipulation units for the given shipper
521
     *
522
     * @param string $shipper
523
     *
524
     * @return string[]
525
     *
526
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
527
     */
528 8 View Code Duplication
    public function getAdrUnits(string $shipper): array
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
529
    {
530 8
        $response = $this->requester->call('v1', $shipper, Request::ADR_UNITS);
531
532 5
        if ($response['units'] === null) {
533 1
            return [];
534
        }
535
536 4
        $units = [];
537
538 4
        foreach ($response['units'] as $item) {
539 2
            $units[$item['code']] = $item['name'];
540
        }
541
542 4
        return $units;
543
    }
544
545
    /**
546
     * Returns available activated services for the given shipper
547
     *
548
     * @param string $shipper
549
     *
550
     * @return array
551
     *
552
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
553
     */
554 6
    public function getActivatedServices(string $shipper): array
555
    {
556 6
        $response = $this->requester->call('v1', $shipper, Request::ACTIVATEDSERVICES);
557
558 4
        unset($response['status']);
559
560 4
        return $response;
561
    }
562
}
563