PluginProbe
Packeta / 1.4
Packeta v1.4
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Core / Api / Soap / Client.php

Client.php in Packeta 1.4, at src/Packetery/Core/Api/Soap/Client.php

307 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Packetery\Core\Api\Soap\Request;
13 use Packetery\Core\Api\Soap\Response;
14 use SoapClient;
15 use SoapFault;
16
17 /**
18 * Class Packet.
19 *
20 * @package Packetery\Api\Soap
21 */
22 class Client {
23
24 private const WSDL_URL = 'http://www.zasilkovna.cz/api/soap.wsdl';
25
26 /**
27 * API password.
28 *
29 * @var string|null
30 */
31 private $apiPassword;
32
33 /**
34 * Client constructor.
35 *
36 * @param string|null $apiPassword Api password.
37 */
38 public function __construct( ?string $apiPassword ) {
39 $this->apiPassword = $apiPassword;
40 }
41
42 /**
43 * Sets API password.
44 *
45 * @param string $apiPassword API password.
46 *
47 * @return void
48 */
49 public function setApiPassword( string $apiPassword ): void {
50 $this->apiPassword = $apiPassword;
51 }
52
53 /**
54 * Submits packet data to Packeta API.
55 *
56 * @param Request\CreatePacket $request Packet attributes.
57 *
58 * @return Response\CreatePacket
59 */
60 public function createPacket( Request\CreatePacket $request ): Response\CreatePacket {
61 $response = new Response\CreatePacket();
62 try {
63 $soapClient = new SoapClient( self::WSDL_URL );
64 $packet = $soapClient->createPacket( $this->apiPassword, $request->getSubmittableData() );
65 $response->setId( $packet->id );
66 } catch ( SoapFault $exception ) {
67 $response->setFault( $this->getFaultIdentifier( $exception ) );
68 $response->setFaultString( $exception->faultstring );
69 $response->setValidationErrors( $this->getValidationErrors( $exception ) );
70 }
71
72 return $response;
73 }
74
75 /**
76 * Submits packet data to Packeta API.
77 *
78 * @param Request\CancelPacket $request Packet attributes.
79 *
80 * @return Response\CancelPacket
81 */
82 public function cancelPacket( Request\CancelPacket $request ): Response\CancelPacket {
83 $response = new Response\CancelPacket();
84 try {
85 $soapClient = new SoapClient( self::WSDL_URL );
86 $soapClient->cancelPacket( $this->apiPassword, $request->getPacketId() );
87 } catch ( SoapFault $exception ) {
88 $response->setFault( $this->getFaultIdentifier( $exception ) );
89 $response->setFaultString( $exception->faultstring );
90 }
91
92 return $response;
93 }
94
95 /**
96 * Retrieves packet status.
97 *
98 * @param Request\PacketStatus $request Packet attributes.
99 *
100 * @return Response\PacketStatus
101 */
102 public function packetStatus( Request\PacketStatus $request ): Response\PacketStatus {
103 $response = new Response\PacketStatus();
104 try {
105 $soapClient = new SoapClient( self::WSDL_URL );
106 $result = $soapClient->packetStatus( $this->apiPassword, $request->getPacketId() );
107 $response->setCodeText( $result->codeText );
108 } catch ( SoapFault $exception ) {
109 $response->setFault( $this->getFaultIdentifier( $exception ) );
110 $response->setFaultString( $exception->faultstring );
111 }
112
113 return $response;
114 }
115
116 /**
117 * Create shipment.
118 *
119 * @param Request\CreateShipment $request Request.
120 *
121 * @return Response\CreateShipment
122 */
123 public function createShipment( Request\CreateShipment $request ): Response\CreateShipment {
124 $response = new Response\CreateShipment();
125 try {
126 $soapClient = new SoapClient( self::WSDL_URL );
127 $packet = $soapClient->createShipment( $this->apiPassword, $request->getPacketIds(), $request->getCustomBarcode() );
128 $response->setId( $packet->id );
129 $response->setChecksum( $packet->checksum );
130 $response->setBarcode( $packet->barcode );
131 $response->setBarcodeText( $packet->barcodeText );
132 } catch ( SoapFault $exception ) {
133 $response->setFault( $this->getFaultIdentifier( $exception ) );
134 $response->setFaultString( $exception->faultstring );
135
136 if ( isset( $exception->detail, $exception->detail->PacketIdsFault ) ) {
137 $invalidPacketIds = (array) $exception->detail->PacketIdsFault->ids->packetId;
138 $invalidPacketIdsFiltered = [];
139
140 foreach ( $invalidPacketIds as $invalidPacketId ) {
141 if ( empty( $invalidPacketId ) ) {
142 continue;
143 }
144
145 $invalidPacketIdsFiltered[] = $invalidPacketId;
146 }
147
148 $response->setInvalidPacketIds( $invalidPacketIdsFiltered );
149 }
150 }
151
152 return $response;
153 }
154
155 /**
156 * Barcode PNG.
157 *
158 * @param Request\BarcodePng $request Request.
159 *
160 * @return Response\BarcodePng
161 */
162 public function barcodePng( Request\BarcodePng $request ): Response\BarcodePng {
163 $response = new Response\BarcodePng();
164 try {
165 $soapClient = new SoapClient( self::WSDL_URL );
166 $data = $soapClient->barcodePng( $this->apiPassword, $request->getBarcode() );
167 $response->setImageContent( $data );
168 } catch ( SoapFault $exception ) {
169 $response->setFault( $this->getFaultIdentifier( $exception ) );
170 $response->setFaultString( $exception->faultstring );
171 }
172
173 return $response;
174 }
175
176 /**
177 * Asks for packeta labels.
178 *
179 * @param Request\PacketsLabelsPdf $request Label request.
180 *
181 * @return Response\PacketsLabelsPdf
182 */
183 public function packetsLabelsPdf( Request\PacketsLabelsPdf $request ): Response\PacketsLabelsPdf {
184 $response = new Response\PacketsLabelsPdf();
185 try {
186 $soapClient = new SoapClient( self::WSDL_URL );
187 $pdfContents = $soapClient->packetsLabelsPdf( $this->apiPassword, $request->getPacketIds(), $request->getFormat(), $request->getOffset() );
188 $response->setPdfContents( $pdfContents );
189 } catch ( SoapFault $exception ) {
190 $response->setFault( $this->getFaultIdentifier( $exception ) );
191 $response->setFaultString( $exception->faultstring );
192
193 if ( $response->hasPacketIdsFault() ) {
194 $response->setInvalidPacketIds( (array) $exception->detail->PacketIdsFault->ids->packetId );
195 }
196 }
197
198 return $response;
199 }
200
201 /**
202 * Asks for carrier labels.
203 *
204 * @param Request\PacketsCourierLabelsPdf $request Label request.
205 *
206 * @return Response\PacketsCourierLabelsPdf
207 */
208 public function packetsCarrierLabelsPdf( Request\PacketsCourierLabelsPdf $request ): Response\PacketsCourierLabelsPdf {
209 $response = new Response\PacketsCourierLabelsPdf();
210 try {
211 $soapClient = new SoapClient( self::WSDL_URL );
212 $pdfContents = $soapClient->packetsCourierLabelsPdf( $this->apiPassword, $request->getPacketIdsWithCourierNumbers(), $request->getOffset(), $request->getFormat() );
213 $response->setPdfContents( $pdfContents );
214 } catch ( SoapFault $exception ) {
215 $response->setFault( $this->getFaultIdentifier( $exception ) );
216 $response->setFaultString( $exception->faultstring );
217
218 if ( $response->hasInvalidCourierNumberFault() && count( $request->getPacketIdsWithCourierNumbers() ) === 1 ) {
219 $response->setInvalidCourierNumbers( array_column( $request->getPacketIdsWithCourierNumbers(), 'courierNumber' ) );
220 }
221 if ( $response->hasPacketIdFault() && count( $request->getPacketIdsWithCourierNumbers() ) === 1 ) {
222 $response->setInvalidPacketIds( array_column( $request->getPacketIdsWithCourierNumbers(), 'packetId' ) );
223 }
224 }
225
226 return $response;
227 }
228
229 /**
230 * Requests carrier number for a packet.
231 *
232 * @param Request\PacketCourierNumber $request PacketCourierNumber request.
233 *
234 * @return Response\PacketCourierNumber
235 */
236 public function packetCourierNumber( Request\PacketCourierNumber $request ): Response\PacketCourierNumber {
237 $response = new Response\PacketCourierNumber();
238 try {
239 $soapClient = new SoapClient( self::WSDL_URL );
240 $number = $soapClient->packetCourierNumber( $this->apiPassword, $request->getPacketId() );
241 $response->setNumber( $number );
242 } catch ( SoapFault $exception ) {
243 $response->setFault( $this->getFaultIdentifier( $exception ) );
244 $response->setFaultString( $exception->faultstring );
245 }
246
247 return $response;
248 }
249
250 /**
251 * Requests for sender return routing strings.
252 *
253 * @param Request\SenderGetReturnRouting $request Request.
254 *
255 * @return Response\SenderGetReturnRouting
256 */
257 public function senderGetReturnRouting( Request\SenderGetReturnRouting $request ): Response\SenderGetReturnRouting {
258 $response = new Response\SenderGetReturnRouting();
259 try {
260 $soapClient = new SoapClient( self::WSDL_URL );
261 $soapClient->senderGetReturnRouting( $this->apiPassword, $request->getSenderLabel() );
262 // TODO: Set return routing strings.
263 } catch ( SoapFault $exception ) {
264 $response->setFault( $this->getFaultIdentifier( $exception ) );
265 $response->setFaultString( $exception->faultstring );
266 }
267
268 return $response;
269 }
270
271 /**
272 * Gets human-readable errors from SoapFault exception.
273 *
274 * @param SoapFault $exception Exception.
275 *
276 * @return array
277 */
278 protected function getValidationErrors( SoapFault $exception ): array {
279 $errors = [];
280
281 $faults = ( $exception->detail->PacketAttributesFault->attributes->fault ?? [] );
282 if ( $faults && ! is_array( $faults ) ) {
283 $faults = [ $faults ];
284 }
285 foreach ( $faults as $fault ) {
286 $errors[] = sprintf( '%s: %s', $fault->name, $fault->fault );
287 }
288
289 return $errors;
290 }
291
292 /**
293 * Gets fault identifier from SoapFault exception.
294 *
295 * @param SoapFault $exception Exception.
296 *
297 * @return int|string
298 */
299 private function getFaultIdentifier( SoapFault $exception ): string {
300 if ( isset( $exception->detail ) ) {
301 return array_keys( get_object_vars( $exception->detail ) )[0];
302 }
303
304 return $exception->faultstring;
305 }
306 }
307