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

Balikobot::getServicesForShipper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 2
nc 2
nop 1
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\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 69
    public function __construct(RequesterInterface $requester)
33
    {
34 69
        $this->client = new Client($requester);
35 69
    }
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 6
    public function getServices(string $shipper): array
322
    {
323 6
        $services = $this->client->getServices($shipper);
324
325 5
        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 5
    public function getBranchesForShipperService(string $shipper, ?string $service, string $country = null): iterable
455
    {
456 5
        $fullData = Shipper::hasFullBranchesSupport($shipper, $service);
457 5
        $branches = $this->client->getBranches($shipper, $service, $fullData, $country);
458
459 5
        foreach ($branches as $branch) {
460 3
            yield Branch::newInstanceFromData($shipper, $service, $branch);
461
        }
462 5
    }
463
464
    /**
465
     * Get all available branches for given shipper, service type and country code
466
     *
467
     * @param string      $shipper
468
     * @param string|null $service
469
     * @param string|null $country
470
     *
471
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
472
     *
473
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
474
     */
475 4
    public function getBranchesForShipperServiceForCountry(
476
        string $shipper,
477
        ?string $service,
478
        ?string $country
479
    ): iterable {
480 4
        $usedCountry = Shipper::hasBranchCountryFilterSupport($shipper) ? $country : null;
481 4
        $branches    = $this->getBranchesForShipperService($shipper, $service, $usedCountry);
482
483 4
        foreach ($branches as $branch) {
484 4
            if ($country === null || $branch->getCountry() === $country) {
485 4
                yield $branch;
486
            }
487
        }
488 4
    }
489
490
    /**
491
     * Get all available branches for given shipper and service type for countries
492
     *
493
     * @param string      $shipper
494
     * @param string|null $service
495
     * @param array       $countries
496
     *
497
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
498
     *
499
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
500
     */
501 3
    public function getBranchesForShipperServiceForCountries(
502
        string $shipper,
503
        ?string $service,
504
        array $countries
505
    ): iterable {
506 3
        if (Shipper::hasBranchCountryFilterSupport($shipper)) {
507 2
            foreach ($countries as $country) {
508 2
                yield from $this->getBranchesForShipperServiceForCountry($shipper, $service, $country);
509
            }
510
511 2
            return;
512
        }
513
514 2
        $branches = $this->getBranchesForShipperService($shipper, $service);
515
516 2
        foreach ($branches as $branch) {
517 2
            if (in_array($branch->getCountry(), $countries)) {
518 2
                yield $branch;
519
            }
520
        }
521 2
    }
522
523
    /**
524
     * Get all available branches for given shipper
525
     *
526
     * @param string      $shipper
527
     * @param string      $country
528
     * @param string      $city
529
     * @param string|null $postcode
530
     * @param string|null $street
531
     * @param int|null    $maxResults
532
     * @param float|null  $radius
533
     * @param string|null $type
534
     *
535
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
536
     *
537
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
538
     */
539 3
    public function getBranchesForLocation(
540
        string $shipper,
541
        string $country,
542
        string $city,
543
        string $postcode = null,
544
        string $street = null,
545
        int $maxResults = null,
546
        float $radius = null,
547
        string $type = null
548
    ): iterable {
549 3
        $branches = $this->client->getBranchesForLocation(
550 3
            $shipper,
551 3
            $country,
552 3
            $city,
553 3
            $postcode,
554 3
            $street,
555 3
            $maxResults,
556 3
            $radius,
557 3
            $type
558
        );
559
560 3
        foreach ($branches as $branch) {
561 1
            yield Branch::newInstanceFromData($shipper, null, $branch);
562
        }
563 3
    }
564
565
    /**
566
     * Returns list of countries where service with cash-on-delivery payment type is available in
567
     *
568
     * @param string $shipper
569
     *
570
     * @return array[]
571
     *
572
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
573
     */
574 2
    public function getCodCountries(string $shipper): array
575
    {
576 2
        $countries = $this->client->getCodCountries($shipper);
577
578 2
        return $countries;
579
    }
580
581
    /**
582
     * Returns list of countries where service is available in
583
     *
584
     * @param string $shipper
585
     *
586
     * @return array[]
587
     *
588
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
589
     */
590 2
    public function getCountries(string $shipper): array
591
    {
592 2
        $countries = $this->client->getCountries($shipper);
593
594 2
        return $countries;
595
    }
596
597
    /**
598
     * Returns available branches for the given shipper and its service
599
     *
600
     * @param string      $shipper
601
     * @param string      $service
602
     * @param string|null $country
603
     *
604
     * @return \Generator|\Inspirum\Balikobot\Model\Values\PostCode[]
605
     *
606
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
607
     */
608 2
    public function getPostCodes(string $shipper, string $service, string $country = null): iterable
609
    {
610 2
        $postcodes = $this->client->getPostCodes($shipper, $service, $country);
611
612 2
        foreach ($postcodes as $postcode) {
613 1
            yield PostCode::newInstanceFromData($shipper, $service, $postcode);
614
        }
615 2
    }
616
617
    /**
618
     * Check package(s) data
619
     *
620
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
621
     *
622
     * @return void
623
     *
624
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
625
     */
626 1
    public function checkPackages(PackageCollection $packages): void
627
    {
628 1
        $this->client->checkPackages($packages->getShipper(), $packages->toArray());
629 1
    }
630
631
    /**
632
     * Returns available manipulation units for the given shipper
633
     *
634
     * @param string $shipper
635
     *
636
     * @return string[]
637
     *
638
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
639
     */
640 2
    public function getAdrUnits(string $shipper): array
641
    {
642 2
        $units = $this->client->getAdrUnits($shipper);
643
644 2
        return $units;
645
    }
646
647
    /**
648
     * Returns available activated services for the given shipper
649
     *
650
     * @param string $shipper
651
     *
652
     * @return array
653
     *
654
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
655
     */
656 2
    public function getActivatedServices(string $shipper): array
657
    {
658 2
        $units = $this->client->getActivatedServices($shipper);
659
660 2
        return $units;
661
    }
662
663
    /**
664
     * Order shipments from place B (typically supplier / previous consignee) to place A (shipping point)
665
     *
666
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
667
     *
668
     * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[]
669
     *
670
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
671
     */
672 3
    public function orderB2AShipment(PackageCollection $packages): OrderedPackageCollection
673
    {
674 3
        $response = $this->client->orderB2AShipment($packages->getShipper(), $packages->toArray());
675
676
        // create return value object
677 3
        $orderedPackages = new OrderedPackageCollection();
678
679 3
        foreach ($response as $i => $package) {
680 3
            $orderedPackages->add(OrderedPackage::newInstanceFromData(
681 3
                $packages->getShipper(),
682 3
                $packages->getEID(),
683 3
                $package
684
            ));
685
        }
686
687 3
        return $orderedPackages;
688
    }
689
690
    /**
691
     * Get PDF link with signed consignment delivery document by the recipient
692
     *
693
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
694
     *
695
     * @return string
696
     *
697
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
698
     */
699 2
    public function getProofOfDelivery(OrderedPackage $package): string
700
    {
701 2
        $packages = new OrderedPackageCollection();
702 2
        $packages->add($package);
703
704 2
        $response = $this->getProofOfDeliveries($packages);
705
706 2
        return $response[0];
707
    }
708
709
    /**
710
     * Get array of PDF links with signed consignment delivery document by the recipient
711
     *
712
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
713
     *
714
     * @return string[]
715
     *
716
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
717
     */
718 4
    public function getProofOfDeliveries(OrderedPackageCollection $packages): array
719
    {
720 4
        $response = $this->client->getProofOfDeliveries($packages->getShipper(), $packages->getCarrierIds());
721
722 4
        return $response;
723
    }
724
}
725