| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Packet. |
| 4 |
* |
| 5 |
* @package Packetery\Api\Soap |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Core\Api\Soap; |
| 11 |
|
| 12 |
use SoapClient; |
| 13 |
use SoapFault; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Packet. |
| 17 |
* |
| 18 |
* @package Packetery\Api\Soap |
| 19 |
*/ |
| 20 |
class Client { |
| 21 |
|
| 22 |
/** |
| 23 |
* API password. |
| 24 |
* |
| 25 |
* @var string|null |
| 26 |
*/ |
| 27 |
private $apiPassword; |
| 28 |
|
| 29 |
/** |
| 30 |
* WSDL Url. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
public $wsdlUrl; |
| 35 |
|
| 36 |
/** |
| 37 |
* Client constructor. |
| 38 |
* |
| 39 |
* @param string|null $apiPassword Api password. |
| 40 |
* @param string $wsdlUrl WSDL url. |
| 41 |
*/ |
| 42 |
public function __construct( ?string $apiPassword, string $wsdlUrl ) { |
| 43 |
$this->apiPassword = $apiPassword; |
| 44 |
$this->wsdlUrl = $wsdlUrl; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Sets API password. |
| 49 |
* |
| 50 |
* @param string $apiPassword API password. |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
public function setApiPassword( string $apiPassword ): void { |
| 55 |
$this->apiPassword = $apiPassword; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Creates storage file. |
| 60 |
* |
| 61 |
* @param Request\CreateStorageFile $request Request. |
| 62 |
* @return Response\CreateStorageFile |
| 63 |
*/ |
| 64 |
public function createStorageFile( Request\CreateStorageFile $request ): Response\CreateStorageFile { |
| 65 |
$response = new Response\CreateStorageFile(); |
| 66 |
try { |
| 67 |
$soapClient = new SoapClient( $this->wsdlUrl ); |
| 68 |
$storageFile = $soapClient->createStorageFile( |
| 69 |
$this->apiPassword, |
| 70 |
[ |
| 71 |
'content' => $request->getContent(), |
| 72 |
'name' => $request->getName(), |
| 73 |
] |
| 74 |
); |
| 75 |
$response->setId( (string) $storageFile->id ); |
| 76 |
} catch ( SoapFault $exception ) { |
| 77 |
$response->setFault( $this->getFaultIdentifier( $exception ) ); |
| 78 |
$response->setFaultString( $exception->faultstring ); |
| 79 |
} |
| 80 |
|
| 81 |
return $response; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Submits packet data to Packeta API. |
| 86 |
* We deliberately don't use Request\CreatePacket class to send data to the API, but we keep the class for possible later use. |
| 87 |
* |
| 88 |
* @param array<string, mixed> $requestData Packet attributes. |
| 89 |
* |
| 90 |
* @return Response\CreatePacket |
| 91 |
*/ |
| 92 |
public function createPacket( array $requestData ): Response\CreatePacket { |
| 93 |
$response = new Response\CreatePacket(); |
| 94 |
try { |
| 95 |
$soapClient = new SoapClient( $this->wsdlUrl ); |
| 96 |
$packet = $soapClient->createPacket( $this->apiPassword, $requestData ); |
| 97 |
$response->setId( $packet->id ); |
| 98 |
} catch ( SoapFault $exception ) { |
| 99 |
$response->setFault( $this->getFaultIdentifier( $exception ) ); |
| 100 |
$response->setFaultString( $exception->faultstring ); |
| 101 |
$response->setValidationErrors( $this->getValidationErrors( $exception ) ); |
| 102 |
} |
| 103 |
|
| 104 |
return $response; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Create claim packet with password. |
| 109 |
* |
| 110 |
* @param Request\CreatePacketClaimWithPassword $request Request attributes. |
| 111 |
* |
| 112 |
* @return Response\CreatePacketClaimWithPassword |
| 113 |
*/ |
| 114 |
public function createPacketClaimWithPassword( Request\CreatePacketClaimWithPassword $request ): Response\CreatePacketClaimWithPassword { |
| 115 |
$response = new Response\CreatePacketClaimWithPassword(); |
| 116 |
try { |
| 117 |
$soapClient = new SoapClient( $this->wsdlUrl ); |
| 118 |
$packetClaimWithPassword = $soapClient->createPacketClaimWithPassword( $this->apiPassword, $request->getSubmittableData() ); |
| 119 |
$response->setId( $packetClaimWithPassword->id ); |
| 120 |
$response->setPassword( $packetClaimWithPassword->password ); |
| 121 |
} catch ( SoapFault $exception ) { |
| 122 |
$response->setFault( $this->getFaultIdentifier( $exception ) ); |
| 123 |
$response->setFaultString( $exception->faultstring ); |
| 124 |
$response->setValidationErrors( $this->getValidationErrors( $exception ) ); |
| 125 |
} |
| 126 |
|
| 127 |
return $response; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Submits packet data to Packeta API. |
| 132 |
* |
| 133 |
* @param Request\CancelPacket $request Packet attributes. |
| 134 |
* |
| 135 |
* @return Response\CancelPacket |
| 136 |
*/ |
| 137 |
public function cancelPacket( Request\CancelPacket $request ): Response\CancelPacket { |
| 138 |
$response = new Response\CancelPacket(); |
| 139 |
try { |
| 140 |
$soapClient = new SoapClient( $this->wsdlUrl ); |
| 141 |
$soapClient->cancelPacket( $this->apiPassword, $request->getPacketId() ); |
| 142 |
} catch ( SoapFault $exception ) { |
| 143 |
$response->setFault( $this->getFaultIdentifier( $exception ) ); |
| 144 |
$response->setFaultString( $exception->faultstring ); |
| 145 |
} |
| 146 |
|
| 147 |
return $response; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Retrieves packet status. |
| 152 |
* |
| 153 |
* @param Request\PacketStatus $request Packet attributes. |
| 154 |
* |
| 155 |
* @return Response\PacketStatus |
| 156 |
*/ |
| 157 |
public function packetStatus( Request\PacketStatus $request ): Response\PacketStatus { |
| 158 |
$response = new Response\PacketStatus(); |
| 159 |
try { |
| 160 |
$soapClient = new SoapClient( $this->wsdlUrl ); |
| 161 |
$result = $soapClient->packetStatus( $this->apiPassword, $request->getPacketId() ); |
| 162 |
$response->setCodeText( $result->codeText ); |
| 163 |
$response->setStoredUntil( $result->storedUntil ); |
| 164 |
} catch ( SoapFault $exception ) { |
| 165 |
$response->setFault( $this->getFaultIdentifier( $exception ) ); |
| 166 |
$response->setFaultString( $exception->faultstring ); |
| 167 |
} |
| 168 |
|
| 169 |
return $response; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Retrieves packet detail including optional consign password. |
| 174 |
* |
| 175 |
* @param Request\PacketInfo $request Packet attributes. |
| 176 |
* |
| 177 |
* @return Response\PacketInfo |
| 178 |
*/ |
| 179 |
public function packetInfo( Request\PacketInfo $request ): Response\PacketInfo { |
| 180 |
$response = new Response\PacketInfo(); |
| 181 |
try { |
| 182 |
$soapClient = new SoapClient( $this->wsdlUrl ); |
| 183 |
$result = $soapClient->packetInfo( $this->apiPassword, $request->getPacketId() ); |
| 184 |
if ( isset( $result->consignPassword ) && is_string( $result->consignPassword ) && $result->consignPassword !== '' ) { |
| 185 |
$response->setConsignPassword( $result->consignPassword ); |
| 186 |
} |
| 187 |
} catch ( SoapFault $exception ) { |
| 188 |
$response->setFault( $this->getFaultIdentifier( $exception ) ); |
| 189 |
$response->setFaultString( $exception->faultstring ); |
| 190 |
} |
| 191 |
|
| 192 |
return $response; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Create shipment. |
| 197 |
* |
| 198 |
* @param Request\CreateShipment $request Request. |
| 199 |
* |
| 200 |
* @return Response\CreateShipment |
| 201 |
*/ |
| 202 |
public function createShipment( Request\CreateShipment $request ): Response\CreateShipment { |
| 203 |
$response = new Response\CreateShipment(); |
| 204 |
try { |
| 205 |
$soapClient = new SoapClient( $this->wsdlUrl ); |
| 206 |
$shipment = $soapClient->createShipment( $this->apiPassword, $request->getPacketIds(), $request->getCustomBarcode() ); |
| 207 |
$response->setId( (string) $shipment->id ); |
| 208 |
$response->setChecksum( $shipment->checksum ); |
| 209 |
$response->setBarcode( $shipment->barcode ); |
| 210 |
$response->setBarcodeText( $shipment->barcodeText ); |
| 211 |
} catch ( SoapFault $exception ) { |
| 212 |
$response->setFault( $this->getFaultIdentifier( $exception ) ); |
| 213 |
$response->setFaultString( $exception->faultstring ); |
| 214 |
|
| 215 |
if ( isset( $exception->detail, $exception->detail->PacketIdsFault ) ) { |
| 216 |
$invalidPacketIds = (array) $exception->detail->PacketIdsFault->ids->packetId; |
| 217 |
$invalidPacketIdsFiltered = []; |
| 218 |
|
| 219 |
foreach ( $invalidPacketIds as $invalidPacketId ) { |
| 220 |
if ( ! is_string( $invalidPacketId ) || $invalidPacketId === '' ) { |
| 221 |
continue; |
| 222 |
} |
| 223 |
|
| 224 |
$invalidPacketIdsFiltered[] = $invalidPacketId; |
| 225 |
} |
| 226 |
|
| 227 |
$response->setInvalidPacketIds( $invalidPacketIdsFiltered ); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
return $response; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Barcode PNG. |
| 236 |
* |
| 237 |
* @param Request\BarcodePng $request Request. |
| 238 |
* |
| 239 |
* @return Response\BarcodePng |
| 240 |
*/ |
| 241 |
public function barcodePng( Request\BarcodePng $request ): Response\BarcodePng { |
| 242 |
$response = new Response\BarcodePng(); |
| 243 |
try { |
| 244 |
$soapClient = new SoapClient( $this->wsdlUrl ); |
| 245 |
$data = $soapClient->barcodePng( $this->apiPassword, $request->getBarcode() ); |
| 246 |
$response->setImageContent( $data ); |
| 247 |
} catch ( SoapFault $exception ) { |
| 248 |
$response->setFault( $this->getFaultIdentifier( $exception ) ); |
| 249 |
$response->setFaultString( $exception->faultstring ); |
| 250 |
} |
| 251 |
|
| 252 |
return $response; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Asks for Packeta labels. |
| 257 |
* |
| 258 |
* @param Request\PacketsLabelsPdf $request Label request. |
| 259 |
* |
| 260 |
* @return Response\PacketsLabelsPdf |
| 261 |
*/ |
| 262 |
public function packetsLabelsPdf( Request\PacketsLabelsPdf $request ): Response\PacketsLabelsPdf { |
| 263 |
$response = new Response\PacketsLabelsPdf(); |
| 264 |
try { |
| 265 |
$soapClient = new SoapClient( $this->wsdlUrl ); |
| 266 |
$pdfContents = $soapClient->packetsLabelsPdf( $this->apiPassword, $request->getPacketIds(), $request->getFormat(), $request->getOffset() ); |
| 267 |
$response->setPdfContents( $pdfContents ); |
| 268 |
} catch ( SoapFault $exception ) { |
| 269 |
$response->setFault( $this->getFaultIdentifier( $exception ) ); |
| 270 |
$response->setFaultString( $exception->faultstring ); |
| 271 |
|
| 272 |
if ( $response->hasPacketIdsFault() ) { |
| 273 |
$response->setInvalidPacketIds( (array) $exception->detail->PacketIdsFault->ids->packetId ); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
return $response; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Asks for carrier labels. |
| 282 |
* |
| 283 |
* @param Request\PacketsCourierLabelsPdf $request Label request. |
| 284 |
* |
| 285 |
* @return Response\PacketsCourierLabelsPdf |
| 286 |
*/ |
| 287 |
public function packetsCarrierLabelsPdf( Request\PacketsCourierLabelsPdf $request ): Response\PacketsCourierLabelsPdf { |
| 288 |
$response = new Response\PacketsCourierLabelsPdf(); |
| 289 |
try { |
| 290 |
$soapClient = new SoapClient( $this->wsdlUrl ); |
| 291 |
$pdfContents = $soapClient->packetsCourierLabelsPdf( $this->apiPassword, $request->getPacketIdsWithCourierNumbers(), $request->getOffset(), $request->getFormat() ); |
| 292 |
$response->setPdfContents( $pdfContents ); |
| 293 |
} catch ( SoapFault $exception ) { |
| 294 |
$response->setFault( $this->getFaultIdentifier( $exception ) ); |
| 295 |
$response->setFaultString( $exception->faultstring ); |
| 296 |
|
| 297 |
if ( $response->hasInvalidCourierNumberFault() && count( $request->getPacketIdsWithCourierNumbers() ) === 1 ) { |
| 298 |
$response->setInvalidCourierNumbers( array_column( $request->getPacketIdsWithCourierNumbers(), 'courierNumber' ) ); |
| 299 |
} |
| 300 |
if ( $response->hasPacketIdFault() && count( $request->getPacketIdsWithCourierNumbers() ) === 1 ) { |
| 301 |
$response->setInvalidPacketIds( array_column( $request->getPacketIdsWithCourierNumbers(), 'packetId' ) ); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
return $response; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Requests carrier number for a packet. |
| 310 |
* |
| 311 |
* @param Request\PacketCourierNumber $request PacketCourierNumber request. |
| 312 |
* |
| 313 |
* @return Response\PacketCourierNumber |
| 314 |
*/ |
| 315 |
public function packetCourierNumber( Request\PacketCourierNumber $request ): Response\PacketCourierNumber { |
| 316 |
$response = new Response\PacketCourierNumber(); |
| 317 |
try { |
| 318 |
$soapClient = new SoapClient( $this->wsdlUrl ); |
| 319 |
$number = $soapClient->packetCourierNumber( $this->apiPassword, $request->getPacketId() ); |
| 320 |
$response->setNumber( $number ); |
| 321 |
} catch ( SoapFault $exception ) { |
| 322 |
$response->setFault( $this->getFaultIdentifier( $exception ) ); |
| 323 |
$response->setFaultString( $exception->faultstring ); |
| 324 |
} |
| 325 |
|
| 326 |
return $response; |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Requests for sender return routing strings. |
| 331 |
* |
| 332 |
* @param Request\SenderGetReturnRouting $request Request. |
| 333 |
* |
| 334 |
* @return Response\SenderGetReturnRouting |
| 335 |
*/ |
| 336 |
public function senderGetReturnRouting( Request\SenderGetReturnRouting $request ): Response\SenderGetReturnRouting { |
| 337 |
$response = new Response\SenderGetReturnRouting(); |
| 338 |
try { |
| 339 |
$soapClient = new SoapClient( $this->wsdlUrl ); |
| 340 |
$soapClient->senderGetReturnRouting( $this->apiPassword, $request->getSenderLabel() ); |
| 341 |
} catch ( SoapFault $exception ) { |
| 342 |
$response->setFault( $this->getFaultIdentifier( $exception ) ); |
| 343 |
$response->setFaultString( $exception->faultstring ); |
| 344 |
} |
| 345 |
|
| 346 |
return $response; |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Sets new stored until date. |
| 351 |
* |
| 352 |
* @param Request\PacketSetStoredUntil $request Request. |
| 353 |
* |
| 354 |
* @return Response\PacketSetStoredUntil |
| 355 |
*/ |
| 356 |
public function packetSetStoredUntil( Request\PacketSetStoredUntil $request ): Response\PacketSetStoredUntil { |
| 357 |
$response = new Response\PacketSetStoredUntil(); |
| 358 |
try { |
| 359 |
$soapClient = new SoapClient( $this->wsdlUrl ); |
| 360 |
$soapClient->packetSetStoredUntil( $this->apiPassword, $request->getPacketId(), $request->getStoredUntil() ); |
| 361 |
} catch ( SoapFault $exception ) { |
| 362 |
$response->setFault( $this->getFaultIdentifier( $exception ) ); |
| 363 |
$response->setFaultString( $exception->faultstring ); |
| 364 |
} |
| 365 |
|
| 366 |
return $response; |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Gets human-readable errors from SoapFault exception. |
| 371 |
* |
| 372 |
* @param SoapFault $exception Exception. |
| 373 |
* |
| 374 |
* @return string[] |
| 375 |
*/ |
| 376 |
protected function getValidationErrors( SoapFault $exception ): array { |
| 377 |
$errors = []; |
| 378 |
|
| 379 |
$faults = ( $exception->detail->PacketAttributesFault->attributes->fault ?? [] ); |
| 380 |
if ( $faults && ! is_array( $faults ) ) { |
| 381 |
$faults = [ $faults ]; |
| 382 |
} |
| 383 |
if ( is_iterable( $faults ) ) { |
| 384 |
foreach ( $faults as $fault ) { |
| 385 |
$errors[] = sprintf( '%s: %s', $fault->name, $fault->fault ); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
return $errors; |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Gets fault identifier from SoapFault exception. |
| 394 |
* |
| 395 |
* @param SoapFault $exception Exception. |
| 396 |
* |
| 397 |
* @return string |
| 398 |
*/ |
| 399 |
private function getFaultIdentifier( SoapFault $exception ): string { |
| 400 |
if ( isset( $exception->detail ) ) { |
| 401 |
return (string) array_keys( get_object_vars( $exception->detail ) )[0]; |
| 402 |
} |
| 403 |
|
| 404 |
return $exception->faultstring; |
| 405 |
} |
| 406 |
} |
| 407 |
|