Completed
Push — master ( c19848...6bc0c5 )
by Tomáš
03:30
created

Balikobot::getB2AServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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