1 | <?php |
||
2 | |||
3 | /** |
||
4 | * ================================== |
||
5 | * Responsible PHP API |
||
6 | * ================================== |
||
7 | * |
||
8 | * @link Git https://github.com/vince-scarpa/responsibleAPI.git |
||
9 | * |
||
10 | * @api Responible API |
||
11 | * @package responsible\ |
||
12 | * @version 1.2 |
||
13 | * |
||
14 | * @author Vince scarpa <[email protected]> |
||
15 | * |
||
16 | */ |
||
17 | |||
18 | namespace responsible; |
||
19 | |||
20 | use responsible\core as responsibleCore; |
||
21 | use responsible\core\configuration; |
||
22 | use responsible\core\user; |
||
23 | use responsible\core\headers; |
||
24 | use responsible\core\exception\responsibleException; |
||
25 | |||
26 | class responsible |
||
27 | { |
||
28 | use \responsible\core\traits\optionsTrait; |
||
29 | |||
30 | /** |
||
31 | * [$config Variable store for the Responsible API config set] |
||
32 | * @var array |
||
33 | */ |
||
34 | private $config; |
||
35 | |||
36 | /** |
||
37 | * [$defaults Variable store for the Responsible API defaults set] |
||
38 | * @var array |
||
39 | */ |
||
40 | private $defaults; |
||
41 | |||
42 | /** |
||
43 | * [$server Core server object] |
||
44 | * @var object |
||
45 | */ |
||
46 | private $server; |
||
47 | |||
48 | /** |
||
49 | * [$response Response data] |
||
50 | * @var array|object |
||
51 | */ |
||
52 | private static $response; |
||
53 | |||
54 | /** |
||
55 | * [$requestType Header request response format] |
||
56 | * @var string |
||
57 | */ |
||
58 | private $requestType = 'json'; |
||
59 | |||
60 | /** |
||
61 | * [$requestType Request limit] |
||
62 | * @var integer |
||
63 | */ |
||
64 | private $requestLimit; |
||
65 | |||
66 | /** |
||
67 | * [$requestType Request window rate] |
||
68 | * @var string|integer |
||
69 | */ |
||
70 | private $requestRateWindow; |
||
71 | |||
72 | /** |
||
73 | * [__construc :: Construct the Responsible API] |
||
74 | * @param array $DEFAULTS |
||
75 | * environment settings |
||
76 | * @param array $options |
||
77 | * API options |
||
78 | */ |
||
79 | public function __construct(array $options = [], $initiate = true) |
||
80 | { |
||
81 | /** |
||
82 | * Initiate the Responsible API configuration and options |
||
83 | */ |
||
84 | $this->setConfig($options); |
||
85 | |||
86 | $this->setRequestType(($options['requestType']) ?? 'json'); |
||
87 | |||
88 | $this->setRateLimit(($options['rateLimit']) ?? 100); |
||
89 | |||
90 | $this->setRateWindow(($options['rateWindow']) ?? 'MINUTE'); |
||
91 | |||
92 | if ($initiate) { |
||
93 | $this->run(); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Run the server |
||
99 | * @return mixed |
||
100 | */ |
||
101 | public function run() |
||
102 | { |
||
103 | /** |
||
104 | * Initiate the Responsible API server |
||
105 | */ |
||
106 | $this->server(); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * [setConfig Set the ResponsibleAPI configuration] |
||
111 | * @return void |
||
112 | */ |
||
113 | private function setConfig($options) |
||
114 | { |
||
115 | $config = new configuration\config(); |
||
116 | $config->baseApiRoot(dirname(__DIR__)); |
||
117 | $config->responsibleDefault($options); |
||
118 | |||
119 | $this->setOptions($config->getOptions()); |
||
120 | $this->config($config->getConfig()); |
||
121 | $this->defaults($config->getDefaults()); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * [server :: Initiate the Responsible core server] |
||
126 | * @return void |
||
127 | */ |
||
128 | private function server() |
||
129 | { |
||
130 | $options = $this->getOptions(); |
||
131 | $route = ($options['route']) ?? ''; |
||
132 | |||
133 | /** |
||
134 | * [$this->server :: Set the a new API server object] |
||
135 | * @var responsibleCore\server [Alias for responsible\core] |
||
136 | */ |
||
137 | $this->server = new responsibleCore\server( |
||
138 | $this->getConfig(), |
||
139 | $options, |
||
140 | true |
||
141 | ); |
||
142 | |||
143 | // Set the header request format |
||
144 | $this->server->requestType($this->getRequestType()); |
||
145 | |||
146 | // Authenticate the API connections |
||
147 | try { |
||
148 | $this->server->authenticate(); |
||
149 | } catch (responsibleException | \Exception $e) { |
||
150 | if (($options['throwException'] ?? null) === true) { |
||
151 | throw new responsibleException($e->getMessage(), $e->getCode()); |
||
152 | } |
||
153 | self::$response = $e->getMessage(); |
||
0 ignored issues
–
show
|
|||
154 | return; |
||
155 | } |
||
156 | |||
157 | // Set the rate limit and timeframe for API connection limits |
||
158 | try { |
||
159 | $this->server->rateLimit( |
||
160 | $this->getRateLimit(), |
||
161 | $this->getRateWindow() |
||
162 | ); |
||
163 | } catch (responsibleException | \Exception $e) { |
||
164 | if (($options['throwException'] ?? null) === true) { |
||
165 | throw new responsibleException($e->getMessage(), $e->getCode()); |
||
166 | } |
||
167 | self::$response = $e->getMessage(); |
||
168 | return; |
||
169 | } |
||
170 | |||
171 | // Build the APIs internal router |
||
172 | try { |
||
173 | $this->server->route($route); |
||
174 | } catch (responsibleException | \Exception $e) { |
||
175 | if (($options['throwException'] ?? null) === true) { |
||
176 | throw new responsibleException($e->getMessage(), $e->getCode()); |
||
177 | } |
||
178 | self::$response = $e->getMessage(); |
||
179 | return; |
||
180 | } |
||
181 | |||
182 | self::$response = $this->server->response(); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * [getApiData Get the Responsible API router data] |
||
187 | * @return array |
||
188 | */ |
||
189 | public function Router() |
||
190 | { |
||
191 | return $this->server->getRouter(); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * [response :: Get the final API response as an output] |
||
196 | * @return object|array |
||
197 | */ |
||
198 | public function responseData($debug = 'coredata') |
||
199 | { |
||
200 | return $this->server->response($debug); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * [config Set the Responsible API configuration] |
||
205 | * @return void |
||
206 | */ |
||
207 | private function config($config) |
||
208 | { |
||
209 | $this->config = $config; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * [getConfig Get the stored Responsible API configuration] |
||
214 | * @return array |
||
215 | */ |
||
216 | public function getConfig() |
||
217 | { |
||
218 | return $this->config; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * [defaults Set the Responsible API defaults] |
||
223 | * Configuration and Options merged |
||
224 | * @return void |
||
225 | */ |
||
226 | private function defaults($defaults) |
||
227 | { |
||
228 | $this->defaults = $defaults; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * [getDefaults Get the stored Responsible API defaults] |
||
233 | * @return array |
||
234 | */ |
||
235 | public function getDefaults() |
||
236 | { |
||
237 | return $this->defaults; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * [setRequestType :: Header request response format] |
||
242 | * @param string $type |
||
243 | */ |
||
244 | private function setRequestType($type) |
||
245 | { |
||
246 | $this->requestType = $type; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * [getRequestType :: Get the header request format] |
||
251 | * @return string |
||
252 | */ |
||
253 | private function getRequestType() |
||
254 | { |
||
255 | return $this->requestType; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * [setRateLimit :: Set the Responsible API ratelimit] |
||
260 | * How many API requests a connection is allowed |
||
261 | * in a certain timeframe |
||
262 | * |
||
263 | * EG: 100 requests per minute |
||
264 | * |
||
265 | * @param integer $limit |
||
266 | */ |
||
267 | private function setRateLimit($limit) |
||
268 | { |
||
269 | $this->requestLimit = $limit; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * [getRateLimit :: Get the Responsible API ratelimit] |
||
274 | * @return integer |
||
275 | */ |
||
276 | private function getRateLimit() |
||
277 | { |
||
278 | return $this->requestLimit; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * [setRateWindow Set the Responsible API window for rate limits] |
||
283 | * The window is a range set for API connection requests |
||
284 | * |
||
285 | * @see setRateLimit() |
||
286 | * @param string|integer $frame |
||
287 | */ |
||
288 | private function setRateWindow($frame) |
||
289 | { |
||
290 | if (is_numeric($frame)) { |
||
291 | $this->requestRateWindow = $frame; |
||
292 | return; |
||
293 | } |
||
294 | |||
295 | $this->requestRateWindow = $frame; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * [getRateWindow Get the timeframe set for rate limits] |
||
300 | * @return integer|string |
||
301 | */ |
||
302 | private function getRateWindow() |
||
303 | { |
||
304 | return $this->requestRateWindow; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * ************************************** |
||
309 | * PUBLIC FACTORY METHODS |
||
310 | * *************************************** |
||
311 | */ |
||
312 | |||
313 | /** |
||
314 | * [API Initiate the Responsible API] |
||
315 | * @param array $options |
||
316 | * @return self|object |
||
317 | */ |
||
318 | public static function API(array $options = []) |
||
319 | { |
||
320 | return new self($options); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * [unauthorised Set a custom unauthorized header] |
||
325 | * @return void |
||
326 | */ |
||
327 | public static function unauthorised() |
||
328 | { |
||
329 | (new headers\header())->unauthorised(); |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * [response Get the Responsible API response] |
||
334 | * @return array|object|void |
||
335 | */ |
||
336 | public static function response($echo = false) |
||
337 | { |
||
338 | if ($echo) { |
||
339 | print_r(self::$response); |
||
340 | return; |
||
341 | } |
||
342 | return self::$response; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * [createUser Create a new user access] |
||
347 | * @param string $name |
||
348 | * @param string $mail |
||
349 | * @return array |
||
350 | */ |
||
351 | public static function createUser($name, $mail, array $options = []) |
||
352 | { |
||
353 | return (new user\user()) |
||
354 | ->setOptions($options) |
||
355 | ->credentials($name, $mail) |
||
356 | ->create() |
||
357 | ; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * [updateUser Update a user account] |
||
362 | * @param array $properties |
||
363 | * @return array |
||
364 | */ |
||
365 | public static function updateUser($properties) |
||
366 | { |
||
367 | return (new user\user()) |
||
368 | ->update($properties) |
||
369 | ; |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * [loadUser Load a stored account] |
||
374 | * @param integer|string $property |
||
375 | * @param string $type |
||
376 | * @return array |
||
377 | */ |
||
378 | public static function loadUser($property, array $options = []) |
||
379 | { |
||
380 | $loadBy = (isset($options['loadBy']) && !empty($options['loadBy'])) |
||
381 | ? $options['loadBy'] : 'account_id'; |
||
382 | |||
383 | $getJWT = (isset($options['getJWT']) && is_bool($options['getJWT'])) |
||
384 | ? $options['getJWT'] : true; |
||
385 | |||
386 | $getSecretAppend = (isset($options['secret']) && ($options['secret'] == 'append') ) |
||
387 | ? $options['secret'] : false; |
||
388 | |||
389 | return (new user\user()) |
||
390 | ->setOptions($options) |
||
391 | ->load( |
||
392 | $property, |
||
393 | array( |
||
394 | 'loadBy' => $loadBy, |
||
395 | 'getJWT' => $getJWT, |
||
396 | 'secret' => $getSecretAppend |
||
397 | ) |
||
398 | ); |
||
399 | } |
||
400 | } |
||
401 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..