|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Inspirum\Balikobot\Service; |
|
6
|
|
|
|
|
7
|
|
|
use Inspirum\Balikobot\Client\Client; |
|
8
|
|
|
use Inspirum\Balikobot\Definitions\Method; |
|
9
|
|
|
use Inspirum\Balikobot\Definitions\Version; |
|
10
|
|
|
use Inspirum\Balikobot\Model\Package\Package; |
|
11
|
|
|
use Inspirum\Balikobot\Model\Package\PackageCollection; |
|
12
|
|
|
use Inspirum\Balikobot\Model\Status\Status; |
|
13
|
|
|
use Inspirum\Balikobot\Model\Status\StatusCollection; |
|
14
|
|
|
use Inspirum\Balikobot\Model\Status\StatusFactory; |
|
15
|
|
|
use Inspirum\Balikobot\Model\Status\Statuses; |
|
16
|
|
|
use Inspirum\Balikobot\Model\Status\StatusesCollection; |
|
17
|
|
|
use OutOfBoundsException; |
|
18
|
|
|
|
|
19
|
|
|
final class DefaultTrackService implements TrackService |
|
20
|
|
|
{ |
|
21
|
6 |
|
public function __construct( |
|
22
|
|
|
private readonly Client $client, |
|
23
|
|
|
private readonly StatusFactory $statusFactory, |
|
24
|
|
|
) { |
|
25
|
6 |
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
public function trackPackage(Package $package): Statuses |
|
28
|
|
|
{ |
|
29
|
1 |
|
return $this->trackPackageById($package->getCarrier(), $package->getCarrierId()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
public function trackPackageById(string $carrier, string $carrierId): Statuses |
|
33
|
|
|
{ |
|
34
|
1 |
|
return $this->trackPackagesByIds($carrier, [$carrierId])->getForCarrierId($carrierId) ?? throw new OutOfBoundsException(); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
public function trackPackages(PackageCollection $packages): StatusesCollection |
|
38
|
|
|
{ |
|
39
|
2 |
|
return $this->trackPackagesByIds($packages->getCarrier(), $packages->getCarrierIds()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** @inheritDoc */ |
|
43
|
3 |
|
public function trackPackagesByIds(string $carrier, array $carrierIds): StatusesCollection |
|
44
|
|
|
{ |
|
45
|
3 |
|
$response = $this->client->call( |
|
46
|
3 |
|
Version::V2V2, |
|
47
|
3 |
|
$carrier, |
|
48
|
3 |
|
Method::TRACK, |
|
49
|
3 |
|
['carrier_ids' => $carrierIds], |
|
50
|
3 |
|
shouldHaveStatus: false, |
|
51
|
3 |
|
); |
|
52
|
|
|
|
|
53
|
3 |
|
return $this->statusFactory->createCollection($carrier, $carrierIds, $response); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
public function trackPackageLastStatus(Package $package): Status |
|
57
|
|
|
{ |
|
58
|
1 |
|
return $this->trackPackageLastStatusById($package->getCarrier(), $package->getCarrierId()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
public function trackPackageLastStatusById(string $carrier, string $carrierId): Status |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->trackPackagesLastStatusesByIds($carrier, [$carrierId])->getForCarrierId($carrierId) ?? throw new OutOfBoundsException(); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
public function trackPackagesLastStatuses(PackageCollection $packages): StatusCollection |
|
67
|
|
|
{ |
|
68
|
2 |
|
return $this->trackPackagesLastStatusesByIds($packages->getCarrier(), $packages->getCarrierIds()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** @inheritDoc */ |
|
72
|
3 |
|
public function trackPackagesLastStatusesByIds(string $carrier, array $carrierIds): StatusCollection |
|
73
|
|
|
{ |
|
74
|
3 |
|
$response = $this->client->call( |
|
75
|
3 |
|
Version::V2V1, |
|
76
|
3 |
|
$carrier, |
|
77
|
3 |
|
Method::TRACK_STATUS, |
|
78
|
3 |
|
['carrier_ids' => $carrierIds], |
|
79
|
3 |
|
shouldHaveStatus: false, |
|
80
|
3 |
|
); |
|
81
|
|
|
|
|
82
|
3 |
|
return $this->statusFactory->createLastStatusCollection($carrier, $carrierIds, $response); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|