Overview

Namespaces

  • ApiAxle
    • Api
    • Shared
  • ApiAxleTest
    • Api
    • Shared
  • PHP

Classes

  • Api
  • Key
  • Keyring
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: /**
  3:  * ApiAxle (https://github.com/fillup/apiaxle-module/)
  4:  *
  5:  * @link      https://github.com/fillup/apiaxle-module for the canonical source repository
  6:  * @license   MIT
  7:  */
  8: namespace ApiAxle\Api;
  9: 
 10: use ApiAxle\Shared\Config;
 11: use ApiAxle\Shared\Utilities;
 12: use ApiAxle\Shared\ItemList;
 13: 
 14: /**
 15:  * ApiAxle\Api\Keyring class
 16:  * 
 17:  * Wraps Keyring related calls to the ApiAxle API
 18:  * 
 19:  * @author Phillip Shipley <phillip@phillipshipley.com>
 20:  */
 21: class Keyring
 22: {
 23:     /**
 24:      * Configuration data
 25:      * 
 26:      * @var \ApiAxle\Shared\Config
 27:      */
 28:     protected $config;
 29:     
 30:     /**
 31:      * Keyring Name
 32:      * 
 33:      * @var string $name
 34:      */
 35:     protected $name;
 36:     
 37:     /**
 38:      * Created at timestamp, set automatically by API
 39:      * 
 40:      * @var integer
 41:      */
 42:     protected $createdAt;
 43:     
 44:     /**
 45:      * Updated at timestamp, set automatically by API
 46:      * 
 47:      * @var integer
 48:      */
 49:     protected $updatedAt;
 50:     
 51:     /**
 52:      * Construct new Keyring object
 53:      * 
 54:      * If $name is provided it will fetch the created/updated values from API
 55:      * 
 56:      * @param ApiAxle\Shared\Config $config
 57:      * @param string $name
 58:      */
 59:     public function __construct($config=false,$name=false) 
 60:     {
 61:         $this->config = new Config($config);
 62:         if($name){
 63:             $this->get($name);
 64:         }
 65:     }
 66:     
 67:     /**
 68:      * Return current name value
 69:      * 
 70:      * @return string
 71:      */
 72:     public function getName()
 73:     {
 74:         return $this->name;
 75:     }
 76:     
 77:     /**
 78:      * Set name value
 79:      * 
 80:      * @param string $name
 81:      */
 82:     public function setName($name)
 83:     {
 84:         $this->name = $name;
 85:     }
 86:     
 87:     /**
 88:      * Set object properties
 89:      * 
 90:      * @param array $data
 91:      * @return \ApiAxle\Api\Keyring
 92:      */
 93:     public function setData($data)
 94:     {
 95:         if(is_array($data)){
 96:             $data = json_decode(json_encode($data));
 97:         }
 98:         
 99:         /**
100:          * @todo Refactor to use setters for validation?
101:          */
102:         $this->createdAt = isset($data->createdAt) ? $data->createdAt : null;
103:         $this->updatedAt = isset($data->updatedAt) ? $data->updatedAt : null;
104:         
105:         return $this;
106:     }
107:     
108:     /**
109:      * Fetch keyring information from ApiAxle API and update object properties
110:      * 
111:      * @param string $key
112:      * @return \ApiAxle\Api\Keyring
113:      */
114:     public function get($name)
115:     {
116:         if($name){
117:             $apiPath = 'keyring/'.$name;
118:             $request = Utilities::callApi($apiPath,'GET',null,$this->getConfig());
119:             if($request){
120:                 $this->setName($name);
121:                 $this->setData($request);
122:             }
123:         }
124:         
125:         return $this;
126:     }
127:     
128:     /**
129:      * Create a new keyring
130:      * 
131:      * @param string $name
132:      * @param array $keys
133:      * @return \ApiAxle\Api\Key
134:      * @throws \ErrorException
135:      */
136:     public function create($name,$keys=false)
137:     {
138:         $this->setName($name);
139:         if($this->isValid()){
140:             $apiPath = 'keyring/'.$this->getName();
141:             $data = array('createdAt' => time());
142:             $request = Utilities::callApi($apiPath, 'POST', $data, $this->getConfig());
143:             if($request){
144:                 $this->get($name);
145:                 if($keys){
146:                     $this->linkKeys($keys);
147:                 }
148:                 return $this;
149:             } else {
150:                 throw new \ErrorException('Unable to create keyring',270);
151:             }
152:         }
153:     }
154:     
155:     /**
156:      * Get a list of all Keyrings
157:      * 
158:      * Parameters $from and $to are used for pagination of results
159:      * 
160:      * @param integer $from
161:      * @param integer $to
162:      * @param string $resolve
163:      * @return \ApiAxle\Shared\ItemList
164:      */
165:     public function getList($from=0, $to=100, $resolve='true')
166:     {
167:         $apiPath = 'keyrings';
168:         $params = array(
169:             'from' => $from,
170:             'to' => $to,
171:             'resolve' => $resolve
172:         );
173:         
174:         $keyringList = new ItemList();
175:         $request = Utilities::callApi($apiPath, 'GET', $params, $this->getConfig());
176:         if($request){
177:             foreach($request as $name => $data){
178:                 $keyring = new Keyring($this->getConfig());
179:                 $keyring->setName($name);
180:                 $keyring->setData($data);
181:                 $keyringList->addItem($keyring);
182:             }
183:         }
184:         
185:         return $keyringList;
186:     }
187:     
188:     
189:     /**
190:      * Delete a keyring
191:      * 
192:      * @param string $name
193:      * @return boolean
194:      * @throws \Exception
195:      * @throws \ErrorException
196:      */
197:     public function delete($name=false)
198:     {
199:         if($name){
200:             $this->setName($name);
201:         }
202:         if(is_null($this->getName())){
203:             throw new \Exception('A name is required to delete.',271);
204:         } else {
205:             $apiPath = 'keyring/'.$this->getName();
206:             $request = Utilities::callApi($apiPath, 'DELETE', null, $this->getConfig());
207:             if($request){
208:                 return true;
209:             } else {
210:                 throw new \ErrorException('Unable to delete keyring.', 272);
211:             }
212:         }
213:     }
214:     
215:     /**
216:      * Get a list of Keys with access to this Keyring
217:      * 
218:      * Parameters $from and $to are used for paginating through results
219:      * 
220:      * @param integer $from Default is 0
221:      * @param integer $to Default is 100
222:      * @param string $resolve Wether or not get Key details. Default is 'true'
223:      * @return \ApiAxle\Shared\ItemList
224:      * @throws \Exception
225:      * @throws \ErrorException
226:      */
227:     public function getKeyList($from=0, $to=100, $resolve='true')
228:     {
229:         if(is_null($this->getName())){
230:             throw new \Exception('An Keyring name is required to get keylist.',273);
231:         } else {
232:             $apiPath = 'keyring/'.$this->getName().'/keys';
233:             $data = array(
234:                 'from' => $from,
235:                 'to' => $to,
236:                 'resolve' => $resolve
237:             );
238:             $keyList = new ItemList();
239:             $request = Utilities::callApi($apiPath, 'GET', $data,$this->getConfig());
240:             if($request){
241:                 foreach($request as $item => $value){
242:                     $key = new Key($this->getConfig());
243:                     $key->setKey($item);
244:                     $key->setData($value);
245:                     $keyList->addItem($key);
246:                 }
247:                 return $keyList;
248:             } else {
249:                 throw new \ErrorException('Unable to retrieve keys for Keyring.', 274);
250:             }
251:         }
252:     }
253:     
254:     /**
255:      * Convenience method to pass an array of keys to be linked after creating
256:      * keyring.
257:      * 
258:      * @param array $keys
259:      * @return \ApiAxle\Api\Keyring
260:      */
261:     public function linkKeys($keys)
262:     {
263:         foreach($keys as $key){
264:             $this->linkKey($key);
265:         }
266:         
267:         return $this;
268:     }
269:     
270:     /**
271:      * Link a Key to this Keyring
272:      * 
273:      * @param \ApiAxle\Api\Key $key
274:      * @return \ApiAxle\Api\Keyring
275:      * @throws \Exception
276:      */
277:     public function linkKey($key)
278:     {
279:         if(is_null($this->getName())){
280:             throw new \Exception('An API name is required to link a key.',275);
281:         } else {
282:             $apiPath = 'keyring/'.$this->getName().'/linkkey/';
283:             if(is_string($key)){
284:                 $apiPath .= $key;
285:             } elseif($key instanceof Key){
286:                 $apiPath .= $key->getKey();
287:             } else {
288:                 throw new \Exception('Key must be a string or instance of ApiAxle\Api\Key',212);
289:             }
290:             
291:             $request = Utilities::callApi($apiPath, 'PUT',null,$this->getConfig());
292:             if($request){
293:                 return $this;
294:             } else {
295:                 throw new \Exception('Unable to link key',276);
296:             }
297:         }
298:     }
299:     
300:     /**
301:      * Unlink a Key from this Keyring
302:      * 
303:      * @param \ApiAxle\Api\Key $key
304:      * @return \ApiAxle\Api\Keyring
305:      * @throws \Exception
306:      */
307:     public function unLinkKey($key)
308:     {
309:         if(is_null($this->getName())){
310:             throw new \Exception('A Keyring name is required to unlink a key.',277);
311:         } else {
312:             $apiPath = 'keyring/'.$this->getName().'/unlinkkey/';
313:             if(is_string($key)){
314:                 $apiPath .= $key;
315:             } elseif($key instanceof Key){
316:                 $apiPath .= $key->getKey();
317:             } else {
318:                 throw new \Exception('Key must be a string or instance of ApiAxle\Api\Key',215);
319:             }
320:             
321:             $request = Utilities::callApi($apiPath, 'PUT',null,$this->getConfig());
322:             if($request){
323:                 return $this;
324:             } else {
325:                 throw new \Exception('Unable to unlink key',278);
326:             }
327:         }
328:     }
329:     
330:     /**
331:      * Get real time hits for a keyring
332:      * 
333:      * @param integer $timestart
334:      * @param integer $timeend
335:      * @param string $granularity
336:      * @param string $format_timeseries
337:      * @param string $format_timestamp
338:      * @param \ApiAxle\Api\Key $forkey
339:      * @param \ApiAxle\Api\Api $forapi
340:      * @return type
341:      * @throws \Exception
342:      */
343:     public function getStats($timestart=false, $timeend=false, 
344:             $granularity='minute',$format_timeseries='true',
345:             $format_timestamp='epoch_seconds', $forkey=false, $forapi=false)
346:     {
347:         if(is_null($this->getName())){
348:             throw new \Exception('A keyring name is required to get stats.',279);
349:         } else {
350:             
351:             $data = array(
352:                 'granularity' => $granularity,
353:                 'format_timeseries' => $format_timeseries,
354:                 'format_timestamp' => $format_timestamp,
355:             );
356:             if($timestart){
357:                 $data['from'] = $timestart;
358:             }
359:             if($timeend){
360:                 $data['to'] = $timeend;
361:             }
362:             
363:             if($forkey && is_string($forkey)){
364:                 $data['forkey'] = $forkey;
365:             } elseif($forkey && $forkey instanceof ApiAxle\Api\Key){
366:                 $data['forkey'] = $forkey->getKey();
367:             }
368:             
369:             if($forapi && is_string($forapi)){
370:                 $data['forapi'] = $forapi;
371:             } elseif($forapi && $forapi instanceof ApiAxle\Api\Api){
372:                 $data['forapi'] = $forapi->getName();
373:             }
374:             
375:             $apiPath = 'keyring/'.$this->getName().'/stats';
376:             $request = Utilities::callApi($apiPath, 'GET', $data,$this->getConfig());
377:             if($request){
378:                 return $request;
379:             } else {
380:                 throw new \Exception('Unable to get stats for keyring',280);
381:             }
382:         }
383:     }
384:     
385:     public function getCreatedAt()
386:     {
387:         return $this->createdAt;
388:     }
389:     
390:     /**
391:      * Return config object
392:      * 
393:      * @return \ApiAxle\Shared\Config
394:      */
395:     public function getConfig()
396:     {
397:         return $this->config;
398:     }
399:     
400:     public function isValid()
401:     {
402:         if(!is_null($this->getName())){
403:             return true;
404:         }
405:         
406:         return false;
407:     }
408: }
fillup/apiaxle API documentation generated by ApiGen 2.8.0