1: <?php
2: 3: 4: 5: 6: 7:
8: namespace ApiAxle\Api;
9:
10: use ApiAxle\Shared\Config;
11: use ApiAxle\Shared\Utilities;
12: use ApiAxle\Shared\ItemList;
13:
14: 15: 16: 17: 18: 19: 20:
21: class Key
22: {
23: 24: 25: 26: 27:
28: protected $config;
29:
30: 31: 32: 33: 34:
35: protected $key;
36:
37: 38: 39: 40: 41:
42: protected $createdAt;
43:
44: 45: 46: 47: 48:
49: protected $updatedAt;
50:
51: 52: 53: 54: 55:
56: protected $sharedSecret;
57:
58: 59: 60: 61: 62:
63: protected $qpd = 172800;
64:
65: 66: 67: 68: 69:
70: protected $qps = 2;
71:
72: 73: 74: 75: 76:
77: protected $forApis;
78:
79: 80: 81: 82: 83:
84: protected $disabled = false;
85:
86: 87: 88: 89: 90: 91: 92:
93: protected $isNSA = 'false';
94:
95: 96: 97: 98: 99: 100: 101: 102: 103:
104: public function __construct($config=false,$key=false)
105: {
106: $this->config = new Config($config);
107: if($key){
108: $this->get($key);
109: }
110: }
111:
112: 113: 114: 115: 116:
117: public function getKey()
118: {
119: return $this->key;
120: }
121:
122: 123: 124: 125: 126:
127: public function setKey($key)
128: {
129: $this->key = $key;
130: }
131:
132: 133: 134: 135: 136: 137:
138: public function setData($data)
139: {
140: if(is_array($data)){
141: $data = json_decode(json_encode($data));
142: }
143:
144: 145: 146:
147: $this->createdAt = isset($data->createdAt) ? $data->createdAt : null;
148: $this->updatedAt = isset($data->updatedAt) ? $data->updatedAt : null;
149: $this->sharedSecret = isset($data->sharedSecret) ? $data->sharedSecret : null;
150: $this->qpd = isset($data->qpd) ? $data->qpd : $this->qpd;
151: $this->qps = isset($data->qps) ? $data->qps : $this->qps;
152: $this->forApis = isset($data->forApis) ? $data->forApis : null;
153: $this->disabled = isset($data->disabled) ? $data->disabled : $this->disabled;
154: $this->isNSA = isset($data->isNSA) ? $data->isNSA : $this->isNSA;
155:
156: return $this;
157: }
158:
159: 160: 161: 162: 163:
164: public function getData()
165: {
166: $data = array(
167: 'createdAt' => $this->createdAt,
168: 'updatedAt' => $this->updatedAt,
169: 'sharedSecret' => $this->sharedSecret,
170: 'qpd' => $this->qpd,
171: 'qps' => $this->qps,
172: 'forApis' => $this->forApis,
173: 'disabled' => $this->disabled,
174: 'isNSA' => $this->isNSA,
175: );
176:
177: return $data;
178: }
179:
180: 181: 182: 183: 184: 185:
186: public function getDataForApiCall()
187: {
188: $data = array(
189: 'qpd' => $this->qpd,
190: 'qps' => $this->qps,
191: 'disabled' => $this->disabled,
192: );
193:
194: if(is_array($this->forApis)){
195: $data['forApis'] = $this->forApis;
196: }
197: if(!is_null($this->sharedSecret)){
198: $data['sharedSecret'] = $this->sharedSecret;
199: }
200:
201: return $data;
202: }
203:
204: 205: 206: 207: 208: 209:
210: public function get($key)
211: {
212: if($key){
213: $apiPath = 'key/'.$key;
214: $request = Utilities::callApi($apiPath,'GET',null,$this->getConfig());
215: if($request){
216: $this->setKey($key);
217: $this->setData($request);
218: }
219: }
220:
221: return $this;
222: }
223:
224: 225: 226: 227: 228: 229: 230: 231:
232: public function create($key, $data=false)
233: {
234: $this->setKey($key);
235: if($data){
236: $this->setData($data);
237: }
238: if($this->isValid()){
239: $apiPath = 'key/'.$this->getKey().'?isNSA='.$this->isNSA;
240: $request = Utilities::callApi($apiPath, 'POST', $this->getDataForApiCall(),$this->getConfig());
241: if($request){
242: $this->get($key);
243: return $this;
244: } else {
245: throw new \ErrorException('Unable to create key',251);
246: }
247: }
248:
249: }
250:
251: 252: 253: 254: 255: 256: 257:
258: public function update($data)
259: {
260: if(!is_null($this->getKey()) && $this->isValid()){
261: $apiPath = 'key/'.$this->getKey();
262: $request = Utilities::callApi($apiPath,'PUT',$data,$this->getConfig());
263: if($request){
264: $this->get($this->getKey());
265: return $this;
266: } else {
267: throw new \ErrorException('Unable to update key',252);
268: }
269: } else {
270: throw new \ErrorException('A key value is required to update.',253);
271: }
272:
273: }
274:
275: 276: 277: 278: 279: 280: 281: 282:
283: public function delete($key=false)
284: {
285: if($key){
286: $this->setKey($key);
287: }
288: if(is_null($this->getKey())){
289: throw new \Exception('A key value is required to delete.',254);
290: } else {
291: $apiPath = 'key/'.$this->getKey();
292: $request = Utilities::callApi($apiPath, 'DELETE', null, $this->getConfig());
293: if($request){
294: return true;
295: } else {
296: throw new \ErrorException('Unable to delete key.', 255);
297: }
298: }
299: }
300:
301: 302: 303: 304: 305: 306: 307: 308: 309: 310:
311: public function getList($from=0, $to=100, $resolve='true')
312: {
313: $apiPath = 'keys';
314: $params = array(
315: 'from' => $from,
316: 'to' => $to,
317: 'resolve' => $resolve
318: );
319:
320: $keyList = new ItemList();
321: $request = Utilities::callApi($apiPath, 'GET', $params, $this->getConfig());
322: if($request){
323: foreach($request as $name => $data){
324: $key = new Key($this->getConfig());
325: $key->setKey($name);
326: $key->setData($data);
327: $keyList->addItem($key);
328: }
329: }
330:
331: return $keyList;
332: }
333:
334: 335: 336: 337: 338: 339: 340: 341:
342: public function getApiList($resolve='true')
343: {
344: if(is_null($this->getKey())){
345: throw new \Exception('A key value is required to fetch associated apis.',256);
346: } else {
347: $apiList = new ItemList();
348: $data = array('resolve' => $resolve);
349: $apiPath = 'key/'.$this->getKey().'/apis';
350: $request = Utilities::callApi($apiPath, 'GET', $data, $this->getConfig());
351: if($request){
352: foreach($request as $name => $value){
353: $api = new Api($this->getConfig());
354: $api->setName($name);
355: $api->setData($value);
356: $apiList->addItem($api);
357: }
358:
359: return $apiList;
360: } else {
361: throw new \ErrorException('Unable to get list of APIs for key.', 257);
362: }
363: }
364: }
365:
366: 367: 368: 369: 370: 371: 372: 373:
374: public function getApiCharts($granularity='minute')
375: {
376: if(is_null($this->getKey())){
377: throw new \Exception('A key value is required to fetch api charts.',258);
378: } else {
379: $apiPath = 'key/'.$this->getKey().'/apicharts';
380: $data = array('granularity' => $granularity);
381: $request = Utilities::callApi($apiPath, 'GET', $data,$this->getConfig());
382: if($request){
383: return $request;
384: } else {
385: throw new Exception('Unable to fetch api charts', 259);
386: }
387: }
388: }
389:
390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401:
402: public function getStats($timestart=false, $timeend=false,
403: $granularity='minute',$format_timeseries='true',
404: $format_timestamp='epoch_seconds', $forapi=false)
405: {
406: if(is_null($this->getKey())){
407: throw new \Exception('A key value is required to get stats.',260);
408: } else {
409:
410: $data = array(
411: 'granularity' => $granularity,
412: 'format_timeseries' => $format_timeseries,
413: 'format_timestamp' => $format_timestamp,
414: );
415: if($timestart){
416: $data['from'] = $timestart;
417: }
418: if($timeend){
419: $data['to'] = $timeend;
420: }
421:
422: if($forapi && is_string($forapi)){
423: $data['forapi'] = $forapi;
424: } elseif($forapi && $forapi instanceof ApiAxle\Api\Api){
425: $data['forapi'] = $forapi->getName();
426: }
427:
428: $apiPath = 'key/'.$this->getKey().'/stats';
429: $request = Utilities::callApi($apiPath, 'GET', $data,$this->getConfig());
430: if($request){
431: return $request;
432: } else {
433: throw new \Exception('Unable to get stats for key',261);
434: }
435: }
436: }
437:
438: 439: 440: 441: 442: 443: 444:
445: public function getCharts($granularity='minute')
446: {
447: $apiPath = 'keys/charts';
448: $data = array('granularity' => $granularity);
449: $request = Utilities::callApi($apiPath, 'GET', $data,$this->getConfig());
450: if($request){
451: return $request;
452: } else {
453: throw new \Exception('Unable to get charts for key.',262);
454: }
455: }
456:
457: 458: 459: 460: 461:
462: public function getConfig()
463: {
464: return $this->config;
465: }
466:
467: 468: 469: 470: 471: 472:
473: public function isValid()
474: {
475: if(!is_null($this->getKey())){
476: return true;
477: } else {
478: throw new \Exception('A key value is required to interact with keys.',250);
479: }
480: }
481:
482: }
483: