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 ($this->readFromCache($cached)) {
61 header("X-Cache-Hit: $this", false);
62 call_user_func_array($callback, $cached);
64 header("X-Cache-Miss: $this", false);
65 $this->enqueue($callback);
74 function __toString() {
75 $parts = explode("\\", get_class($this));
76 return strtolower(end($parts));
83 return $this->api
->getClient()->send();
87 * Get associated cache storage
88 * @param int $ttl out param of configure ttl
91 function getCache(&$ttl = null) {
92 if (isset($this->config
->storage
->cache
->{$this}->ttl
)) {
93 $ttl = $this->config
->storage
->cache
->{$this}->ttl
;
95 return $this->api
->getCacheStorage();
98 function getCacheKey() {
100 unset($args["fresh"]);
102 return sprintf("github:%s:%s:%s", $this->api
->getToken(), $this,
103 new QueryString($args));
106 function readFromCache(array &$cached = null, &$ttl = null) {
107 if (empty($this->args
["fresh"]) && ($cache = $this->api
->getCacheStorage())) {
108 $key = $this->getCacheKey();
109 return $cache->get($key, $cached, $ttl);
114 function saveToCache(array $fresh) {
115 if (($cache = $this->api
->getCacheStorage())) {
116 if (isset($this->config
->storage
->cache
->{$this}->ttl
)) {
117 $ttl = $this->config
->storage
->cache
->{$this}->ttl
;
122 $key = $this->getCacheKey();
123 $cache->set($key, $fresh, $ttl);
127 function dropFromCache() {
128 if (($cache = $this->api
->getCacheStorage())) {
129 $key = $this->getCacheKey();