1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @category Brownie/CartsGuru |
4
|
|
|
* @author Brownie <[email protected]> |
5
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Brownie\CartsGuru\HTTPClient; |
9
|
|
|
|
10
|
|
|
use Brownie\CartsGuru\Exception\InvalidCodeException; |
11
|
|
|
use Brownie\CartsGuru\Exception\JsonException; |
12
|
|
|
use Brownie\CartsGuru\Config; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* HTTP client. |
16
|
|
|
*/ |
17
|
|
|
class HTTPClient |
18
|
|
|
{ |
19
|
|
|
const HTTP_METHOD_GET = 'GET'; |
20
|
|
|
|
21
|
|
|
const HTTP_METHOD_POST = 'POST'; |
22
|
|
|
|
23
|
|
|
const HTTP_METHOD_PUT = 'PUT'; |
24
|
|
|
|
25
|
|
|
const HTTP_METHOD_DELETE = 'DELETE'; |
26
|
|
|
|
27
|
|
|
const HTTP_CODE_200 = 200; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* CartsGuru configuration. |
31
|
|
|
* @var Config |
32
|
|
|
*/ |
33
|
|
|
private $config; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* HTTP client. |
37
|
|
|
* @var Client |
38
|
|
|
*/ |
39
|
|
|
private $client; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Sets incoming data. |
43
|
|
|
* |
44
|
|
|
* @param Client $client HTTP client. |
45
|
|
|
* @param Config $config CartsGuru configuration. |
46
|
|
|
*/ |
47
|
8 |
|
public function __construct(Client $client, Config $config) |
48
|
|
|
{ |
49
|
|
|
$this |
50
|
8 |
|
->setClient($client) |
51
|
8 |
|
->setConfig($config); |
52
|
8 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sets the request client. |
56
|
|
|
* Returns the current object. |
57
|
|
|
* |
58
|
|
|
* @param Client $client |
59
|
|
|
* |
60
|
|
|
* @return self |
61
|
|
|
*/ |
62
|
8 |
|
private function setClient(Client $client) |
63
|
|
|
{ |
64
|
8 |
|
$this->client = $client; |
65
|
8 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Sets the CartsGuru configuration. |
70
|
|
|
* Returns the current object. |
71
|
|
|
* |
72
|
|
|
* @param Config $config CartsGuru configuration. |
73
|
|
|
* |
74
|
|
|
* @return self |
75
|
|
|
*/ |
76
|
8 |
|
private function setConfig(Config $config) |
77
|
|
|
{ |
78
|
8 |
|
$this->config = $config; |
79
|
8 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns request client. |
84
|
|
|
* |
85
|
|
|
* @return Client |
86
|
|
|
*/ |
87
|
5 |
|
private function getClient() |
88
|
|
|
{ |
89
|
5 |
|
return $this->client; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns CartsGuru configuration. |
94
|
|
|
* |
95
|
|
|
* @return Config |
96
|
|
|
*/ |
97
|
5 |
|
private function getConfig() |
98
|
|
|
{ |
99
|
5 |
|
return $this->config; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Performs a network request in CartsGuru. |
104
|
|
|
* Returns the response from CartsGuru. |
105
|
|
|
* |
106
|
|
|
* @param int $checkHTTPCode Checked HTTP Code |
107
|
|
|
* @param string $endpoint The access endpoint to the resource. |
108
|
|
|
* @param array $data An array of data to send. |
109
|
|
|
* @param string $method Query Method. |
110
|
|
|
* @param boolean $ignoreEmptyResponse Semaphore of ignoring an empty response. |
111
|
|
|
* |
112
|
|
|
* @throws InvalidCodeException |
113
|
|
|
* @throws JsonException |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
5 |
|
public function request( |
118
|
|
|
$checkHTTPCode, |
119
|
|
|
$endpoint, |
120
|
|
|
$data = array(), |
121
|
|
|
$method = self::HTTP_METHOD_GET, |
122
|
|
|
$ignoreEmptyResponse = false |
123
|
|
|
) { |
124
|
5 |
|
$apiUrl = $this->getApiUrl($endpoint); |
125
|
|
|
|
126
|
5 |
|
if (is_object($data)) { |
127
|
2 |
|
$data = $data->toArray(); |
128
|
|
|
} |
129
|
|
|
|
130
|
5 |
|
$query = new Query(); |
131
|
|
|
$query |
132
|
5 |
|
->setApiUrl($apiUrl) |
133
|
5 |
|
->setXAuthKey($this->getConfig()->getApiAuthKey()) |
134
|
5 |
|
->setData($data) |
135
|
5 |
|
->setMethod($method) |
136
|
5 |
|
->setTimeOut($this->getConfig()->getTimeOut()); |
137
|
|
|
|
138
|
|
|
list($responseBody, $httpCode, $runtime) = $this |
139
|
5 |
|
->getClient() |
140
|
5 |
|
->httpRequest($query); |
141
|
|
|
|
142
|
5 |
|
$response = $this->parseResponse($ignoreEmptyResponse, $responseBody); |
143
|
|
|
|
144
|
4 |
|
$this->checkingHTTPCode( |
145
|
4 |
|
$checkHTTPCode, |
146
|
4 |
|
$httpCode, |
147
|
4 |
|
is_array($response) && isset($response['error']) ? ', ' . $response['error'] : '' |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
return array( |
151
|
3 |
|
'response' => $response, |
152
|
3 |
|
'runtime' => $runtime, |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns the text of the JSON parsing error. |
158
|
|
|
* |
159
|
|
|
* @param integer $errorId Error ID |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
2 |
|
public function getJsonLastErrorMsg($errorId) |
164
|
|
|
{ |
165
|
2 |
|
$message = 'Unknown error'; |
166
|
|
|
|
167
|
|
|
$errors = array( |
168
|
2 |
|
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
169
|
2 |
|
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', |
170
|
2 |
|
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', |
171
|
2 |
|
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
172
|
2 |
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' |
173
|
|
|
); |
174
|
|
|
|
175
|
2 |
|
if (!empty($errors[$errorId])) { |
176
|
2 |
|
$message = $errors[$errorId]; |
177
|
|
|
} |
178
|
|
|
|
179
|
2 |
|
return $message; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Creates a complete URL to the resource. |
184
|
|
|
* Returns the created URL. |
185
|
|
|
* |
186
|
|
|
* @param string $endpoint API Endpoint. |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
5 |
|
private function getApiUrl($endpoint) |
191
|
|
|
{ |
192
|
5 |
|
return implode( |
193
|
5 |
|
'/', |
194
|
|
|
array( |
195
|
5 |
|
$this->getConfig()->getApiUrl(), |
196
|
5 |
|
$endpoint |
197
|
|
|
) |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Checking HTTP Code. |
203
|
|
|
* |
204
|
|
|
* @param int $checkHTTPCode Verification HTTP code. |
205
|
|
|
* @param int $httpCode Verifiable HTTP code. |
206
|
|
|
* @param string $message Error message. |
207
|
|
|
* |
208
|
|
|
* @throws InvalidCodeException |
209
|
|
|
*/ |
210
|
4 |
|
private function checkingHTTPCode($checkHTTPCode, $httpCode, $message) |
211
|
|
|
{ |
212
|
4 |
|
if ($checkHTTPCode != $httpCode) { |
213
|
1 |
|
throw new InvalidCodeException($httpCode . ($message)); |
214
|
|
|
} |
215
|
3 |
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Parsing with the API response. |
219
|
|
|
* |
220
|
|
|
* @param bool $ignoreEmptyResponse A sign of ignoring an empty response. |
221
|
|
|
* @param string $responseBody HTTP response from the API. |
222
|
|
|
* |
223
|
|
|
* @return array |
224
|
|
|
* |
225
|
|
|
* @throws JsonException |
226
|
|
|
*/ |
227
|
5 |
|
private function parseResponse($ignoreEmptyResponse, $responseBody) |
228
|
|
|
{ |
229
|
5 |
|
$response = array(); |
230
|
5 |
|
if (!$ignoreEmptyResponse && !empty($responseBody)) { |
231
|
4 |
|
$response = json_decode($responseBody, true); |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Parse Json checking. |
235
|
|
|
*/ |
236
|
4 |
|
if (json_last_error() != JSON_ERROR_NONE) { |
237
|
1 |
|
throw new JsonException($this->getJsonLastErrorMsg(json_last_error())); |
238
|
|
|
} |
239
|
|
|
} |
240
|
4 |
|
return $response; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|