-
Notifications
You must be signed in to change notification settings - Fork 12
/
DefaultTrackService.php
84 lines (70 loc) · 2.82 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
<?php
declare(strict_types=1);
namespace Inspirum\Balikobot\Service;
use Inspirum\Balikobot\Client\Client;
use Inspirum\Balikobot\Definitions\Method;
use Inspirum\Balikobot\Definitions\Version;
use Inspirum\Balikobot\Model\Package\Package;
use Inspirum\Balikobot\Model\Package\PackageCollection;
use Inspirum\Balikobot\Model\Status\Status;
use Inspirum\Balikobot\Model\Status\StatusCollection;
use Inspirum\Balikobot\Model\Status\StatusFactory;
use Inspirum\Balikobot\Model\Status\Statuses;
use Inspirum\Balikobot\Model\Status\StatusesCollection;
use OutOfBoundsException;
final class DefaultTrackService implements TrackService
{
public function __construct(
private readonly Client $client,
private readonly StatusFactory $statusFactory,
) {
}
public function trackPackage(Package $package): Statuses
{
return $this->trackPackageById($package->getCarrier(), $package->getCarrierId());
}
public function trackPackageById(string $carrier, string $carrierId): Statuses
{
return $this->trackPackagesByIds($carrier, [$carrierId])->getForCarrierId($carrierId) ?? throw new OutOfBoundsException();
}
public function trackPackages(PackageCollection $packages): StatusesCollection
{
return $this->trackPackagesByIds($packages->getCarrier(), $packages->getCarrierIds());
}
/** @inheritDoc */
public function trackPackagesByIds(string $carrier, array $carrierIds): StatusesCollection
{
$response = $this->client->call(
Version::V2V2,
$carrier,
Method::TRACK,
['carrier_ids' => $carrierIds],
shouldHaveStatus: false,
);
return $this->statusFactory->createCollection($carrier, $carrierIds, $response);
}
public function trackPackageLastStatus(Package $package): Status
{
return $this->trackPackageLastStatusById($package->getCarrier(), $package->getCarrierId());
}
public function trackPackageLastStatusById(string $carrier, string $carrierId): Status
{
return $this->trackPackagesLastStatusesByIds($carrier, [$carrierId])->getForCarrierId($carrierId) ?? throw new OutOfBoundsException();
}
public function trackPackagesLastStatuses(PackageCollection $packages): StatusCollection
{
return $this->trackPackagesLastStatusesByIds($packages->getCarrier(), $packages->getCarrierIds());
}
/** @inheritDoc */
public function trackPackagesLastStatusesByIds(string $carrier, array $carrierIds): StatusCollection
{
$response = $this->client->call(
Version::V2V1,
$carrier,
Method::TRACK_STATUS,
['carrier_ids' => $carrierIds],
shouldHaveStatus: false,
);
return $this->statusFactory->createLastStatusCollection($carrier, $carrierIds, $response);
}
}