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 Keyring
22: {
23: 24: 25: 26: 27:
28: protected $config;
29:
30: 31: 32: 33: 34:
35: protected $name;
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: 57: 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: 69: 70: 71:
72: public function getName()
73: {
74: return $this->name;
75: }
76:
77: 78: 79: 80: 81:
82: public function setName($name)
83: {
84: $this->name = $name;
85: }
86:
87: 88: 89: 90: 91: 92:
93: public function setData($data)
94: {
95: if(is_array($data)){
96: $data = json_decode(json_encode($data));
97: }
98:
99: 100: 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: 110: 111: 112: 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: 130: 131: 132: 133: 134: 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: 157: 158: 159: 160: 161: 162: 163: 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: 191: 192: 193: 194: 195: 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: 217: 218: 219: 220: 221: 222: 223: 224: 225: 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: 256: 257: 258: 259: 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: 272: 273: 274: 275: 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: 302: 303: 304: 305: 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: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 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: 392: 393: 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: }