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

Client::orderB2AShipment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 12
loc 12
c 0
b 0
f 0
rs 9.8666
ccs 6
cts 6
cp 1
cc 2
nc 2
nop 2
crap 2
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 174
    public function __construct(RequesterInterface $requester)
26
    {
27 174
        $this->requester = $requester;
28 174
    }
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 View Code Duplication
    public function addPackages(string $shipper, array $packages): 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...
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
     * @param string|null $type
374
     *
375
     * @return array[]
376
     *
377
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
378
     */
379 9
    public function getBranchesForLocation(
380
        string $shipper,
381
        string $country,
382
        string $city,
383
        string $postcode = null,
384
        string $street = null,
385
        int $maxResults = null,
386
        float $radius = null,
387
        string $type = null
388
    ): array {
389 9
        Country::validateCode($country);
390
391
        $data = [
392 9
            'country'     => $country,
393 9
            'city'        => $city,
394 9
            'zip'         => $postcode,
395 9
            'street'      => $street,
396 9
            'max_results' => $maxResults,
397 9
            'radius'      => $radius,
398 9
            'type'        => $type,
399
        ];
400
401 9
        $data = array_filter($data);
402
403 9
        $response = $this->requester->call('v1', $shipper, Request::BRANCH_LOCATOR, $data);
404
405 6
        if ($response['branches'] === null) {
406 1
            return [];
407
        }
408
409 5
        return $response['branches'];
410
    }
411
412
    /**
413
     * Returns list of countries where service with cash-on-delivery payment type is available in
414
     *
415
     * @param string $shipper
416
     *
417
     * @return array[]
418
     *
419
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
420
     */
421 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...
422
    {
423 8
        $response = $this->requester->call('v1', $shipper, Request::CASH_ON_DELIVERY_COUNTRIES);
424
425 5
        if ($response['service_types'] === null) {
426 1
            return [];
427
        }
428
429 4
        $services = [];
430
431 4
        foreach ($response['service_types'] as $item) {
432 2
            $services[$item['service_type']] = $item['cod_countries'];
433
        }
434
435 4
        return $services;
436
    }
437
438
    /**
439
     * Returns list of countries where service is available in
440
     *
441
     * @param string $shipper
442
     *
443
     * @return array[]
444
     *
445
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
446
     */
447 8 View Code Duplication
    public function getCountries(string $shipper): array
448
    {
449 8
        $response = $this->requester->call('v1', $shipper, Request::COUNTRIES);
450
451 5
        if ($response['service_types'] === null) {
452 1
            return [];
453
        }
454
455 4
        $services = [];
456
457 4
        foreach ($response['service_types'] as $item) {
458 2
            $services[$item['service_type']] = $item['countries'];
459
        }
460
461 4
        return $services;
462
    }
463
464
    /**
465
     * Returns available branches for the given shipper and its service
466
     *
467
     * @param string      $shipper
468
     * @param string      $service
469
     * @param string|null $country
470
     *
471
     * @return array[]
472
     *
473
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
474
     */
475 12
    public function getPostCodes(string $shipper, string $service, string $country = null): array
476
    {
477 12
        if ($country !== null) {
478 1
            Country::validateCode($country);
479
480 1
            $urlPath = $service . '/' . $country;
481
        } else {
482 11
            $urlPath = $service;
483
        }
484
485 12
        $response = $this->requester->call('v1', $shipper, Request::ZIP_CODES . '/' . $urlPath);
486
487 9
        if ($response['zip_codes'] === null) {
488 1
            return [];
489
        }
490
491 8
        $country   = $response['country'] ?? $country;
492 8
        $postCodes = [];
493
494 8
        foreach ($response['zip_codes'] as $postCode) {
495 5
            $postCodes[] = [
496 5
                'postcode'     => $postCode['zip'] ?? ($postCode['zip_start'] ?? null),
497 5
                'postcode_end' => $postCode['zip_end'] ?? null,
498 5
                'city'         => $postCode['city'] ?? null,
499 5
                'country'      => $postCode['country'] ?? $country,
500 5
                '1B'           => (bool) ($postCode['1B'] ?? false),
501
            ];
502
        }
503
504 8
        return $postCodes;
505
    }
506
507
    /**
508
     * Check package(s) data
509
     *
510
     * @param string $shipper
511
     * @param array  $packages
512
     *
513
     * @return void
514
     *
515
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
516
     */
517 5
    public function checkPackages(string $shipper, array $packages): void
518
    {
519 5
        $this->requester->call('v1', $shipper, Request::CHECK, $packages);
520 2
    }
521
522
    /**
523
     * Returns available manipulation units for the given shipper
524
     *
525
     * @param string $shipper
526
     *
527
     * @return string[]
528
     *
529
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
530
     */
531 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...
532
    {
533 8
        $response = $this->requester->call('v1', $shipper, Request::ADR_UNITS);
534
535 5
        if ($response['units'] === null) {
536 1
            return [];
537
        }
538
539 4
        $units = [];
540
541 4
        foreach ($response['units'] as $item) {
542 2
            $units[$item['code']] = $item['name'];
543
        }
544
545 4
        return $units;
546
    }
547
548
    /**
549
     * Returns available activated services for the given shipper
550
     *
551
     * @param string $shipper
552
     *
553
     * @return array
554
     *
555
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
556
     */
557 6
    public function getActivatedServices(string $shipper): array
558
    {
559 6
        $response = $this->requester->call('v1', $shipper, Request::ACTIVATEDSERVICES);
560
561 4
        unset($response['status']);
562
563 4
        return $response;
564
    }
565
566
    /**
567
     * Order shipments from place B (typically supplier / previous consignee) to place A (shipping point)
568
     *
569
     * @param string $shipper
570
     * @param array  $packages
571
     *
572
     * @return array
573
     *
574
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
575
     */
576 9 View Code Duplication
    public function orderB2AShipment(string $shipper, array $packages): 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...
577
    {
578 9
        $response = $this->requester->call('v1', $shipper, Request::B2A, $packages);
579
580 6
        if (isset($response[0]['package_id']) === false) {
581 1
            throw new BadRequestException($response);
582
        }
583
584 5
        unset($response['status']);
585
586 5
        return $response;
587
    }
588
}
589