PluginProbe
Packeta / 1.3.0
Packeta v1.3.0
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.3.0, at src/Packetery/Core/Api/Soap/Client.php

296 lines 8.4 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
194 return $response;
195 }
196
197 /**
198 * Asks for carrier labels.
199 *
200 * @param Request\PacketsCourierLabelsPdf $request Label request.
201 *
202 * @return Response\PacketsCourierLabelsPdf
203 */
204 public function packetsCarrierLabelsPdf( Request\PacketsCourierLabelsPdf $request ): Response\PacketsCourierLabelsPdf {
205 $response = new Response\PacketsCourierLabelsPdf();
206 try {
207 $soapClient = new SoapClient( self::WSDL_URL );
208 $pdfContents = $soapClient->packetsCourierLabelsPdf( $this->apiPassword, $request->getPacketIdsWithCourierNumbers(), $request->getOffset(), $request->getFormat() );
209 $response->setPdfContents( $pdfContents );
210 } catch ( SoapFault $exception ) {
211 $response->setFault( $this->getFaultIdentifier( $exception ) );
212 $response->setFaultString( $exception->faultstring );
213 }
214
215 return $response;
216 }
217
218 /**
219 * Requests carrier number for a packet.
220 *
221 * @param Request\PacketCourierNumber $request PacketCourierNumber request.
222 *
223 * @return Response\PacketCourierNumber
224 */
225 public function packetCourierNumber( Request\PacketCourierNumber $request ): Response\PacketCourierNumber {
226 $response = new Response\PacketCourierNumber();
227 try {
228 $soapClient = new SoapClient( self::WSDL_URL );
229 $number = $soapClient->packetCourierNumber( $this->apiPassword, $request->getPacketId() );
230 $response->setNumber( $number );
231 } catch ( SoapFault $exception ) {
232 $response->setFault( $this->getFaultIdentifier( $exception ) );
233 $response->setFaultString( $exception->faultstring );
234 }
235
236 return $response;
237 }
238
239 /**
240 * Requests for sender return routing strings.
241 *
242 * @param Request\SenderGetReturnRouting $request Request.
243 *
244 * @return Response\SenderGetReturnRouting
245 */
246 public function senderGetReturnRouting( Request\SenderGetReturnRouting $request ): Response\SenderGetReturnRouting {
247 $response = new Response\SenderGetReturnRouting();
248 try {
249 $soapClient = new SoapClient( self::WSDL_URL );
250 $soapClient->senderGetReturnRouting( $this->apiPassword, $request->getSenderLabel() );
251 // TODO: Set return routing strings.
252 } catch ( SoapFault $exception ) {
253 $response->setFault( $this->getFaultIdentifier( $exception ) );
254 $response->setFaultString( $exception->faultstring );
255 }
256
257 return $response;
258 }
259
260 /**
261 * Gets human-readable errors from SoapFault exception.
262 *
263 * @param SoapFault $exception Exception.
264 *
265 * @return array
266 */
267 protected function getValidationErrors( SoapFault $exception ): array {
268 $errors = [];
269
270 $faults = ( $exception->detail->PacketAttributesFault->attributes->fault ?? [] );
271 if ( $faults && ! is_array( $faults ) ) {
272 $faults = [ $faults ];
273 }
274 foreach ( $faults as $fault ) {
275 $errors[] = sprintf( '%s: %s', $fault->name, $fault->fault );
276 }
277
278 return $errors;
279 }
280
281 /**
282 * Gets fault identifier from SoapFault exception.
283 *
284 * @param SoapFault $exception Exception.
285 *
286 * @return int|string
287 */
288 private function getFaultIdentifier( SoapFault $exception ): string {
289 if ( isset( $exception->detail ) ) {
290 return array_keys( get_object_vars( $exception->detail ) )[0];
291 }
292
293 return $exception->faultstring;
294 }
295 }
296