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

Balikobot::trackPackages()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.7333
ccs 8
cts 8
cp 1
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Inspirum\Balikobot\Services;
4
5
use DateTime;
6
use Inspirum\Balikobot\Contracts\RequesterInterface;
7
use Inspirum\Balikobot\Definitions\Option;
8
use Inspirum\Balikobot\Definitions\Shipper;
9
use Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection;
10
use Inspirum\Balikobot\Model\Aggregates\PackageCollection;
11
use Inspirum\Balikobot\Model\Values\Branch;
12
use Inspirum\Balikobot\Model\Values\OrderedPackage;
13
use Inspirum\Balikobot\Model\Values\OrderedShipment;
14
use Inspirum\Balikobot\Model\Values\Package;
15
use Inspirum\Balikobot\Model\Values\PackageStatus;
16
use Inspirum\Balikobot\Model\Values\PostCode;
17
18
class Balikobot
19
{
20
    /**
21
     * Balikobot API
22
     *
23
     * @var \Inspirum\Balikobot\Services\Client
24
     */
25
    private $client;
26
27
    /**
28
     * Balikobot constructor
29
     *
30
     * @param \Inspirum\Balikobot\Contracts\RequesterInterface $requester
31
     */
32 66
    public function __construct(RequesterInterface $requester)
33
    {
34 66
        $this->client = new Client($requester);
35 66
    }
36
37
    /**
38
     * All supported shipper services
39
     *
40
     * @return string[]
41
     */
42 1
    public function getShippers(): array
43
    {
44 1
        return Shipper::all();
45
    }
46
47
    /**
48
     * Add packages
49
     *
50
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
51
     *
52
     * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[]
53
     *
54
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
55
     */
56 7
    public function addPackages(PackageCollection $packages): OrderedPackageCollection
57
    {
58 7
        $version  = Shipper::resolveAddRequestVersion($packages->getShipper(), $packages->toArray());
59 7
        $response = $this->client->addPackages($packages->getShipper(), $packages->toArray(), $version);
60
61
        // create return value object
62 7
        $orderedPackages = new OrderedPackageCollection();
63
64 7
        foreach ($response as $i => $package) {
65 7
            $orderedPackages->add(OrderedPackage::newInstanceFromData(
66 7
                $packages->getShipper(),
67 7
                $packages->getEID(),
68 7
                $package
69
            ));
70
        }
71
72 7
        return $orderedPackages;
73
    }
74
75
    /**
76
     * Exports order into Balikobot system
77
     *
78
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
79
     *
80
     * @return void
81
     *
82
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
83
     */
84 1
    public function dropPackages(OrderedPackageCollection $packages): void
85
    {
86 1
        $this->client->dropPackages($packages->getShipper(), $packages->getPackageIds());
87 1
    }
88
89
    /**
90
     * Exports order into Balikobot system
91
     *
92
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
93
     *
94
     * @return void
95
     *
96
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
97
     */
98 1
    public function dropPackage(OrderedPackage $package): void
99
    {
100 1
        $this->client->dropPackage($package->getShipper(), $package->getPackageId());
101 1
    }
102
103
    /**
104
     * Order shipment
105
     *
106
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
107
     * @param \DateTime|null                                                $date
108
     *
109
     * @return \Inspirum\Balikobot\Model\Values\OrderedShipment
110
     *
111
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
112
     */
113 2
    public function orderShipment(OrderedPackageCollection $packages, DateTime $date = null): OrderedShipment
114
    {
115 2
        if ($date !== null) {
116 2
            $date->setTime(0, 0, 0);
117
        }
118
119 2
        $response = $this->client->orderShipment($packages->getShipper(), $packages->getPackageIds(), $date);
120
121 2
        $orderedShipment = OrderedShipment::newInstanceFromData(
122 2
            $packages->getShipper(),
123 2
            $packages->getPackageIds(),
124 2
            $response,
125 2
            $date
126
        );
127
128 2
        return $orderedShipment;
129
    }
130
131
    /**
132
     * Track package
133
     *
134
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
135
     *
136
     * @return \Inspirum\Balikobot\Model\Values\PackageStatus[]
137
     *
138
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
139
     */
140 2
    public function trackPackage(OrderedPackage $package): array
141
    {
142 2
        $packages = new OrderedPackageCollection();
143 2
        $packages->add($package);
144
145 2
        $response = $this->trackPackages($packages);
146
147 2
        return $response[0];
148
    }
149
150
    /**
151
     * Track packages
152
     *
153
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
154
     *
155
     * @return \Inspirum\Balikobot\Model\Values\PackageStatus[][]
156
     *
157
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
158
     */
159 4
    public function trackPackages(OrderedPackageCollection $packages): array
160
    {
161 4
        $response = $this->client->trackPackages($packages->getShipper(), $packages->getCarrierIds());
162
163 4
        $statuses = [];
164
165 4
        foreach ($response as $i => $responseStatuses) {
166 4
            $statuses[$i] = [];
167
168 4
            foreach ($responseStatuses as $status) {
169 4
                $statuses[$i][] = PackageStatus::newInstanceFromData($status);
170
            }
171
        }
172
173 4
        return $statuses;
174
    }
175
176
    /**
177
     * Track package last status
178
     *
179
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
180
     *
181
     * @return \Inspirum\Balikobot\Model\Values\PackageStatus
182
     *
183
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
184
     */
185 2
    public function trackPackageLastStatus(OrderedPackage $package): PackageStatus
186
    {
187 2
        $packages = new OrderedPackageCollection();
188 2
        $packages->add($package);
189
190 2
        $response = $this->trackPackagesLastStatus($packages);
191
192 2
        return $response[0];
193
    }
194
195
    /**
196
     * Track package last status
197
     *
198
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $package
0 ignored issues
show
Documentation introduced by
There is no parameter named $package. Did you maybe mean $packages?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
199
     *
200
     * @return \Inspirum\Balikobot\Model\Values\PackageStatus[]
201
     *
202
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
203
     */
204 4
    public function trackPackagesLastStatus(OrderedPackageCollection $packages): array
205
    {
206 4
        $response = $this->client->trackPackagesLastStatus($packages->getShipper(), $packages->getCarrierIds());
207
208 4
        $statuses = [];
209
210 4
        foreach ($response as $status) {
211 4
            $statuses[] = PackageStatus::newInstanceFromData($status);
212
        }
213
214 4
        return $statuses;
215
    }
216
217
    /**
218
     * Get overview for given shipper
219
     *
220
     * @param string $shipper
221
     *
222
     * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection
223
     *
224
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
225
     */
226 2
    public function getOverview(string $shipper): OrderedPackageCollection
227
    {
228 2
        $response = $this->client->getOverview($shipper);
229
230
        // create return value object
231 2
        $orderedPackages = new OrderedPackageCollection();
232
233 2
        foreach ($response as $i => $package) {
234 1
            $orderedPackages->add(OrderedPackage::newInstanceFromData(
235 1
                $shipper,
236 1
                $package['eshop_id'],
237 1
                $package
238
            ));
239
        }
240
241 2
        return $orderedPackages;
242
    }
243
244
    /**
245
     * Get labels for orders
246
     *
247
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
248
     *
249
     * @return string
250
     *
251
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
252
     */
253 2
    public function getLabels(OrderedPackageCollection $packages): string
254
    {
255 2
        $response = $this->client->getLabels($packages->getShipper(), $packages->getPackageIds());
256
257 2
        return $response;
258
    }
259
260
    /**
261
     * Gets complete information about a package
262
     *
263
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
264
     *
265
     * @return \Inspirum\Balikobot\Model\Values\Package
266
     *
267
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
268
     */
269 2
    public function getPackageInfo(OrderedPackage $package): Package
270
    {
271 2
        $response = $this->client->getPackageInfo($package->getShipper(), $package->getPackageId());
272
273 2
        $options              = $response;
274 2
        $options[Option::EID] = $package->getBatchId();
275
276 2
        unset($options['package_id']);
277 2
        unset($options['eshop_id']);
278 2
        unset($options['carrier_id']);
279 2
        unset($options['track_url']);
280 2
        unset($options['label_url']);
281 2
        unset($options['carrier_id_swap']);
282 2
        unset($options['pieces']);
283
284 2
        $package = new Package($options);
285
286 2
        return $package;
287
    }
288
289
    /**
290
     * Gets complete information about a package
291
     *
292
     * @param string $shipper
293
     * @param int    $orderId
294
     *
295
     * @return \Inspirum\Balikobot\Model\Values\OrderedShipment
296
     *
297
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
298
     */
299 2
    public function getOrder(string $shipper, int $orderId): OrderedShipment
300
    {
301 2
        $response = $this->client->getOrder($shipper, $orderId);
302
303 2
        $orderedShipment = OrderedShipment::newInstanceFromData(
304 2
            $shipper,
305 2
            $response['package_ids'],
306 2
            $response
307
        );
308
309 2
        return $orderedShipment;
310
    }
311
312
    /**
313
     * Returns available services for the given shipper
314
     *
315
     * @param string $shipper
316
     *
317
     * @return string[]
318
     *
319
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
320
     */
321 4
    public function getServices(string $shipper): array
322
    {
323 4
        $services = $this->client->getServices($shipper);
324
325 3
        return $services;
326
    }
327
328
    /**
329
     * Returns available manipulation units for the given shipper
330
     *
331
     * @param string $shipper
332
     *
333
     * @return string[]
334
     *
335
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
336
     */
337 2
    public function getManipulationUnits(string $shipper): array
338
    {
339 2
        $units = $this->client->getManipulationUnits($shipper);
340
341 2
        return $units;
342
    }
343
344
    /**
345
     * Get all available branches
346
     *
347
     * @param string|null $country
348
     *
349
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
350
     *
351
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
352
     */
353 3
    public function getBranches(string $country = null): iterable
354
    {
355
        // get all shipper service codes
356 3
        $shippers = $this->getShippers();
357
358 3
        foreach ($shippers as $shipper) {
359 3
            yield from $this->getBranchesForShipper($shipper, $country);
360
        }
361 3
    }
362
363
    /**
364
     * Get all available branches for countries
365
     *
366
     * @param array $countries
367
     *
368
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
369
     *
370
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
371
     */
372 1
    public function getBranchesForCountries(array $countries): iterable
373
    {
374 1
        foreach ($countries as $country) {
375 1
            yield from $this->getBranches($country);
376
        }
377 1
    }
378
379
    /**
380
     * Get all available branches for given shipper
381
     *
382
     * @param string      $shipper
383
     * @param string|null $country
384
     *
385
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
386
     *
387
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
388
     */
389 4
    public function getBranchesForShipper(string $shipper, string $country = null): iterable
390
    {
391
        // get all services for shipper service
392 4
        $services = array_keys($this->getServices($shipper));
393
394
        // support shipper withou service type
395 4
        if (empty($services)) {
396 1
            $services = [null];
397
        }
398
399
        // get branches for all services
400 4
        foreach ($services as $service) {
401 4
            yield from $this->getBranchesForShipperService($shipper, $service, $country);
402
        }
403 4
    }
404
405
    /**
406
     * Get all available branches for given shipper for countries
407
     *
408
     * @param string $shipper
409
     * @param array  $countries
410
     *
411
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
412
     *
413
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
414
     */
415 1
    public function getBranchesForShipperForCountries(string $shipper, array $countries): iterable
416
    {
417 1
        foreach ($countries as $country) {
418 1
            yield from $this->getBranchesForShipper($shipper, $country);
419
        }
420 1
    }
421
422
    /**
423
     * Get all available branches for given shipper and service type
424
     *
425
     * @param string      $shipper
426
     * @param string|null $service
427
     * @param string|null $country
428
     *
429
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
430
     *
431
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
432
     */
433 3
    public function getBranchesForShipperService(string $shipper, ?string $service, string $country = null): iterable
434
    {
435 3
        $fullData = Shipper::hasFullBranchesSupport($shipper, $service);
436 3
        $branches = $this->client->getBranches($shipper, $service, $fullData, $country);
437
438 3
        foreach ($branches as $branch) {
439 1
            yield Branch::newInstanceFromData($shipper, $service, $branch);
440
        }
441 3
    }
442
443
    /**
444
     * Get all available branches for given shipper and service type for countries
445
     *
446
     * @param string      $shipper
447
     * @param string|null $service
448
     * @param array       $countries
449
     *
450
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
451
     *
452
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
453
     */
454 1
    public function getBranchesForShipperServiceForCountries(string $shipper, ?string $service, array $countries): iterable
455
    {
456 1
        foreach ($countries as $country) {
457 1
            yield from $this->getBranchesForShipperService($shipper, $service, $country);
458
        }
459 1
    }
460
461
    /**
462
     * Get all available branches for given shipper
463
     *
464
     * @param string      $shipper
465
     * @param string      $country
466
     * @param string      $city
467
     * @param string|null $postcode
468
     * @param string|null $street
469
     * @param int|null    $maxResults
470
     * @param float|null  $radius
471
     * @param string|null $type
472
     *
473
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
474
     *
475
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
476
     */
477 3
    public function getBranchesForLocation(
478
        string $shipper,
479
        string $country,
480
        string $city,
481
        string $postcode = null,
482
        string $street = null,
483
        int $maxResults = null,
484
        float $radius = null,
485
        string $type = null
486
    ): iterable {
487 3
        $branches = $this->client->getBranchesForLocation(
488 3
            $shipper,
489 3
            $country,
490 3
            $city,
491 3
            $postcode,
492 3
            $street,
493 3
            $maxResults,
494 3
            $radius,
495 3
            $type
496
        );
497
498 3
        foreach ($branches as $branch) {
499 1
            yield Branch::newInstanceFromData($shipper, null, $branch);
500
        }
501 3
    }
502
503
    /**
504
     * Returns list of countries where service with cash-on-delivery payment type is available in
505
     *
506
     * @param string $shipper
507
     *
508
     * @return array[]
509
     *
510
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
511
     */
512 2
    public function getCodCountries(string $shipper): array
513
    {
514 2
        $countries = $this->client->getCodCountries($shipper);
515
516 2
        return $countries;
517
    }
518
519
    /**
520
     * Returns list of countries where service is available in
521
     *
522
     * @param string $shipper
523
     *
524
     * @return array[]
525
     *
526
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
527
     */
528 2
    public function getCountries(string $shipper): array
529
    {
530 2
        $countries = $this->client->getCountries($shipper);
531
532 2
        return $countries;
533
    }
534
535
    /**
536
     * Returns available branches for the given shipper and its service
537
     *
538
     * @param string      $shipper
539
     * @param string      $service
540
     * @param string|null $country
541
     *
542
     * @return \Generator|\Inspirum\Balikobot\Model\Values\PostCode[]
543
     *
544
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
545
     */
546 2
    public function getPostCodes(string $shipper, string $service, string $country = null): iterable
547
    {
548 2
        $postcodes = $this->client->getPostCodes($shipper, $service, $country);
549
550 2
        foreach ($postcodes as $postcode) {
551 1
            yield PostCode::newInstanceFromData($shipper, $service, $postcode);
552
        }
553 2
    }
554
555
    /**
556
     * Check package(s) data
557
     *
558
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
559
     *
560
     * @return void
561
     *
562
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
563
     */
564 1
    public function checkPackages(PackageCollection $packages): void
565
    {
566 1
        $this->client->checkPackages($packages->getShipper(), $packages->toArray());
567 1
    }
568
569
    /**
570
     * Returns available manipulation units for the given shipper
571
     *
572
     * @param string $shipper
573
     *
574
     * @return string[]
575
     *
576
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
577
     */
578 2
    public function getAdrUnits(string $shipper): array
579
    {
580 2
        $units = $this->client->getAdrUnits($shipper);
581
582 2
        return $units;
583
    }
584
585
    /**
586
     * Returns available activated services for the given shipper
587
     *
588
     * @param string $shipper
589
     *
590
     * @return array
591
     *
592
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
593
     */
594 2
    public function getActivatedServices(string $shipper): array
595
    {
596 2
        $units = $this->client->getActivatedServices($shipper);
597
598 2
        return $units;
599
    }
600
601
    /**
602
     * Order shipments from place B (typically supplier / previous consignee) to place A (shipping point)
603
     *
604
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
605
     *
606
     * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[]
607
     *
608
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
609
     */
610 3
    public function orderB2AShipment(PackageCollection $packages): OrderedPackageCollection
611
    {
612 3
        $response = $this->client->orderB2AShipment($packages->getShipper(), $packages->toArray());
613
614
        // create return value object
615 3
        $orderedPackages = new OrderedPackageCollection();
616
617 3
        foreach ($response as $i => $package) {
618 3
            $orderedPackages->add(OrderedPackage::newInstanceFromData(
619 3
                $packages->getShipper(),
620 3
                $packages->getEID(),
621 3
                $package
622
            ));
623
        }
624
625 3
        return $orderedPackages;
626
    }
627
628
    /**
629
     * Get PDF link with signed consignment delivery document by the recipient
630
     *
631
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
0 ignored issues
show
Documentation introduced by
There is no parameter named $packages. Did you maybe mean $package?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
632
     *
633
     * @return string
634
     *
635
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
636
     */
637 2
    public function getProofOfDelivery(OrderedPackage $package): string
638
    {
639 2
        $packages = new OrderedPackageCollection();
640 2
        $packages->add($package);
641
642 2
        $response = $this->getProofOfDeliveries($packages);
643
644 2
        return $response[0];
645
    }
646
647
    /**
648
     * Get array of PDF links with signed consignment delivery document by the recipient
649
     *
650
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
651
     *
652
     * @return string[]
653
     *
654
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
655
     */
656 4
    public function getProofOfDeliveries(OrderedPackageCollection $packages): array
657
    {
658 4
        $response = $this->client->getProofOfDeliveries($packages->getShipper(), $packages->getCarrierIds());
659
660 4
        return $response;
661
    }
662
}
663