3615851693f1870d2094ea70d8a3a018298326f6
3 namespace app\Github\API
;
18 * @var \app\Gituhub\API
38 * Queue this call to the API client
40 abstract function enqueue(callable
$callback);
46 function __construct(API
$api, array $args = null) {
48 $this->config
= $this->api
->getConfig();
49 $this->url
= new Url($this->config
->api
->url
, null, 0);
54 if (isset($this->config
->api
->call
->{$this}->args
)) {
55 $this->args +
= $this->config
->api
->call
->{$this}->args
->toArray();
59 function __invoke(callable
$callback) {
60 if (empty($this->args
["fresh"]) && ($cache = $this->api
->getCacheStorage())) {
61 $key = $this->getCacheKey();
63 if ($cache->get($key, $cached)) {
64 call_user_func_array($callback, $cached);
65 return $this->api
->getClient();
69 $this->enqueue($callback);
70 return $this->api
->getClient();
77 function __toString() {
78 $parts = explode("\\", get_class($this));
79 return strtolower(end($parts));
83 * Get associated cache storage
84 * @param int $ttl out param of configure ttl
87 function getCache(&$ttl = null) {
88 if (isset($this->config
->storage
->cache
->{$this}->ttl
)) {
89 $ttl = $this->config
->storage
->cache
->{$this}->ttl
;
91 return $this->api
->getCacheStorage();
94 function getCacheKey() {
95 return sprintf("github:%s:%s:%s", $this->api
->getToken(), $this,
96 new QueryString($this->args
));
99 function readFromCache(array &$cached = null, &$ttl = null) {
100 if (empty($this->args
["fresh"]) && ($cache = $this->api
->getCacheStorage())) {
101 $key = $this->getCacheKey();
102 return $cache->get($key, $cached, $ttl);
107 function saveToCache(array $fresh) {
108 if (($cache = $this->api
->getCacheStorage())) {
109 if (isset($this->config
->storage
->cache
->{$this}->ttl
)) {
110 $ttl = $this->config
->storage
->cache
->{$this}->ttl
;
115 $key = $this->getCacheKey();
116 $cache->set($key, $fresh, $ttl);