1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ups; |
4
|
|
|
|
5
|
|
|
use DOMDocument; |
6
|
|
|
use Exception; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use SimpleXMLElement; |
9
|
|
|
use Ups\Entity\TimeInTransitRequest; |
10
|
|
|
use Ups\Entity\TimeInTransitResponse; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* TimeInTransit API Wrapper. |
14
|
|
|
* |
15
|
|
|
* @author Sebastien Vergnes <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class TimeInTransit extends Ups |
18
|
|
|
{ |
19
|
|
|
private $request; |
20
|
|
|
|
21
|
|
|
const ENDPOINT = '/TimeInTransit'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string|null $accessKey UPS License Access Key |
25
|
|
|
* @param string|null $userId UPS User ID |
26
|
|
|
* @param string|null $password UPS User Password |
27
|
|
|
* @param bool $useIntegration Determine if we should use production or CIE URLs. |
28
|
|
|
* @param RequestInterface|null $request |
29
|
|
|
* @param LoggerInterface|null $logger PSR3 compatible logger (optional) |
30
|
|
|
*/ |
31
|
4 |
View Code Duplication |
public function __construct($accessKey = null, $userId = null, $password = null, $useIntegration = false, RequestInterface $request = null, LoggerInterface $logger = null) |
|
|
|
|
32
|
|
|
{ |
33
|
4 |
|
if (null !== $request) { |
34
|
|
|
$this->setRequest($request); |
35
|
|
|
} |
36
|
4 |
|
parent::__construct($accessKey, $userId, $password, $useIntegration, $logger); |
37
|
4 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param TimeInTransitRequest $shipment |
41
|
|
|
* |
42
|
|
|
* @throws Exception |
43
|
|
|
* |
44
|
|
|
* @return TimeInTransitRequest |
45
|
|
|
*/ |
46
|
4 |
|
public function getTimeInTransit(TimeInTransitRequest $shipment) |
47
|
|
|
{ |
48
|
4 |
|
return $this->sendRequest($shipment); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Creates and sends a request for the given shipment. This handles checking for |
53
|
|
|
* errors in the response back from UPS. |
54
|
|
|
* |
55
|
|
|
* @param TimeInTransitRequest $timeInTransitRequest |
56
|
|
|
* |
57
|
|
|
* @throws Exception |
58
|
|
|
* |
59
|
|
|
* @return TimeInTransitRequest |
60
|
|
|
*/ |
61
|
4 |
View Code Duplication |
private function sendRequest(TimeInTransitRequest $timeInTransitRequest) |
|
|
|
|
62
|
|
|
{ |
63
|
4 |
|
$request = $this->createRequest($timeInTransitRequest); |
64
|
4 |
|
$this->response = $this->getRequest()->request($this->createAccess(), $request, $this->compileEndpointUrl(self::ENDPOINT)); |
|
|
|
|
65
|
4 |
|
$response = $this->response->getResponse(); |
|
|
|
|
66
|
|
|
|
67
|
4 |
|
if (null === $response) { |
68
|
1 |
|
throw new Exception('Failure (0): Unknown error', 0); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
if ($response instanceof SimpleXMLElement && $response->Response->ResponseStatusCode == 0) { |
72
|
|
|
throw new Exception( |
73
|
|
|
"Failure ({$response->Response->Error->ErrorSeverity}): {$response->Response->Error->ErrorDescription}", |
74
|
|
|
(int)$response->Response->Error->ErrorCode |
75
|
|
|
); |
76
|
|
|
} else { |
77
|
3 |
|
return $this->formatResponse($response); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create the TimeInTransit request. |
83
|
|
|
* |
84
|
|
|
* @param TimeInTransitRequest $timeInTransitRequest The request details. Refer to the UPS documentation for available structure |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
4 |
|
private function createRequest(TimeInTransitRequest $timeInTransitRequest) |
89
|
|
|
{ |
90
|
4 |
|
$xml = new DOMDocument(); |
91
|
4 |
|
$xml->formatOutput = true; |
92
|
|
|
|
93
|
4 |
|
$trackRequest = $xml->appendChild($xml->createElement('TimeInTransitRequest')); |
94
|
4 |
|
$trackRequest->setAttribute('xml:lang', 'en-US'); |
95
|
|
|
|
96
|
4 |
|
$request = $trackRequest->appendChild($xml->createElement('Request')); |
97
|
|
|
|
98
|
4 |
|
$node = $xml->importNode($this->createTransactionNode(), true); |
99
|
4 |
|
$request->appendChild($node); |
100
|
|
|
|
101
|
4 |
|
$request->appendChild($xml->createElement('RequestAction', 'TimeInTransit')); |
102
|
|
|
|
103
|
4 |
|
$transitFromNode = $trackRequest->appendChild($xml->createElement('TransitFrom')); |
104
|
4 |
|
$address = $timeInTransitRequest->getTransitFrom(); |
105
|
4 |
|
if (isset($address)) { |
106
|
4 |
|
$transitFromNode->appendChild($address->toNode($xml)); |
107
|
4 |
|
} |
108
|
|
|
|
109
|
4 |
|
$transitToNode = $trackRequest->appendChild($xml->createElement('TransitTo')); |
110
|
4 |
|
$address = $timeInTransitRequest->getTransitTo(); |
111
|
4 |
|
if (isset($address)) { |
112
|
4 |
|
$transitToNode->appendChild($address->toNode($xml)); |
113
|
4 |
|
} |
114
|
|
|
|
115
|
4 |
|
$weight = $timeInTransitRequest->getShipmentWeight(); |
116
|
4 |
|
if (isset($weight)) { |
117
|
4 |
|
$trackRequest->appendChild($weight->toNode($xml)); |
118
|
4 |
|
} |
119
|
|
|
|
120
|
4 |
|
$packages = $timeInTransitRequest->getTotalPackagesInShipment(); |
121
|
4 |
|
if (isset($packages)) { |
122
|
1 |
|
$trackRequest->appendChild($xml->createElement('TotalPackagesInShipment', $packages)); |
123
|
1 |
|
} |
124
|
|
|
|
125
|
4 |
|
$invoiceLineTotal = $timeInTransitRequest->getInvoiceLineTotal(); |
126
|
4 |
|
if (isset($invoiceLineTotal)) { |
127
|
4 |
|
$trackRequest->appendChild($invoiceLineTotal->toNode($xml)); |
128
|
4 |
|
} |
129
|
|
|
|
130
|
4 |
|
$pickupDate = $timeInTransitRequest->getPickupDate(); |
131
|
4 |
|
if ($pickupDate) { |
132
|
4 |
|
$trackRequest->appendChild($xml->createElement('PickupDate', $pickupDate->format('Ymd'))); |
133
|
4 |
|
} |
134
|
|
|
|
135
|
4 |
|
$indicator = $timeInTransitRequest->getDocumentsOnlyIndicator(); |
136
|
4 |
|
if ($indicator) { |
137
|
1 |
|
$trackRequest->appendChild($xml->createElement('DocumentsOnlyIndicator')); |
138
|
1 |
|
} |
139
|
|
|
|
140
|
4 |
|
return $xml->saveXML(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Format the response. |
145
|
|
|
* |
146
|
|
|
* @param SimpleXMLElement $response |
147
|
|
|
* |
148
|
|
|
* @return TimeInTransitRequest |
149
|
|
|
*/ |
150
|
3 |
|
private function formatResponse(SimpleXMLElement $response) |
151
|
|
|
{ |
152
|
|
|
// We don't need to return data regarding the response to the user |
153
|
3 |
|
unset($response->Response); |
154
|
|
|
|
155
|
3 |
|
$result = $this->convertXmlObject($response); |
156
|
|
|
|
157
|
|
|
// Fix for when one ServiceSummary while expecting an array in later responses - easiest place to fix here |
158
|
3 |
|
if (isset($result->TransitResponse->ServiceSummary->Service)) { |
159
|
|
|
$result->TransitResponse->ServiceSummary = array($result->TransitResponse->ServiceSummary); |
160
|
|
|
} |
161
|
|
|
|
162
|
3 |
|
return new TimeInTransitResponse($result->TransitResponse); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return RequestInterface |
167
|
|
|
*/ |
168
|
4 |
View Code Duplication |
public function getRequest() |
|
|
|
|
169
|
|
|
{ |
170
|
4 |
|
if (null === $this->request) { |
171
|
|
|
$this->request = new Request($this->logger); |
172
|
|
|
} |
173
|
|
|
|
174
|
4 |
|
return $this->request; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param RequestInterface $request |
179
|
|
|
* |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
4 |
|
public function setRequest(RequestInterface $request) |
183
|
|
|
{ |
184
|
4 |
|
$this->request = $request; |
185
|
|
|
|
186
|
4 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return ResponseInterface |
191
|
|
|
*/ |
192
|
|
|
public function getResponse() |
193
|
|
|
{ |
194
|
|
|
return $this->response; |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param ResponseInterface $response |
199
|
|
|
* |
200
|
|
|
* @return $this |
201
|
|
|
*/ |
202
|
|
|
public function setResponse(ResponseInterface $response) |
203
|
|
|
{ |
204
|
|
|
$this->response = $response; |
|
|
|
|
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.