Completed
Push — master ( cb1d4b...ee462c )
by Tomáš
03:34
created

Balikobot::getBranchesForLocation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 2
nc 2
nop 8
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Inspirum\Balikobot\Services;
4
5
use Inspirum\Balikobot\Contracts\RequesterInterface;
6
use Inspirum\Balikobot\Definitions\Option;
7
use Inspirum\Balikobot\Definitions\Shipper;
8
use Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection;
9
use Inspirum\Balikobot\Model\Aggregates\PackageCollection;
10
use Inspirum\Balikobot\Model\Aggregates\PackageTransportCostCollection;
11
use Inspirum\Balikobot\Model\Values\Branch;
12
use Inspirum\Balikobot\Model\Values\Country;
13
use Inspirum\Balikobot\Model\Values\OrderedPackage;
14
use Inspirum\Balikobot\Model\Values\OrderedShipment;
15
use Inspirum\Balikobot\Model\Values\Package;
16
use Inspirum\Balikobot\Model\Values\PackageStatus;
17
use Inspirum\Balikobot\Model\Values\PackageTransportCost;
18
use Inspirum\Balikobot\Model\Values\PostCode;
19
20
class Balikobot
21
{
22
    /**
23
     * Balikobot API
24
     *
25
     * @var \Inspirum\Balikobot\Services\Client
26
     */
27
    private $client;
28
29
    /**
30
     * Balikobot constructor
31
     *
32
     * @param \Inspirum\Balikobot\Contracts\RequesterInterface $requester
33
     */
34 76
    public function __construct(RequesterInterface $requester)
35
    {
36 76
        $this->client = new Client($requester);
37 76
    }
38
39
    /**
40
     * All supported shipper services
41
     *
42
     * @return array<string>
43
     */
44 1
    public function getShippers(): array
45
    {
46 1
        return Shipper::all();
47
    }
48
49
    /**
50
     * Add packages
51
     *
52
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
53
     *
54
     * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[]
55
     *
56
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
57
     */
58 8
    public function addPackages(PackageCollection $packages): OrderedPackageCollection
59
    {
60 8
        $usedRequestVersion = Shipper::resolveAddRequestVersion($packages->getShipper(), $packages->toArray());
61 8
        $labelsUrl          = null;
62
63 8
        $response = $this->client->addPackages(
64 8
            $packages->getShipper(),
65 8
            $packages->toArray(),
66
            $usedRequestVersion,
67
            $labelsUrl
68
        );
69
70 8
        $orderedPackages = new OrderedPackageCollection();
71 8
        $orderedPackages->setLabelsUrl($labelsUrl);
72
73 8
        foreach ($response as $i => $package) {
74 8
            $orderedPackage = OrderedPackage::newInstanceFromData(
75 8
                $packages->getShipper(),
76 8
                $packages->offsetGet($i)->getEID(),
77
                $package
78
            );
79 8
            $orderedPackages->add($orderedPackage);
80
        }
81
82 8
        return $orderedPackages;
83
    }
84
85
    /**
86
     * Exports order into Balikobot system
87
     *
88
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
89
     *
90
     * @return void
91
     *
92
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
93
     */
94 1
    public function dropPackages(OrderedPackageCollection $packages): void
95
    {
96 1
        $this->client->dropPackages($packages->getShipper(), $packages->getPackageIds());
97 1
    }
98
99
    /**
100
     * Exports order into Balikobot system
101
     *
102
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
103
     *
104
     * @return void
105
     *
106
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
107
     */
108 1
    public function dropPackage(OrderedPackage $package): void
109
    {
110 1
        $this->client->dropPackage($package->getShipper(), $package->getPackageId());
111 1
    }
112
113
    /**
114
     * Order shipment
115
     *
116
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
117
     *
118
     * @return \Inspirum\Balikobot\Model\Values\OrderedShipment
119
     *
120
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
121
     */
122 2
    public function orderShipment(OrderedPackageCollection $packages): OrderedShipment
123
    {
124 2
        $response = $this->client->orderShipment($packages->getShipper(), $packages->getPackageIds());
125
126 2
        $orderedShipment = OrderedShipment::newInstanceFromData(
127 2
            $packages->getShipper(),
128 2
            $packages->getPackageIds(),
129
            $response
130
        );
131
132 2
        return $orderedShipment;
133
    }
134
135
    /**
136
     * Track package
137
     *
138
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
139
     *
140
     * @return array<\Inspirum\Balikobot\Model\Values\PackageStatus>|\Inspirum\Balikobot\Model\Values\PackageStatus[]
141
     *
142
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
143
     */
144 3
    public function trackPackage(OrderedPackage $package): array
145
    {
146 3
        $packages = new OrderedPackageCollection();
147 3
        $packages->add($package);
148
149 3
        $response = $this->trackPackages($packages);
150
151 2
        return $response[0];
152
    }
153
154
    /**
155
     * Track packages
156
     *
157
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
158
     *
159
     * @return array<array<\Inspirum\Balikobot\Model\Values\PackageStatus>>|\Inspirum\Balikobot\Model\Values\PackageStatus[][]
160
     *
161
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
162
     */
163 5
    public function trackPackages(OrderedPackageCollection $packages): array
164
    {
165 5
        $response = $this->client->trackPackages($packages->getShipper(), $packages->getCarrierIds());
166
167 4
        $statuses = [];
168
169 4
        foreach ($response as $i => $responseStatuses) {
170 4
            $statuses[$i] = [];
171
172 4
            foreach ($responseStatuses as $status) {
173 4
                $statuses[$i][] = PackageStatus::newInstanceFromData($status);
174
            }
175
        }
176
177 4
        return $statuses;
178
    }
179
180
    /**
181
     * Track package last status
182
     *
183
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
184
     *
185
     * @return \Inspirum\Balikobot\Model\Values\PackageStatus
186
     *
187
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
188
     */
189 2
    public function trackPackageLastStatus(OrderedPackage $package): PackageStatus
190
    {
191 2
        $packages = new OrderedPackageCollection();
192 2
        $packages->add($package);
193
194 2
        $response = $this->trackPackagesLastStatus($packages);
195
196 2
        return $response[0];
197
    }
198
199
    /**
200
     * Track package last status
201
     *
202
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
203
     *
204
     * @return array<\Inspirum\Balikobot\Model\Values\PackageStatus>|\Inspirum\Balikobot\Model\Values\PackageStatus[]
205
     *
206
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
207
     */
208 4
    public function trackPackagesLastStatus(OrderedPackageCollection $packages): array
209
    {
210 4
        $response = $this->client->trackPackagesLastStatus($packages->getShipper(), $packages->getCarrierIds());
211
212 4
        $statuses = [];
213
214 4
        foreach ($response as $status) {
215 4
            $statuses[] = PackageStatus::newInstanceFromData($status);
216
        }
217
218 4
        return $statuses;
219
    }
220
221
    /**
222
     * Get overview for given shipper
223
     *
224
     * @param string $shipper
225
     *
226
     * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[]
227
     *
228
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
229
     */
230 2
    public function getOverview(string $shipper): OrderedPackageCollection
231
    {
232 2
        $response = $this->client->getOverview($shipper);
233
234 2
        $orderedPackages = new OrderedPackageCollection();
235
236 2
        foreach ($response as $package) {
237 1
            $orderedPackage = OrderedPackage::newInstanceFromData(
238 1
                $shipper,
239 1
                (string) $package['eshop_id'],
240
                $package
241
            );
242 1
            $orderedPackages->add($orderedPackage);
243
        }
244
245 2
        return $orderedPackages;
246
    }
247
248
    /**
249
     * Get labels for orders
250
     *
251
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
252
     *
253
     * @return string
254
     *
255
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
256
     */
257 2
    public function getLabels(OrderedPackageCollection $packages): string
258
    {
259 2
        $labelUrl = $this->client->getLabels($packages->getShipper(), $packages->getPackageIds());
260
261 2
        return $labelUrl;
262
    }
263
264
    /**
265
     * Gets complete information about a package
266
     *
267
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
268
     *
269
     * @return \Inspirum\Balikobot\Model\Values\Package
270
     *
271
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
272
     */
273 2
    public function getPackageInfo(OrderedPackage $package): Package
274
    {
275 2
        $response = $this->client->getPackageInfo($package->getShipper(), $package->getPackageId());
276
277 2
        unset($response['package_id']);
278 2
        unset($response['eshop_id']);
279 2
        unset($response['carrier_id']);
280 2
        unset($response['track_url']);
281 2
        unset($response['label_url']);
282 2
        unset($response['carrier_id_swap']);
283 2
        unset($response['pieces']);
284
285 2
        $options              = $response;
286 2
        $options[Option::EID] = $package->getBatchId();
287
288 2
        $package = new Package($options);
289
290 2
        return $package;
291
    }
292
293
    /**
294
     * Gets complete information about a package
295
     *
296
     * @param string $shipper
297
     * @param int    $orderId
298
     *
299
     * @return \Inspirum\Balikobot\Model\Values\OrderedShipment
300
     *
301
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
302
     */
303 2
    public function getOrder(string $shipper, int $orderId): OrderedShipment
304
    {
305 2
        $response = $this->client->getOrder($shipper, $orderId);
306
307 2
        $orderedShipment = OrderedShipment::newInstanceFromData(
308 2
            $shipper,
309 2
            $response['package_ids'],
310
            $response
311
        );
312
313 2
        return $orderedShipment;
314
    }
315
316
    /**
317
     * Returns available services for the given shipper
318
     *
319
     * @param string      $shipper
320
     * @param string|null $country
321
     *
322
     * @return array<string,string>
323
     *
324
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
325
     */
326 5
    public function getServices(string $shipper, string $country = null): array
327
    {
328 5
        $usedRequestVersion = Shipper::resolveServicesRequestVersion($shipper);
329
330 5
        $services = $this->client->getServices($shipper, $country, $usedRequestVersion);
331
332 4
        return $services;
333
    }
334
335
    /**
336
     * Returns available B2A services for the given shipper
337
     *
338
     * @param string $shipper
339
     *
340
     * @return array<string,string>
341
     *
342
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
343
     */
344 2
    public function getB2AServices(string $shipper): array
345
    {
346 2
        $services = $this->client->getB2AServices($shipper);
347
348 2
        return $services;
349
    }
350
351
    /**
352
     * Returns all manipulation units for the given shipper
353
     *
354
     * @param string $shipper
355
     * @param bool   $fullData
356
     *
357
     * @return array<string|array>
358
     *
359
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
360
     */
361 2
    public function getManipulationUnits(string $shipper, bool $fullData = false): array
362
    {
363 2
        $units = $this->client->getManipulationUnits($shipper, $fullData);
364
365 2
        return $units;
366
    }
367
368
    /**
369
     * Returns available manipulation units for the given shipper
370
     *
371
     * @param string $shipper
372
     * @param bool   $fullData
373
     *
374
     * @return array<string|array>
375
     *
376
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
377
     */
378 2
    public function getActivatedManipulationUnits(string $shipper, bool $fullData = false): array
379
    {
380 2
        $units = $this->client->getActivatedManipulationUnits($shipper, $fullData);
381
382 2
        return $units;
383
    }
384
385
    /**
386
     * Get all available branches
387
     *
388
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
389
     *
390
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
391
     */
392 1
    public function getBranches(): iterable
393
    {
394 1
        $shippers = $this->getShippers();
395
396 1
        foreach ($shippers as $shipper) {
397 1
            yield from $this->getBranchesForShipper($shipper);
398
        }
399 1
    }
400
401
    /**
402
     * Get all available branches for countries
403
     *
404
     * @param array<string> $countries
405
     *
406
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
407
     *
408
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
409
     */
410 1
    public function getBranchesForCountries(array $countries): iterable
411
    {
412 1
        $shippers = $this->getShippers();
413
414 1
        foreach ($shippers as $shipper) {
415 1
            yield from $this->getBranchesForShipperForCountries($shipper, $countries);
416
        }
417 1
    }
418
419
    /**
420
     * Get all available branches for given shipper
421
     *
422
     * @param string $shipper
423
     *
424
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
425
     *
426
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
427
     */
428 2
    public function getBranchesForShipper(string $shipper): iterable
429
    {
430 2
        $services = $this->getServicesForShipper($shipper);
431
432 2
        foreach ($services as $service) {
433 2
            yield from $this->getBranchesForShipperService($shipper, $service);
434
        }
435 2
    }
436
437
    /**
438
     * Get all available branches for given shipper for countries
439
     *
440
     * @param string        $shipper
441
     * @param array<string> $countries
442
     *
443
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
444
     *
445
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
446
     */
447 2
    public function getBranchesForShipperForCountries(string $shipper, array $countries): iterable
448
    {
449 2
        $services = $this->getServicesForShipper($shipper);
450
451 2
        foreach ($services as $service) {
452 2
            yield from $this->getBranchesForShipperServiceForCountries($shipper, $service, $countries);
453
        }
454 2
    }
455
456
    /**
457
     * Get services for shipper
458
     *
459
     * @param string $shipper
460
     *
461
     * @return array<string|null>
462
     *
463
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
464
     */
465 4
    private function getServicesForShipper(string $shipper): array
466
    {
467 4
        $services = array_keys($this->getServices($shipper)) ?: [null];
468
469 4
        return $services;
470
    }
471
472
    /**
473
     * Get all available branches for given shipper and service type
474
     *
475
     * @param string      $shipper
476
     * @param string|null $service
477
     * @param string|null $country
478
     *
479
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
480
     *
481
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
482
     */
483 4
    public function getBranchesForShipperService(string $shipper, ?string $service, string $country = null): iterable
484
    {
485 4
        $useFullBranchRequest = Shipper::hasFullBranchesSupport($shipper, $service);
486 4
        $usedRequestVersion   = Shipper::resolveBranchesRequestVersion($shipper, $service);
487
488 4
        $branches = $this->client->getBranches(
489 4
            $shipper,
490
            $service,
491
            $useFullBranchRequest,
492
            $country,
493
            $usedRequestVersion
494
        );
495
496 4
        foreach ($branches as $branch) {
497 2
            yield Branch::newInstanceFromData($shipper, $service, $branch);
498
        }
499 4
    }
500
501
    /**
502
     * Get all available branches for given shipper and service type for countries
503
     *
504
     * @param string        $shipper
505
     * @param string|null   $service
506
     * @param array<string> $countries
507
     *
508
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
509
     *
510
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
511
     */
512 3
    public function getBranchesForShipperServiceForCountries(
513
        string $shipper,
514
        ?string $service,
515
        array $countries
516
    ): iterable {
517 3
        $branches = $this->getAllBranchesForShipperServiceForCountries($shipper, $service, $countries);
518
519 3
        foreach ($branches as $branch) {
520 3
            if (in_array($branch->getCountry(), $countries)) {
521 3
                yield $branch;
522
            }
523
        }
524 3
    }
525
526
    /**
527
     * Get all available branches for given shipper and service type filtered by countries if possible
528
     *
529
     * @param string        $shipper
530
     * @param string|null   $service
531
     * @param array<string> $countries
532
     *
533
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
534
     *
535
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
536
     */
537 3
    private function getAllBranchesForShipperServiceForCountries(
538
        string $shipper,
539
        ?string $service,
540
        array $countries
541
    ): iterable {
542 3
        if (Shipper::hasBranchCountryFilterSupport($shipper) === false) {
543 2
            yield from $this->getBranchesForShipperService($shipper, $service);
544
545 2
            return;
546
        }
547
548 2
        foreach ($countries as $country) {
549 2
            yield from $this->getBranchesForShipperService($shipper, $service, $country);
550
        }
551 2
    }
552
553
    /**
554
     * Get all available branches for given shipper
555
     *
556
     * @param string      $shipper
557
     * @param string      $country
558
     * @param string      $city
559
     * @param string|null $postcode
560
     * @param string|null $street
561
     * @param int|null    $maxResults
562
     * @param float|null  $radius
563
     * @param string|null $type
564
     *
565
     * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[]
566
     *
567
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
568
     */
569 3
    public function getBranchesForLocation(
570
        string $shipper,
571
        string $country,
572
        string $city,
573
        string $postcode = null,
574
        string $street = null,
575
        int $maxResults = null,
576
        float $radius = null,
577
        string $type = null
578
    ): iterable {
579 3
        $branches = $this->client->getBranchesForLocation(
580 3
            $shipper,
581
            $country,
582
            $city,
583
            $postcode,
584
            $street,
585
            $maxResults,
586
            $radius,
587
            $type
588
        );
589
590 3
        foreach ($branches as $branch) {
591 1
            yield Branch::newInstanceFromData($shipper, null, $branch);
592
        }
593 3
    }
594
595
    /**
596
     * Returns list of countries where service with cash-on-delivery payment type is available in
597
     *
598
     * @param string $shipper
599
     *
600
     * @return array<array<int|string,array<string,array>>>
601
     *
602
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
603
     */
604 2
    public function getCodCountries(string $shipper): array
605
    {
606 2
        $countries = $this->client->getCodCountries($shipper);
607
608 2
        return $countries;
609
    }
610
611
    /**
612
     * Returns list of countries where service is available in
613
     *
614
     * @param string $shipper
615
     *
616
     * @return array<array<int|string,string>>
617
     *
618
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
619
     */
620 2
    public function getCountries(string $shipper): array
621
    {
622 2
        $countries = $this->client->getCountries($shipper);
623
624 2
        return $countries;
625
    }
626
627
    /**
628
     * Returns available branches for the given shipper and its service
629
     *
630
     * @param string      $shipper
631
     * @param string      $service
632
     * @param string|null $country
633
     *
634
     * @return \Generator|\Inspirum\Balikobot\Model\Values\PostCode[]
635
     *
636
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
637
     */
638 2
    public function getPostCodes(string $shipper, string $service, string $country = null): iterable
639
    {
640 2
        $postCodes = $this->client->getPostCodes($shipper, $service, $country);
641
642 2
        foreach ($postCodes as $postcode) {
643 1
            yield PostCode::newInstanceFromData($shipper, $service, $postcode);
644
        }
645 2
    }
646
647
    /**
648
     * Check package(s) data
649
     *
650
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
651
     *
652
     * @return void
653
     *
654
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
655
     */
656 1
    public function checkPackages(PackageCollection $packages): void
657
    {
658 1
        $this->client->checkPackages($packages->getShipper(), $packages->toArray());
659 1
    }
660
661
    /**
662
     * Returns available manipulation units for the given shipper
663
     *
664
     * @param string $shipper
665
     * @param bool   $fullData
666
     *
667
     * @return array<string|array>
668
     *
669
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
670
     */
671 2
    public function getAdrUnits(string $shipper, bool $fullData = false): array
672
    {
673 2
        $units = $this->client->getAdrUnits($shipper, $fullData);
674
675 2
        return $units;
676
    }
677
678
    /**
679
     * Returns available activated services for the given shipper
680
     *
681
     * @param string $shipper
682
     *
683
     * @return array<string,mixed>
684
     *
685
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
686
     */
687 2
    public function getActivatedServices(string $shipper): array
688
    {
689 2
        $services = $this->client->getActivatedServices($shipper);
690
691 2
        return $services;
692
    }
693
694
    /**
695
     * Order shipments from place B (typically supplier / previous consignee) to place A (shipping point)
696
     *
697
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
698
     *
699
     * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[]
700
     *
701
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
702
     */
703 3
    public function orderB2AShipment(PackageCollection $packages): OrderedPackageCollection
704
    {
705 3
        $response = $this->client->orderB2AShipment($packages->getShipper(), $packages->toArray());
706
707 3
        $orderedPackages = new OrderedPackageCollection();
708
709 3
        foreach ($response as $i => $package) {
710 3
            $orderedPackage = OrderedPackage::newInstanceFromData(
711 3
                $packages->getShipper(),
712 3
                $packages->offsetGet($i)->getEID(),
713
                $package
714
            );
715 3
            $orderedPackages->add($orderedPackage);
716
        }
717
718 3
        return $orderedPackages;
719
    }
720
721
    /**
722
     * Get PDF link with signed consignment delivery document by the recipient
723
     *
724
     * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package
725
     *
726
     * @return string
727
     *
728
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
729
     */
730 2
    public function getProofOfDelivery(OrderedPackage $package): string
731
    {
732 2
        $packages = new OrderedPackageCollection();
733 2
        $packages->add($package);
734
735 2
        $linkUrls = $this->getProofOfDeliveries($packages);
736
737 2
        return $linkUrls[0];
738
    }
739
740
    /**
741
     * Get array of PDF links with signed consignment delivery document by the recipient
742
     *
743
     * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages
744
     *
745
     * @return array<string>
746
     *
747
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
748
     */
749 4
    public function getProofOfDeliveries(OrderedPackageCollection $packages): array
750
    {
751 4
        $linkUrls = $this->client->getProofOfDeliveries($packages->getShipper(), $packages->getCarrierIds());
752
753 4
        return $linkUrls;
754
    }
755
756
    /**
757
     * Obtain the price of carriage at consignment level
758
     *
759
     * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages
760
     *
761
     * @return \Inspirum\Balikobot\Model\Aggregates\PackageTransportCostCollection|\Inspirum\Balikobot\Model\Values\PackageTransportCost[]
762
     *
763
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
764
     */
765 2
    public function getTransportCosts(PackageCollection $packages): PackageTransportCostCollection
766
    {
767 2
        $response = $this->client->getTransportCosts($packages->getShipper(), $packages->toArray());
768
769 2
        $transportCosts = new PackageTransportCostCollection($packages->getShipper());
770
771 2
        foreach ($response as $i => $package) {
772 2
            $transportCost = PackageTransportCost::newInstanceFromData($packages->getShipper(), $package);
773 2
            $transportCosts->add($transportCost);
774
        }
775
776 2
        return $transportCosts;
777
    }
778
779
    /**
780
     * Ģet information on individual countries of the world
781
     *
782
     * @return array<string,\Inspirum\Balikobot\Model\Values\Country>|\Inspirum\Balikobot\Model\Values\Country[]
783
     *
784
     * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface
785
     */
786 2
    public function getCountriesData(): array
787
    {
788 2
        $response = $this->client->getCountriesData();
789
790 2
        $countries = [];
791
792 2
        foreach ($response as $code => $country) {
793 1
            $countries[$code] = Country::newInstanceFromData($country);
794
        }
795
796 2
        return $countries;
797
    }
798
}
799