1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* xOrder Client. |
5
|
|
|
* |
6
|
|
|
* @package craftt/xorder-sdk |
7
|
|
|
* @author Ryan Stratton <[email protected]> |
8
|
|
|
* @copyright Copyright (c) Ryan Stratton |
9
|
|
|
* @license https://github.com/craftt/xorder-php-sdk/blob/master/LICENSE.md Apache 2.0 |
10
|
|
|
* @link https://github.com/craftt/xorder-php-sdk |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace XOrder; |
14
|
|
|
|
15
|
|
|
use GuzzleHttp\Client as HttpClient; |
16
|
|
|
use GuzzleHttp\ClientInterface as HttpClientInterface; |
17
|
|
|
use GuzzleHttp\Psr7\Request; |
18
|
|
|
use GuzzleHttp\Psr7\Uri; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
use Psr\Http\Message\UriInterface; |
21
|
|
|
use Psr\Log\LoggerAwareInterface; |
22
|
|
|
use XOrder\Contracts\ClientInterface; |
23
|
|
|
use XOrder\Contracts\SessionInterface; |
24
|
|
|
use XOrder\Credentials; |
25
|
|
|
use XOrder\Exceptions\InvalidCredentialsException; |
26
|
|
|
use XOrder\Exceptions\InvalidSessionException; |
27
|
|
|
use XOrder\Exceptions\XOrderConnectionException; |
28
|
|
|
use XOrder\LoggerAware; |
29
|
|
|
use XOrder\Response; |
30
|
|
|
use XOrder\Session; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Client |
34
|
|
|
*/ |
35
|
|
|
class Client implements ClientInterface, LoggerAwareInterface |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
use LoggerAware; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \Psr\Http\Message\UriInterface |
42
|
|
|
*/ |
43
|
|
|
public $baseUri; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var \XOrder\Credentials |
47
|
|
|
*/ |
48
|
|
|
public $credentials; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var \GuzzleHttp\ClientInterface |
52
|
|
|
*/ |
53
|
|
|
public $http; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var \XOrder\Contracts\SessionInterface |
57
|
|
|
*/ |
58
|
|
|
public $session; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Constructor |
62
|
|
|
* |
63
|
|
|
* @param \XOrder\Credentials|null $credentials |
64
|
|
|
*/ |
65
|
42 |
|
public function __construct(Credentials $credentials = null) |
66
|
2 |
|
{ |
67
|
42 |
|
$this->credentials = $credentials; |
68
|
42 |
|
$this->baseUri = new Uri('http://www.xorder.ca/'); |
69
|
42 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Logout from the |
73
|
|
|
*/ |
74
|
42 |
|
public function __destruct() |
75
|
|
|
{ |
76
|
42 |
|
$this->logout(); |
77
|
38 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the credentials. |
81
|
|
|
* |
82
|
|
|
* @return \XOrder\Credentials |
83
|
|
|
*/ |
84
|
24 |
|
public function credentials() |
85
|
|
|
{ |
86
|
24 |
|
if (!$this->hasCredentials()) { |
87
|
4 |
|
$this->logger()->error('XOrder\Client::credentials - No valid credentials have been set'); |
88
|
4 |
|
throw new InvalidCredentialsException('Please set your credentials!'); |
89
|
|
|
} |
90
|
|
|
|
91
|
20 |
|
return $this->credentials; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the logon xml. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
10 |
|
public function getLogonMessage() |
100
|
|
|
{ |
101
|
10 |
|
list($username, $password, $account) = $this->credentials()->toArray(); |
102
|
8 |
|
$xml = '<xLogon><userid>%s</userid><password>%s</password><bcldbNum>%s</bcldbNum></xLogon>'; |
103
|
8 |
|
return sprintf($xml, $username, $password, $account); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the logout xml. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
10 |
|
public function getLogoutMessage() |
112
|
|
|
{ |
113
|
10 |
|
list($username, , $account) = $this->credentials()->toArray(); |
114
|
10 |
|
$xml = '<xLogout><userid>%s</userid><sessionId>%s</sessionId><bcldbNum>%s</bcldbNum></xLogout>'; |
115
|
10 |
|
return sprintf($xml, $username, $this->session()->getId(), $account); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Build a uri based on the baseUri. |
120
|
|
|
* |
121
|
|
|
* @param string $path |
122
|
|
|
* @return \Psr\Http\Message\UriInterface |
123
|
|
|
*/ |
124
|
20 |
|
public function getUri($path) |
125
|
|
|
{ |
126
|
20 |
|
return new Uri($this->baseUri . $path); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Check if the client has the login credentials. |
131
|
|
|
* |
132
|
|
|
* @return boolean |
133
|
|
|
*/ |
134
|
24 |
|
public function hasCredentials() |
135
|
|
|
{ |
136
|
24 |
|
return ($this->credentials instanceof Credentials); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Check if the client has an active session. |
141
|
|
|
* |
142
|
|
|
* @return boolean |
143
|
|
|
*/ |
144
|
42 |
|
public function hasSession() |
145
|
|
|
{ |
146
|
42 |
|
return ($this->session instanceof SessionInterface && $this->session->isValid()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the guzzle client. If one doesn't exist |
151
|
|
|
* we'll create one. |
152
|
|
|
* |
153
|
|
|
* @return \GuzzleHttp\Client |
154
|
|
|
*/ |
155
|
20 |
|
public function http() |
156
|
|
|
{ |
157
|
20 |
|
if (!$this->http instanceof HttpClientInterface) { |
158
|
2 |
|
$this->http = new HttpClient; |
159
|
2 |
|
} |
160
|
|
|
|
161
|
20 |
|
return $this->http; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Login to the Container World LoginServlet. |
166
|
|
|
* |
167
|
|
|
* @param \XOrder\Credentials [$credentials] |
168
|
|
|
* @return \XOrder\Client |
169
|
|
|
*/ |
170
|
8 |
|
public function login(Credentials $credentials = null) |
171
|
|
|
{ |
172
|
8 |
|
if (!is_null($credentials)) { |
173
|
2 |
|
$this->credentials = $credentials; |
174
|
2 |
|
} |
175
|
|
|
|
176
|
8 |
|
$request = $this->makeRequest('POST', $this->getUri('xOrder/LogonServlet'), [ |
177
|
|
|
'Content-Type' => 'text/xml' |
178
|
8 |
|
], $this->getLogonMessage()); |
179
|
|
|
|
180
|
6 |
|
$response = $this->http()->send($request); |
181
|
|
|
|
182
|
6 |
|
if (!$this->responseOK($response)) { |
183
|
2 |
|
$this->logger()->error('XOrder\Client::login - Could not connect to xOrder'); |
184
|
2 |
|
throw new XOrderConnectionException('Could not connect to xOrder.'); |
185
|
|
|
} |
186
|
|
|
|
187
|
4 |
|
$this->session = new Session($response->getBody()->getContents()); |
188
|
4 |
|
$this->logger()->info('XOrder\Client::login - Successfully logged into xOrder Servlet'); |
189
|
|
|
|
190
|
4 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* End the session with the container world xOrder servlet. |
195
|
|
|
* |
196
|
|
|
* @return \XOrder\Response|boolean |
197
|
|
|
*/ |
198
|
42 |
|
public function logout() |
199
|
|
|
{ |
200
|
42 |
|
if (!$this->hasSession()) { |
201
|
32 |
|
return true; |
202
|
|
|
} |
203
|
|
|
|
204
|
10 |
|
$request = $this->makeRequest('POST', $this->getUri('xOrder/LogonServlet'), [ |
205
|
|
|
'Content-Type' => 'text/xml' |
206
|
10 |
|
], $this->getLogoutMessage()); |
207
|
|
|
|
208
|
10 |
|
$response = $this->http()->send($request); |
209
|
|
|
|
210
|
10 |
|
if (!$this->responseOK($response)) { |
211
|
4 |
|
$this->logger()->error('XOrder\Client::logout - Could not connect to xOrder'); |
212
|
4 |
|
throw new XOrderConnectionException('Could not connect to xOrder.'); |
213
|
|
|
} |
214
|
|
|
|
215
|
6 |
|
$this->session()->destroy(); |
216
|
6 |
|
$this->logger()->info('XOrder\Client::logout - Successfully logged out of xOrder Servlet'); |
217
|
|
|
|
218
|
6 |
|
return new Response($response->getBody()->getContents()); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Generate a new request messsage. |
223
|
|
|
* |
224
|
|
|
* @param string $method |
225
|
|
|
* @param string $uri |
226
|
|
|
* @param array $headers |
227
|
|
|
* @param string $body |
228
|
|
|
* @return \Psr\Http\Message\RequestInterface |
229
|
|
|
*/ |
230
|
18 |
|
public function makeRequest($method, $uri, $headers, $body) |
231
|
|
|
{ |
232
|
18 |
|
return new Request($method, $uri, $headers, $body); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Check if the HTTP request returns a valid response. |
237
|
|
|
* |
238
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
239
|
|
|
* @return boolean |
240
|
|
|
*/ |
241
|
16 |
|
public function responseOK(ResponseInterface $response) |
242
|
|
|
{ |
243
|
16 |
|
return $response->getStatusCode() === 200; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Send an order to the xOrder servlet. |
248
|
|
|
* |
249
|
|
|
* @param \XOrder\XOrder $xorder |
250
|
|
|
* @param boolean $validate |
251
|
|
|
* @return \XOrder\Response |
252
|
|
|
*/ |
253
|
4 |
|
public function send(XOrder $xorder, $validate = false) |
254
|
|
|
{ |
255
|
4 |
|
$contentType = $validate ? 'validate/xml' : 'text/xml'; |
256
|
|
|
|
257
|
4 |
|
$response = $this->http()->send( |
258
|
4 |
|
$this->makeRequest('POST', $this->getUri('xOrder/xOrderServlet'), [ |
259
|
4 |
|
'Content-Type' => $contentType, |
260
|
4 |
|
'userid' => $this->credentials()->getUsername(), |
261
|
4 |
|
'password' => $this->credentials()->getPassword(), |
262
|
4 |
|
'sessionId' => $this->session()->getId(), |
263
|
4 |
|
'bcldbNum' => $this->credentials()->getAccount(), |
264
|
|
|
'filename' => 'xorder.xml' |
265
|
4 |
|
], $xorder->getXML()) |
266
|
4 |
|
); |
267
|
|
|
|
268
|
4 |
|
if (!$this->responseOK($response)) { |
269
|
2 |
|
$this->logger()->error('XOrder\Client::send - Could not connect to xOrder'); |
270
|
2 |
|
throw new \Exception('Could not connect to xOrder.'); |
271
|
|
|
} |
272
|
|
|
|
273
|
2 |
|
$this->logger()->info('XOrder\Client::send - xOrder sent successfully to xOrder Servlet'); |
274
|
2 |
|
return new Response($response->getBody()->getContents()); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Get the session object. If one doesn't exist |
279
|
|
|
* we'll create one. |
280
|
|
|
* |
281
|
|
|
* @return \XOrder\Contracts\SessionInterface |
282
|
|
|
*/ |
283
|
12 |
|
public function session() |
284
|
|
|
{ |
285
|
12 |
|
if (!$this->hasSession()) { |
286
|
2 |
|
$this->logger()->error('XOrder\Client::session - You must login before sending xOrders'); |
287
|
2 |
|
throw new InvalidSessionException('You must login before sending xorders.'); |
288
|
|
|
} |
289
|
|
|
|
290
|
10 |
|
return $this->session; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Set the base URI. |
295
|
|
|
* |
296
|
|
|
* @param Psr\Http\Message\UriInterface $uri |
297
|
|
|
*/ |
298
|
4 |
|
public function setBaseUri(UriInterface $uri) |
299
|
|
|
{ |
300
|
4 |
|
$this->baseUri = $uri; |
301
|
4 |
|
$this->logger()->info("XOrder\Client::setBaseUri - Base URI Set to $uri"); |
302
|
4 |
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Set the HTP Client. |
306
|
|
|
* |
307
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
308
|
|
|
*/ |
309
|
18 |
|
public function setHttpClient(HttpClientInterface $client) |
310
|
|
|
{ |
311
|
18 |
|
$this->http = $client; |
312
|
18 |
|
$this->logger()->info('XOrder\Client::setHttpClient - HTTP Client sucessfully set'); |
313
|
18 |
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Send a request to the XOrder Servlet to validate |
317
|
|
|
* the passed order. |
318
|
|
|
* |
319
|
|
|
* @param \XOrder\XOrder $xorder |
320
|
|
|
* @return \XOrder\Response |
321
|
|
|
*/ |
322
|
4 |
|
public function validate(XOrder $xorder) |
323
|
|
|
{ |
324
|
4 |
|
return $this->send($xorder, true); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|