-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDefaultTrackService.php
122 lines (102 loc) · 3.56 KB
/
DefaultTrackService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
namespace Inspirum\Balikobot\Service;
use Inspirum\Balikobot\Client\Client;
use Inspirum\Balikobot\Definitions\Request;
use Inspirum\Balikobot\Definitions\Version;
use Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection;
use Inspirum\Balikobot\Model\PackageStatus;
use Inspirum\Balikobot\Model\PackageStatusFactory;
use Inspirum\Balikobot\Model\Values\OrderedPackage;
use Inspirum\Balikobot\Response\Validator;
use function count;
class DefaultTrackService implements TrackService
{
public function __construct(
private Client $client,
private Validator $validator,
private PackageStatusFactory $packageStatusFactory,
) {
}
/**
* @inheritDoc
*/
public function trackPackage(OrderedPackage $package): array
{
return $this->trackPackageById($package->carrier, $package->carrierId);
}
/**
* @inheritDoc
*/
public function trackPackageById(string $carrier, string $carrierId): array
{
return $this->trackPackagesByIds($carrier, [$carrierId])[0];
}
/**
* @inheritDoc
*/
public function trackPackages(OrderedPackageCollection $packages): array
{
return $this->trackPackagesByIds($packages->getCarrier(), $packages->getCarrierIds());
}
/**
* @inheritDoc
*/
public function trackPackagesByIds(string $carrier, array $carrierIds): array
{
$response = $this->client->call(
Version::V2V2,
$carrier,
Request::TRACK,
['carrier_ids' => $carrierIds],
shouldHaveStatus: false,
);
$packages = $response['packages'] ?? [];
$this->validator->validateIndexes($packages, count($carrierIds));
$statuses = [];
foreach ($packages as $packageIndex => $package) {
$this->validator->validateResponseStatus($package, $response);
$statuses[(int) $packageIndex] = [];
foreach ($package['states'] ?? [] as $status) {
$statuses[(int) $packageIndex][] = $this->packageStatusFactory->createFromStatusData($status, $response);
}
}
return $statuses;
}
public function trackPackageLastStatus(OrderedPackage $package): PackageStatus
{
return $this->trackPackageLastStatusById($package->carrier, $package->carrierId);
}
public function trackPackageLastStatusById(string $carrier, string $carrierId): PackageStatus
{
return $this->trackPackagesLastStatusesByIds($carrier, [$carrierId])[0];
}
/**
* @inheritDoc
*/
public function trackPackagesLastStatuses(OrderedPackageCollection $packages): array
{
return $this->trackPackagesLastStatusesByIds($packages->getCarrier(), $packages->getCarrierIds());
}
/**
* @inheritDoc
*/
public function trackPackagesLastStatusesByIds(string $carrier, array $carrierIds): array
{
$response = $this->client->call(
Version::V2V2,
$carrier,
Request::TRACK_STATUS,
['carrier_ids' => $carrierIds],
shouldHaveStatus: false,
);
$packages = $response['packages'] ?? [];
$this->validator->validateIndexes($packages, count($carrierIds));
$statuses = [];
foreach ($packages as $packageIndex => $status) {
$this->validator->validateResponseStatus($status, $response);
$statuses[(int) $packageIndex] = $this->packageStatusFactory->createFromLastStatusData($status, $response);
}
return $statuses;
}
}