Completed
Push — master ( 7e4c5d...c64fd1 )
by Tomáš
03:19
created

Balikobot::getAllBranchesForShipperServiceForCountries()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 3
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 $packages
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 5
    public function getServices(string $shipper): array
322
    {
323 5
        $services = $this->client->getServices($shipper);
324
325 4
        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
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
348
     *
349
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
350
     */
351 1
    public function getBranches(): iterable
352
    {
353
        // get all shipper service codes
354 1
        $shippers = $this->getShippers();
355
356 1
        foreach ($shippers as $shipper) {
357 1
            yield from $this->getBranchesForShipper($shipper);
358
        }
359 1
    }
360
361
    /**
362
     * Get all available branches for countries
363
     *
364
     * @param array $countries
365
     *
366
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
367
     *
368
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
369
     */
370 1
    public function getBranchesForCountries(array $countries): iterable
371
    {
372
        // get all shipper service codes
373 1
        $shippers = $this->getShippers();
374
375 1
        foreach ($shippers as $shipper) {
376 1
            yield from $this->getBranchesForShipperForCountries($shipper, $countries);
377
        }
378 1
    }
379
380
    /**
381
     * Get all available branches for given shipper
382
     *
383
     * @param string $shipper
384
     *
385
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
386
     *
387
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
388
     */
389 2
    public function getBranchesForShipper(string $shipper): iterable
390
    {
391
        // get all services for shipper service
392 2
        $services = $this->getServicesForShipper($shipper);
393
394
        // get branches for all services
395 2
        foreach ($services as $service) {
396 2
            yield from $this->getBranchesForShipperService($shipper, $service);
397
        }
398 2
    }
399
400
    /**
401
     * Get all available branches for given shipper for countries
402
     *
403
     * @param string $shipper
404
     * @param array  $countries
405
     *
406
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
407
     *
408
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
409
     */
410 2
    public function getBranchesForShipperForCountries(string $shipper, array $countries): iterable
411
    {
412
        // get all services for shipper service
413 2
        $services = $this->getServicesForShipper($shipper);
414
415
        // get branches for all services
416 2
        foreach ($services as $service) {
417 2
            yield from $this->getBranchesForShipperServiceForCountries($shipper, $service, $countries);
418
        }
419 2
    }
420
421
    /**
422
     * Get services for shipper
423
     *
424
     * @param string $shipper
425
     *
426
     * @return array
427
     *
428
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
429
     */
430 4
    private function getServicesForShipper(string $shipper): array
431
    {
432
        // get all services for shipper service
433 4
        $services = array_keys($this->getServices($shipper));
434
435
        // support shipper withou service type
436 4
        if (empty($services)) {
437 2
            $services = [null];
438
        }
439
440 4
        return $services;
441
    }
442
443
    /**
444
     * Get all available branches for given shipper and service type
445
     *
446
     * @param string      $shipper
447
     * @param string|null $service
448
     * @param string|null $country
449
     *
450
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
451
     *
452
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
453
     */
454 4
    public function getBranchesForShipperService(string $shipper, ?string $service, string $country = null): iterable
455
    {
456 4
        $fullData = Shipper::hasFullBranchesSupport($shipper, $service);
457 4
        $branches = $this->client->getBranches($shipper, $service, $fullData, $country);
458
459 4
        foreach ($branches as $branch) {
460 2
            yield Branch::newInstanceFromData($shipper, $service, $branch);
461
        }
462 4
    }
463
464
    /**
465
     * Get all available branches for given shipper and service type for countries
466
     *
467
     * @param string      $shipper
468
     * @param string|null $service
469
     * @param array       $countries
470
     *
471
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
472
     *
473
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
474
     */
475 3
    public function getBranchesForShipperServiceForCountries(
476
        string $shipper,
477
        ?string $service,
478
        array $countries
479
    ): iterable {
480 3
        $branches = $this->getAllBranchesForShipperServiceForCountries($shipper, $service, $countries);
481
482 3
        foreach ($branches as $branch) {
483 3
            if (in_array($branch->getCountry(), $countries)) {
484 3
                yield $branch;
485
            }
486
        }
487 3
    }
488
489
    /**
490
     * Get all available branches for given shipper and service type filtered by countries if possible
491
     *
492
     * @param string      $shipper
493
     * @param string|null $service
494
     * @param array       $countries
495
     *
496
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
497
     *
498
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
499
     */
500 3
    private function getAllBranchesForShipperServiceForCountries(
501
        string $shipper,
502
        ?string $service,
503
        array $countries
504
    ): iterable {
505 3
        if (Shipper::hasBranchCountryFilterSupport($shipper) === false) {
506 2
            yield from $this->getBranchesForShipperService($shipper, $service);
507 2
            return;
508
        }
509
510 2
        foreach ($countries as $country) {
511 2
            yield from $this->getBranchesForShipperService($shipper, $service, $country);
512
        }
513 2
    }
514
515
    /**
516
     * Get all available branches for given shipper
517
     *
518
     * @param string      $shipper
519
     * @param string      $country
520
     * @param string      $city
521
     * @param string|null $postcode
522
     * @param string|null $street
523
     * @param int|null    $maxResults
524
     * @param float|null  $radius
525
     * @param string|null $type
526
     *
527
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
528
     *
529
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
530
     */
531 3
    public function getBranchesForLocation(
532
        string $shipper,
533
        string $country,
534
        string $city,
535
        string $postcode = null,
536
        string $street = null,
537
        int $maxResults = null,
538
        float $radius = null,
539
        string $type = null
540
    ): iterable {
541 3
        $branches = $this->client->getBranchesForLocation(
542 3
            $shipper,
543 3
            $country,
544 3
            $city,
545 3
            $postcode,
546 3
            $street,
547 3
            $maxResults,
548 3
            $radius,
549 3
            $type
550
        );
551
552 3
        foreach ($branches as $branch) {
553 1
            yield Branch::newInstanceFromData($shipper, null, $branch);
554
        }
555 3
    }
556
557
    /**
558
     * Returns list of countries where service with cash-on-delivery payment type is available in
559
     *
560
     * @param string $shipper
561
     *
562
     * @return array[]
563
     *
564
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
565
     */
566 2
    public function getCodCountries(string $shipper): array
567
    {
568 2
        $countries = $this->client->getCodCountries($shipper);
569
570 2
        return $countries;
571
    }
572
573
    /**
574
     * Returns list of countries where service is available in
575
     *
576
     * @param string $shipper
577
     *
578
     * @return array[]
579
     *
580
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
581
     */
582 2
    public function getCountries(string $shipper): array
583
    {
584 2
        $countries = $this->client->getCountries($shipper);
585
586 2
        return $countries;
587
    }
588
589
    /**
590
     * Returns available branches for the given shipper and its service
591
     *
592
     * @param string      $shipper
593
     * @param string      $service
594
     * @param string|null $country
595
     *
596
     * @return \Generator|\Inspirum\Balikobot\Model\Values\PostCode[]
597
     *
598
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
599
     */
600 2
    public function getPostCodes(string $shipper, string $service, string $country = null): iterable
601
    {
602 2
        $postcodes = $this->client->getPostCodes($shipper, $service, $country);
603
604 2
        foreach ($postcodes as $postcode) {
605 1
            yield PostCode::newInstanceFromData($shipper, $service, $postcode);
606
        }
607 2
    }
608
609
    /**
610
     * Check package(s) data
611
     *
612
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
613
     *
614
     * @return void
615
     *
616
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
617
     */
618 1
    public function checkPackages(PackageCollection $packages): void
619
    {
620 1
        $this->client->checkPackages($packages->getShipper(), $packages->toArray());
621 1
    }
622
623
    /**
624
     * Returns available manipulation units for the given shipper
625
     *
626
     * @param string $shipper
627
     *
628
     * @return string[]
629
     *
630
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
631
     */
632 2
    public function getAdrUnits(string $shipper): array
633
    {
634 2
        $units = $this->client->getAdrUnits($shipper);
635
636 2
        return $units;
637
    }
638
639
    /**
640
     * Returns available activated services for the given shipper
641
     *
642
     * @param string $shipper
643
     *
644
     * @return array
645
     *
646
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
647
     */
648 2
    public function getActivatedServices(string $shipper): array
649
    {
650 2
        $units = $this->client->getActivatedServices($shipper);
651
652 2
        return $units;
653
    }
654
655
    /**
656
     * Order shipments from place B (typically supplier / previous consignee) to place A (shipping point)
657
     *
658
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
659
     *
660
     * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[]
661
     *
662
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
663
     */
664 3
    public function orderB2AShipment(PackageCollection $packages): OrderedPackageCollection
665
    {
666 3
        $response = $this->client->orderB2AShipment($packages->getShipper(), $packages->toArray());
667
668
        // create return value object
669 3
        $orderedPackages = new OrderedPackageCollection();
670
671 3
        foreach ($response as $i => $package) {
672 3
            $orderedPackages->add(OrderedPackage::newInstanceFromData(
673 3
                $packages->getShipper(),
674 3
                $packages->getEID(),
675 3
                $package
676
            ));
677
        }
678
679 3
        return $orderedPackages;
680
    }
681
682
    /**
683
     * Get PDF link with signed consignment delivery document by the recipient
684
     *
685
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
686
     *
687
     * @return string
688
     *
689
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
690
     */
691 2
    public function getProofOfDelivery(OrderedPackage $package): string
692
    {
693 2
        $packages = new OrderedPackageCollection();
694 2
        $packages->add($package);
695
696 2
        $response = $this->getProofOfDeliveries($packages);
697
698 2
        return $response[0];
699
    }
700
701
    /**
702
     * Get array of PDF links with signed consignment delivery document by the recipient
703
     *
704
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
705
     *
706
     * @return string[]
707
     *
708
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
709
     */
710 4
    public function getProofOfDeliveries(OrderedPackageCollection $packages): array
711
    {
712 4
        $response = $this->client->getProofOfDeliveries($packages->getShipper(), $packages->getCarrierIds());
713
714 4
        return $response;
715
    }
716
}
717