-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDefaultPackageService.php
228 lines (185 loc) · 8.66 KB
/
DefaultPackageService.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
declare(strict_types=1);
namespace Inspirum\Balikobot\Service;
use DateTimeInterface;
use Inspirum\Balikobot\Client\Client;
use Inspirum\Balikobot\Definitions\Method;
use Inspirum\Balikobot\Definitions\Version;
use Inspirum\Balikobot\Exception\BadRequestException;
use Inspirum\Balikobot\Model\Label\LabelFactory;
use Inspirum\Balikobot\Model\OrderedShipment\OrderedShipment;
use Inspirum\Balikobot\Model\OrderedShipment\OrderedShipmentFactory;
use Inspirum\Balikobot\Model\Package\Package;
use Inspirum\Balikobot\Model\Package\PackageCollection;
use Inspirum\Balikobot\Model\Package\PackageFactory;
use Inspirum\Balikobot\Model\PackageData\PackageData;
use Inspirum\Balikobot\Model\PackageData\PackageDataCollection;
use Inspirum\Balikobot\Model\PackageData\PackageDataFactory;
use Inspirum\Balikobot\Model\ProofOfDelivery\ProofOfDeliveryFactory;
use Inspirum\Balikobot\Model\TransportCost\TransportCostCollection;
use Inspirum\Balikobot\Model\TransportCost\TransportCostFactory;
use function array_key_exists;
use function array_map;
use function count;
use function sprintf;
final class DefaultPackageService implements PackageService
{
public function __construct(
private readonly Client $client,
private readonly PackageDataFactory $packageDataFactory,
private readonly PackageFactory $packageFactory,
private readonly OrderedShipmentFactory $orderedShipmentFactory,
private readonly LabelFactory $labelFactory,
private readonly ProofOfDeliveryFactory $proofOfDeliveryFactory,
private readonly TransportCostFactory $transportCostFactory,
) {
}
public function addPackages(PackageDataCollection $packages): PackageCollection
{
$response = $this->client->call(Version::V2V1, $packages->getCarrier(), Method::ADD, ['packages' => $packages->__toArray()]);
return $this->packageFactory->createCollection($packages->getCarrier(), $packages->__toArray(), $response);
}
public function dropPackages(PackageCollection $packages): void
{
$this->dropPackagesByPackageIds($packages->getCarrier(), $packages->getPackageIds());
}
public function dropPackage(Package $package): void
{
$this->dropPackageByPackageId($package->getCarrier(), $package->getPackageId());
}
public function dropPackageByPackageId(string $carrier, string $packageId): void
{
$this->dropPackagesByPackageIds($carrier, [$packageId]);
}
/** @inheritDoc */
public function dropPackagesByPackageIds(string $carrier, array $packageIds): void
{
if (count($packageIds) > 0) {
$this->client->call(Version::V2V1, $carrier, Method::DROP, ['package_ids' => $packageIds]);
}
}
public function orderShipment(PackageCollection $packages): OrderedShipment
{
return $this->orderShipmentByPackageIds($packages->getCarrier(), $packages->getPackageIds());
}
/** @inheritDoc */
public function orderShipmentByPackageIds(string $carrier, array $packageIds): OrderedShipment
{
$response = $this->client->call(Version::V2V1, $carrier, Method::ORDER, ['package_ids' => $packageIds]);
return $this->orderedShipmentFactory->create($carrier, $packageIds, $response);
}
public function getOrder(string $carrier, string $orderId): OrderedShipment
{
$response = $this->client->call(Version::V2V1, $carrier, Method::ORDER_VIEW, path: $orderId, shouldHaveStatus: false);
return $this->orderedShipmentFactory->create($carrier, $response['package_ids'], $response);
}
public function getOverview(string $carrier): PackageCollection
{
$response = $this->client->call(Version::V2V1, $carrier, Method::OVERVIEW, shouldHaveStatus: false);
return $this->packageFactory->createCollection($carrier, null, $response);
}
public function getLabels(PackageCollection $packages): string
{
return $this->getLabelsByPackageIds($packages->getCarrier(), $packages->getPackageIds());
}
/** @inheritDoc */
public function getLabelsByPackageIds(string $carrier, array $packageIds): string
{
$response = $this->client->call(Version::V2V1, $carrier, Method::LABELS, ['package_ids' => $packageIds]);
return $this->labelFactory->create($response);
}
public function getPackageInfo(Package $package): PackageData
{
$packageData = $this->getPackageInfoByCarrierId($package->getCarrier(), $package->getCarrierId());
$packageData->setEID($package->getBatchId());
return $packageData;
}
public function getPackageInfoByPackageId(string $carrier, string $packageId): PackageData
{
$response = $this->client->call(Version::V2V1, $carrier, Method::PACKAGE, path: $packageId, shouldHaveStatus: false);
return $this->packageDataFactory->create($response);
}
public function getPackageInfoByCarrierId(string $carrier, string $carrierId): PackageData
{
$response = $this->client->call(
Version::V2V1,
$carrier,
Method::PACKAGE,
path: sprintf('carrier_id/%s', $carrierId),
shouldHaveStatus: false,
);
return $this->packageDataFactory->create($response);
}
public function checkPackages(PackageDataCollection $packages): void
{
$this->client->call(Version::V2V1, $packages->getCarrier(), Method::CHECK, ['packages' => $packages->__toArray()]);
}
public function getProofOfDelivery(Package $package): string
{
return $this->getProofOfDeliveryByCarrierId($package->getCarrier(), $package->getCarrierId());
}
/** @inheritDoc */
public function getProofOfDeliveries(PackageCollection $packages): array
{
return $this->getProofOfDeliveriesByCarrierIds($packages->getCarrier(), $packages->getCarrierIds());
}
public function getProofOfDeliveryByCarrierId(string $carrier, string $carrierId): string
{
return $this->getProofOfDeliveriesByCarrierIds($carrier, [$carrierId])[0];
}
/** @inheritDoc */
public function getProofOfDeliveriesByCarrierIds(string $carrier, array $carrierIds): array
{
$response = $this->client->call(
Version::V1V1,
$carrier,
Method::PROOF_OF_DELIVERY,
array_map(static fn (string $carrierId): array => ['id' => $carrierId], $carrierIds),
shouldHaveStatus: false,
);
return $this->proofOfDeliveryFactory->create($carrierIds, $response);
}
public function getTransportCosts(PackageDataCollection $packages): TransportCostCollection
{
$response = $this->client->call(Version::V2V1, $packages->getCarrier(), Method::TRANSPORT_COSTS, ['packages' => $packages->__toArray()]);
return $this->transportCostFactory->createCollection($packages->getCarrier(), $packages->__toArray(), $response);
}
public function orderB2AShipment(PackageDataCollection $packages): PackageCollection
{
$response = $this->client->call(Version::V2V1, $packages->getCarrier(), Method::B2A, ['packages' => $packages->__toArray()]);
return $this->packageFactory->createCollection($packages->getCarrier(), $packages->__toArray(), $response);
}
public function orderPickup(
string $carrier,
DateTimeInterface $dateFrom,
DateTimeInterface $dateTo,
float $weight,
int $packageCount,
?string $message = null,
): void {
$response = $this->client->call(Version::V2V1, $carrier, Method::ORDER_PICKUP, [
'date' => $dateFrom->format('Y-m-d'),
'time_from' => $dateFrom->format('H:i'),
'time_to' => $dateTo->format('H:i'),
'weight' => $weight,
'package_count' => $packageCount,
'message' => $message,
]);
if (array_key_exists('message', $response)) {
throw new BadRequestException($response, 400, null, $response['message']);
}
}
public function orderB2CShipment(PackageDataCollection $packages): PackageCollection
{
$response = $this->client->call(Version::V2V1, $packages->getCarrier(), Method::B2C, ['packages' => $packages->__toArray()]);
return $this->packageFactory->createCollection($packages->getCarrier(), $packages->__toArray(), $response);
}
public function checkB2APackages(PackageDataCollection $packages): void
{
$this->client->call(Version::V2V1, $packages->getCarrier(), Method::B2A_CHECK, ['packages' => $packages->__toArray()]);
}
public function checkB2CPackages(PackageDataCollection $packages): void
{
$this->client->call(Version::V2V1, $packages->getCarrier(), Method::B2C_CHECK, ['packages' => $packages->__toArray()]);
}
}