1 | <?php |
||
34 | class Client |
||
35 | { |
||
36 | /** |
||
37 | * @const Version of our client. |
||
38 | */ |
||
39 | const CLIENT_VERSION = '2.0'; |
||
40 | |||
41 | /** |
||
42 | * @const HTTP Method GET |
||
43 | */ |
||
44 | const HTTP_GET = 'GET'; |
||
45 | |||
46 | /** |
||
47 | * @const HTTP Method POST |
||
48 | */ |
||
49 | const HTTP_POST = 'POST'; |
||
50 | |||
51 | /** |
||
52 | * @const HTTP Method PUT |
||
53 | */ |
||
54 | const HTTP_PUT = 'PUT'; |
||
55 | |||
56 | /** |
||
57 | * @const HTTP Method DELETE |
||
58 | */ |
||
59 | const HTTP_DELETE = 'DELETE'; |
||
60 | |||
61 | /** |
||
62 | * Version of the remote API. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | private $_api_version = '2.0'; |
||
67 | |||
68 | /** |
||
69 | * @var string |
||
70 | */ |
||
71 | private $_api_endpoint = 'https://api.datatrics.com'; |
||
72 | |||
73 | /** |
||
74 | * @var string |
||
75 | */ |
||
76 | private $_api_key; |
||
77 | |||
78 | /** |
||
79 | * @var string |
||
80 | */ |
||
81 | private $_projectId; |
||
82 | |||
83 | /** |
||
84 | * @var Apikey |
||
85 | */ |
||
86 | public $Apikey; |
||
87 | |||
88 | /** |
||
89 | * @var Behavior |
||
90 | */ |
||
91 | public $Behavior; |
||
92 | |||
93 | /** |
||
94 | * @var Billing |
||
95 | */ |
||
96 | public $Billing; |
||
97 | |||
98 | /** |
||
99 | * @var Box |
||
100 | */ |
||
101 | public $Box; |
||
102 | |||
103 | /** |
||
104 | * @var Bucket |
||
105 | */ |
||
106 | public $Bucket; |
||
107 | |||
108 | /** |
||
109 | * @var Campaign |
||
110 | */ |
||
111 | public $Campaign; |
||
112 | |||
113 | /** |
||
114 | * @var Card |
||
115 | */ |
||
116 | public $Card; |
||
117 | |||
118 | /** |
||
119 | * @var Channel |
||
120 | */ |
||
121 | public $Channel; |
||
122 | |||
123 | /** |
||
124 | * @var Content |
||
125 | */ |
||
126 | public $Content; |
||
127 | |||
128 | /** |
||
129 | * @var Geo |
||
130 | */ |
||
131 | public $Geo; |
||
132 | |||
133 | /** |
||
134 | * @var Goal |
||
135 | */ |
||
136 | public $Goal; |
||
137 | |||
138 | /** |
||
139 | * @var Interaction |
||
140 | */ |
||
141 | public $Interaction; |
||
142 | |||
143 | /** |
||
144 | * @var Journey |
||
145 | */ |
||
146 | public $Journey; |
||
147 | |||
148 | /** |
||
149 | * @var Link |
||
150 | */ |
||
151 | public $Link; |
||
152 | |||
153 | /** |
||
154 | * @var NextBestAction |
||
155 | */ |
||
156 | public $NextBestAction; |
||
157 | |||
158 | /** |
||
159 | * @var Profile |
||
160 | */ |
||
161 | public $Profile; |
||
162 | |||
163 | /** |
||
164 | * @var Project |
||
165 | */ |
||
166 | public $Project; |
||
167 | |||
168 | /** |
||
169 | * @var Sale |
||
170 | */ |
||
171 | public $Sale; |
||
172 | |||
173 | /** |
||
174 | * @var Scorecard |
||
175 | */ |
||
176 | public $Scorecard; |
||
177 | |||
178 | /** |
||
179 | * @var Segment |
||
180 | */ |
||
181 | public $Segment; |
||
182 | |||
183 | /** |
||
184 | * @var Subscription |
||
185 | */ |
||
186 | public $Subscription; |
||
187 | |||
188 | /** |
||
189 | * @var Template |
||
190 | */ |
||
191 | public $Template; |
||
192 | |||
193 | /** |
||
194 | * @var Theme |
||
195 | */ |
||
196 | public $Theme; |
||
197 | |||
198 | /** |
||
199 | * @var Touchpoint |
||
200 | */ |
||
201 | public $Touchpoint; |
||
202 | |||
203 | /** |
||
204 | * @var Tracker |
||
205 | */ |
||
206 | public $Tracker; |
||
207 | |||
208 | /** |
||
209 | * @var Tric |
||
210 | */ |
||
211 | public $Tric; |
||
212 | |||
213 | /** |
||
214 | * @var Trigger |
||
215 | */ |
||
216 | public $Trigger; |
||
217 | |||
218 | /** |
||
219 | * @var User |
||
220 | */ |
||
221 | public $User; |
||
222 | |||
223 | /** |
||
224 | * @var Webhook |
||
225 | */ |
||
226 | public $Webhook; |
||
227 | |||
228 | /** |
||
229 | * Create a new API instance |
||
230 | * |
||
231 | * @param string $apiKey The API key |
||
232 | * @param string $projectId The Project id |
||
233 | */ |
||
234 | 18 | public function __construct($apiKey, $projectId = null) |
|
240 | |||
241 | /** |
||
242 | * Get the current API version |
||
243 | * |
||
244 | * @return string $api_version |
||
245 | */ |
||
246 | 10 | public function GetApiVersion() |
|
250 | |||
251 | /** |
||
252 | * Get the API endpoint |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | 11 | public function GetApiEndpoint() |
|
260 | |||
261 | /** |
||
262 | * Set the API endpoint |
||
263 | * |
||
264 | * @param string $api_endpoint |
||
265 | * @return Client |
||
266 | */ |
||
267 | 1 | public function SetApiEndpoint($api_endpoint) |
|
273 | |||
274 | /** |
||
275 | * Get the API key |
||
276 | * |
||
277 | * @return string $api_key |
||
278 | */ |
||
279 | 10 | public function GetApiKey() |
|
283 | |||
284 | /** |
||
285 | * Get the API key |
||
286 | * |
||
287 | * @param string $api_key |
||
288 | * @return Client |
||
289 | */ |
||
290 | 18 | public function SetApiKey($api_key) |
|
296 | |||
297 | /** |
||
298 | * Get the API project id |
||
299 | * |
||
300 | * @return string $projectId |
||
301 | */ |
||
302 | 18 | public function GetProjectId() |
|
306 | |||
307 | /** |
||
308 | * Set the API project id |
||
309 | * |
||
310 | * @param string $projectId |
||
311 | * @return Client |
||
312 | */ |
||
313 | 18 | public function SetProjectId($projectId) |
|
319 | |||
320 | /** |
||
321 | * Register Modules |
||
322 | * |
||
323 | * @return void |
||
324 | */ |
||
325 | 18 | private function _RegisterModules() |
|
357 | |||
358 | /** |
||
359 | * Define the HTTP headers |
||
360 | * |
||
361 | * @return array |
||
362 | */ |
||
363 | 8 | private function _GetHttpHeaders() |
|
375 | |||
376 | /** |
||
377 | * @throws \Exception |
||
378 | * @return boolean |
||
379 | */ |
||
380 | 7 | public function CheckApiKey() |
|
387 | |||
388 | /** |
||
389 | * @param $url |
||
390 | * @param null|array $payload |
||
391 | * @return string |
||
392 | */ |
||
393 | 9 | public function GetUrl($url, $payload = array()) |
|
394 | { |
||
395 | 9 | $url = $this->GetApiEndpoint()."/".$this->GetApiVersion().$url; |
|
396 | 9 | if (count($payload)) { |
|
397 | 2 | $url .= "?".http_build_query($payload); |
|
398 | } |
||
399 | 9 | return $url; |
|
400 | } |
||
401 | |||
402 | /** |
||
403 | * @param string $method HTTP Method |
||
404 | * @param string $url The url |
||
405 | * @param array $payload The Payload |
||
406 | * @throws \Exception |
||
407 | * @return array |
||
408 | */ |
||
409 | 8 | public function BuildRequest($method, $url, $payload = array()) |
|
410 | { |
||
411 | 8 | if($method == self::HTTP_POST || $method == self::HTTP_PUT){ |
|
412 | 3 | if (!$payload || !is_array($payload)) |
|
|
|||
413 | { |
||
414 | throw new \Exception('Invalid payload', 100); |
||
415 | } |
||
416 | $curlOptions = array( |
||
417 | 3 | CURLOPT_URL => $this->getUrl($url), |
|
418 | 3 | CURLOPT_CUSTOMREQUEST => strtoupper($method), |
|
419 | 3 | CURLOPT_POSTFIELDS => json_encode($payload), |
|
420 | ); |
||
421 | 5 | }elseif($method == self::HTTP_DELETE){ |
|
422 | $curlOptions = array( |
||
423 | 1 | CURLOPT_URL => $this->getUrl($url), |
|
424 | 1 | CURLOPT_CUSTOMREQUEST => self::HTTP_DELETE, |
|
425 | ); |
||
426 | }else{ |
||
427 | $curlOptions = array( |
||
428 | 4 | CURLOPT_URL => $this->getUrl($url, $payload), |
|
429 | 4 | CURLOPT_CUSTOMREQUEST => strtoupper($method) |
|
430 | ); |
||
431 | } |
||
432 | |||
433 | $curlOptions += array( |
||
434 | 8 | CURLOPT_RETURNTRANSFER => true, |
|
435 | 8 | CURLOPT_SSL_VERIFYPEER => false, |
|
436 | 8 | CURLOPT_HTTPHEADER => $this->_GetHttpHeaders() |
|
437 | ); |
||
438 | 8 | return $curlOptions; |
|
439 | } |
||
440 | |||
441 | /** |
||
442 | * @param string $method HTTP Method |
||
443 | * @param string $url The url |
||
444 | * @param array $payload The Payload |
||
445 | * @return mixed |
||
446 | * @throws \Exception |
||
447 | */ |
||
448 | 7 | public function SendRequest($method, $url, $payload = array()) |
|
449 | { |
||
450 | 7 | $this->CheckApiKey(); |
|
451 | 7 | $curlOptions = $this->BuildRequest($method, $url, $payload); |
|
452 | 7 | $curlHandle = curl_init(); |
|
453 | 7 | curl_setopt_array($curlHandle, $curlOptions); |
|
454 | 7 | $responseBody = curl_exec($curlHandle); |
|
455 | 7 | if (curl_errno($curlHandle)) |
|
456 | { |
||
457 | throw new \Exception('Curl error: ' . curl_error($curlHandle), curl_errno($curlHandle)); |
||
458 | } |
||
459 | 7 | $responseBody = json_decode($responseBody, true); |
|
460 | 7 | if (json_last_error() !== JSON_ERROR_NONE) { |
|
461 | throw new \Exception(json_last_error_msg()); |
||
462 | } |
||
463 | 7 | $responseCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE); |
|
464 | 7 | curl_close($curlHandle); |
|
465 | 7 | if ($responseCode < 200 || $responseCode > 299) |
|
466 | { |
||
467 | 7 | if($responseBody && array_key_exists('error', $responseBody)){ |
|
468 | 2 | throw new \Exception($responseBody['error']['message'], $responseCode); |
|
469 | } |
||
470 | 5 | if($responseBody && array_key_exists('message', $responseBody)){ |
|
471 | 5 | throw new \Exception($responseBody['message'], $responseCode); |
|
472 | } |
||
473 | throw new \Exception('Something went wrong'); |
||
474 | } |
||
475 | return $responseBody; |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * @param string $url |
||
480 | * @param array $payload |
||
481 | * @return mixed |
||
482 | */ |
||
483 | 2 | public function Post($url, $payload = array()) |
|
487 | |||
488 | /** |
||
489 | * @param string $url |
||
490 | * @param array $payload |
||
491 | * @return mixed |
||
492 | */ |
||
493 | 2 | public function Get($url, $payload = array()) |
|
497 | |||
498 | /** |
||
499 | * @param string $url |
||
500 | * @param array $payload |
||
501 | * @return mixed |
||
502 | */ |
||
503 | 1 | public function Put($url, $payload = array()) |
|
507 | |||
508 | /** |
||
509 | * @param string $url |
||
510 | * @param array $payload |
||
511 | * @return mixed |
||
512 | */ |
||
513 | 1 | public function Delete($url, $payload = array()) |
|
517 | } |
||
518 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.