|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Inspirum\Balikobot\Service; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeInterface; |
|
8
|
|
|
use Inspirum\Balikobot\Client\Client; |
|
9
|
|
|
use Inspirum\Balikobot\Definitions\Method; |
|
10
|
|
|
use Inspirum\Balikobot\Definitions\Version; |
|
11
|
|
|
use Inspirum\Balikobot\Exception\BadRequestException; |
|
12
|
|
|
use Inspirum\Balikobot\Model\Label\LabelFactory; |
|
13
|
|
|
use Inspirum\Balikobot\Model\OrderedShipment\OrderedShipment; |
|
14
|
|
|
use Inspirum\Balikobot\Model\OrderedShipment\OrderedShipmentFactory; |
|
15
|
|
|
use Inspirum\Balikobot\Model\Package\Package; |
|
16
|
|
|
use Inspirum\Balikobot\Model\Package\PackageCollection; |
|
17
|
|
|
use Inspirum\Balikobot\Model\Package\PackageFactory; |
|
18
|
|
|
use Inspirum\Balikobot\Model\PackageData\PackageData; |
|
19
|
|
|
use Inspirum\Balikobot\Model\PackageData\PackageDataCollection; |
|
20
|
|
|
use Inspirum\Balikobot\Model\PackageData\PackageDataFactory; |
|
21
|
|
|
use Inspirum\Balikobot\Model\ProofOfDelivery\ProofOfDeliveryFactory; |
|
22
|
|
|
use Inspirum\Balikobot\Model\TransportCost\TransportCostCollection; |
|
23
|
|
|
use Inspirum\Balikobot\Model\TransportCost\TransportCostFactory; |
|
24
|
|
|
use function array_key_exists; |
|
25
|
|
|
use function array_map; |
|
26
|
|
|
use function count; |
|
27
|
|
|
use function sprintf; |
|
28
|
|
|
|
|
29
|
|
|
final class DefaultPackageService implements PackageService |
|
30
|
|
|
{ |
|
31
|
56 |
|
public function __construct( |
|
32
|
|
|
private readonly Client $client, |
|
33
|
|
|
private readonly PackageDataFactory $packageDataFactory, |
|
34
|
|
|
private readonly PackageFactory $packageFactory, |
|
35
|
|
|
private readonly OrderedShipmentFactory $orderedShipmentFactory, |
|
36
|
|
|
private readonly LabelFactory $labelFactory, |
|
37
|
|
|
private readonly ProofOfDeliveryFactory $proofOfDeliveryFactory, |
|
38
|
|
|
private readonly TransportCostFactory $transportCostFactory, |
|
39
|
|
|
) { |
|
40
|
56 |
|
} |
|
41
|
|
|
|
|
42
|
9 |
|
public function addPackages(PackageDataCollection $packages): PackageCollection |
|
43
|
|
|
{ |
|
44
|
9 |
|
$response = $this->client->call(Version::V2V1, $packages->getCarrier(), Method::ADD, ['packages' => $packages->__toArray()]); |
|
45
|
|
|
|
|
46
|
8 |
|
return $this->packageFactory->createCollection($packages->getCarrier(), $packages->__toArray(), $response); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
public function dropPackages(PackageCollection $packages): void |
|
50
|
|
|
{ |
|
51
|
2 |
|
$this->dropPackagesByPackageIds($packages->getCarrier(), $packages->getPackageIds()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
public function dropPackage(Package $package): void |
|
55
|
|
|
{ |
|
56
|
2 |
|
$this->dropPackageByPackageId($package->getCarrier(), $package->getPackageId()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
public function dropPackageByPackageId(string $carrier, string $packageId): void |
|
60
|
|
|
{ |
|
61
|
3 |
|
$this->dropPackagesByPackageIds($carrier, [$packageId]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @inheritDoc */ |
|
65
|
7 |
|
public function dropPackagesByPackageIds(string $carrier, array $packageIds): void |
|
66
|
|
|
{ |
|
67
|
7 |
|
if (count($packageIds) > 0) { |
|
68
|
6 |
|
$this->client->call(Version::V2V1, $carrier, Method::DROP, ['package_ids' => $packageIds]); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
4 |
|
public function orderShipment(PackageCollection $packages): OrderedShipment |
|
73
|
|
|
{ |
|
74
|
4 |
|
return $this->orderShipmentByPackageIds($packages->getCarrier(), $packages->getPackageIds()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** @inheritDoc */ |
|
78
|
5 |
|
public function orderShipmentByPackageIds(string $carrier, array $packageIds): OrderedShipment |
|
79
|
|
|
{ |
|
80
|
5 |
|
$response = $this->client->call(Version::V2V1, $carrier, Method::ORDER, ['package_ids' => $packageIds]); |
|
81
|
|
|
|
|
82
|
4 |
|
return $this->orderedShipmentFactory->create($carrier, $packageIds, $response); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
3 |
|
public function getOrder(string $carrier, string $orderId): OrderedShipment |
|
86
|
|
|
{ |
|
87
|
3 |
|
$response = $this->client->call(Version::V2V1, $carrier, Method::ORDER_VIEW, path: $orderId, shouldHaveStatus: false); |
|
88
|
|
|
|
|
89
|
2 |
|
return $this->orderedShipmentFactory->create($carrier, $response['package_ids'], $response); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
public function getOverview(string $carrier): PackageCollection |
|
93
|
|
|
{ |
|
94
|
2 |
|
$response = $this->client->call(Version::V2V1, $carrier, Method::OVERVIEW, shouldHaveStatus: false); |
|
95
|
|
|
|
|
96
|
2 |
|
return $this->packageFactory->createCollection($carrier, null, $response); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
public function getLabels(PackageCollection $packages): string |
|
100
|
|
|
{ |
|
101
|
3 |
|
return $this->getLabelsByPackageIds($packages->getCarrier(), $packages->getPackageIds()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** @inheritDoc */ |
|
105
|
4 |
|
public function getLabelsByPackageIds(string $carrier, array $packageIds): string |
|
106
|
|
|
{ |
|
107
|
4 |
|
$response = $this->client->call(Version::V2V1, $carrier, Method::LABELS, ['package_ids' => $packageIds]); |
|
108
|
|
|
|
|
109
|
3 |
|
return $this->labelFactory->create($response); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
3 |
|
public function getPackageInfo(Package $package): PackageData |
|
113
|
|
|
{ |
|
114
|
3 |
|
$packageData = $this->getPackageInfoByCarrierId($package->getCarrier(), $package->getCarrierId()); |
|
115
|
|
|
|
|
116
|
2 |
|
$packageData->setEID($package->getBatchId()); |
|
117
|
|
|
|
|
118
|
2 |
|
return $packageData; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2 |
|
public function getPackageInfoByPackageId(string $carrier, string $packageId): PackageData |
|
122
|
|
|
{ |
|
123
|
2 |
|
$response = $this->client->call(Version::V2V1, $carrier, Method::PACKAGE, path: $packageId, shouldHaveStatus: false); |
|
124
|
|
|
|
|
125
|
2 |
|
return $this->packageDataFactory->create($response); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
4 |
|
public function getPackageInfoByCarrierId(string $carrier, string $carrierId): PackageData |
|
129
|
|
|
{ |
|
130
|
4 |
|
$response = $this->client->call( |
|
131
|
4 |
|
Version::V2V1, |
|
132
|
4 |
|
$carrier, |
|
133
|
4 |
|
Method::PACKAGE, |
|
134
|
4 |
|
path: sprintf('carrier_id/%s', $carrierId), |
|
135
|
4 |
|
shouldHaveStatus: false, |
|
136
|
4 |
|
); |
|
137
|
|
|
|
|
138
|
3 |
|
return $this->packageDataFactory->create($response); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
3 |
|
public function checkPackages(PackageDataCollection $packages): void |
|
142
|
|
|
{ |
|
143
|
3 |
|
$this->client->call(Version::V2V1, $packages->getCarrier(), Method::CHECK, ['packages' => $packages->__toArray()]); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
1 |
|
public function getProofOfDelivery(Package $package): string |
|
147
|
|
|
{ |
|
148
|
1 |
|
return $this->getProofOfDeliveryByCarrierId($package->getCarrier(), $package->getCarrierId()); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** @inheritDoc */ |
|
152
|
3 |
|
public function getProofOfDeliveries(PackageCollection $packages): array |
|
153
|
|
|
{ |
|
154
|
3 |
|
return $this->getProofOfDeliveriesByCarrierIds($packages->getCarrier(), $packages->getCarrierIds()); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
2 |
|
public function getProofOfDeliveryByCarrierId(string $carrier, string $carrierId): string |
|
158
|
|
|
{ |
|
159
|
2 |
|
return $this->getProofOfDeliveriesByCarrierIds($carrier, [$carrierId])[0]; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** @inheritDoc */ |
|
163
|
6 |
|
public function getProofOfDeliveriesByCarrierIds(string $carrier, array $carrierIds): array |
|
164
|
|
|
{ |
|
165
|
6 |
|
$response = $this->client->call( |
|
166
|
6 |
|
Version::V1V1, |
|
167
|
6 |
|
$carrier, |
|
168
|
6 |
|
Method::PROOF_OF_DELIVERY, |
|
169
|
6 |
|
array_map(static fn (string $carrierId): array => ['id' => $carrierId], $carrierIds), |
|
170
|
6 |
|
shouldHaveStatus: false, |
|
171
|
6 |
|
); |
|
172
|
|
|
|
|
173
|
6 |
|
return $this->proofOfDeliveryFactory->create($carrierIds, $response); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
3 |
|
public function getTransportCosts(PackageDataCollection $packages): TransportCostCollection |
|
177
|
|
|
{ |
|
178
|
3 |
|
$response = $this->client->call(Version::V2V1, $packages->getCarrier(), Method::TRANSPORT_COSTS, ['packages' => $packages->__toArray()]); |
|
179
|
|
|
|
|
180
|
2 |
|
return $this->transportCostFactory->createCollection($packages->getCarrier(), $packages->__toArray(), $response); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
3 |
|
public function orderB2AShipment(PackageDataCollection $packages): PackageCollection |
|
184
|
|
|
{ |
|
185
|
3 |
|
$response = $this->client->call(Version::V2V1, $packages->getCarrier(), Method::B2A, ['packages' => $packages->__toArray()]); |
|
186
|
|
|
|
|
187
|
2 |
|
return $this->packageFactory->createCollection($packages->getCarrier(), $packages->__toArray(), $response); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
4 |
|
public function orderPickup( |
|
191
|
|
|
string $carrier, |
|
192
|
|
|
DateTimeInterface $dateFrom, |
|
193
|
|
|
DateTimeInterface $dateTo, |
|
194
|
|
|
float $weight, |
|
195
|
|
|
int $packageCount, |
|
196
|
|
|
?string $message = null, |
|
197
|
|
|
): void { |
|
198
|
4 |
|
$response = $this->client->call(Version::V2V1, $carrier, Method::ORDER_PICKUP, [ |
|
199
|
4 |
|
'date' => $dateFrom->format('Y-m-d'), |
|
200
|
4 |
|
'time_from' => $dateFrom->format('H:i'), |
|
201
|
4 |
|
'time_to' => $dateTo->format('H:i'), |
|
202
|
4 |
|
'weight' => $weight, |
|
203
|
4 |
|
'package_count' => $packageCount, |
|
204
|
4 |
|
'message' => $message, |
|
205
|
4 |
|
]); |
|
206
|
|
|
|
|
207
|
3 |
|
if (array_key_exists('message', $response)) { |
|
208
|
1 |
|
throw new BadRequestException($response, 400, null, $response['message']); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
3 |
|
public function orderB2CShipment(PackageDataCollection $packages): PackageCollection |
|
213
|
|
|
{ |
|
214
|
3 |
|
$response = $this->client->call(Version::V2V1, $packages->getCarrier(), Method::B2C, ['packages' => $packages->__toArray()]); |
|
215
|
|
|
|
|
216
|
2 |
|
return $this->packageFactory->createCollection($packages->getCarrier(), $packages->__toArray(), $response); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
3 |
|
public function checkB2APackages(PackageDataCollection $packages): void |
|
220
|
|
|
{ |
|
221
|
3 |
|
$this->client->call(Version::V2V1, $packages->getCarrier(), Method::B2A_CHECK, ['packages' => $packages->__toArray()]); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
3 |
|
public function checkB2CPackages(PackageDataCollection $packages): void |
|
225
|
|
|
{ |
|
226
|
3 |
|
$this->client->call(Version::V2V1, $packages->getCarrier(), Method::B2C_CHECK, ['packages' => $packages->__toArray()]); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|