3 namespace app\Github\API
;
6 use app\Github\Storage\Item
;
19 * @var \app\Gituhub\API
44 * Queue this call to the API client
46 abstract function enqueue(callable
$callback);
52 function __construct(API
$api, array $args = null) {
54 $this->config
= $this->api
->getConfig();
55 $this->url
= new Url($this->config
->api
->url
, null, 0);
60 if (isset($this->config
->api
->call
->{$this}->args
)) {
61 $this->args +
= $this->config
->api
->call
->{$this}->args
->toArray();
65 function __invoke(callable
$callback) {
66 if ($this->readFromCache($this->result
)) {
67 call_user_func_array($callback, $this->result
);
69 $this->enqueue($callback);
78 function __toString() {
79 $parts = explode("\\", get_class($this));
80 return strtolower(end($parts));
87 $this->api
->getClient()->send();
92 * Get associated cache storage
93 * @param int $ttl out param of configure ttl
96 function getCache(&$ttl = null) {
97 if (isset($this->config
->storage
->cache
->{$this}->ttl
)) {
98 $ttl = $this->config
->storage
->cache
->{$this}->ttl
;
100 return $this->api
->getCacheStorage();
103 function getCacheKey() {
105 unset($args["fresh"]);
106 if (isset($args["page"]) && !strcmp($args["page"], "1")) {
107 unset($args["page"]);
110 return sprintf("%s:%s:%s", $this->api
->getToken(), $this,
111 new QueryString($args));
114 function readFromCache(array &$value = null) {
115 if (!empty($this->args
["fresh"])) {
118 if (!($cache = $this->api
->getCacheStorage())) {
121 if (!strlen($key = $this->getCacheKey())) {
124 if (!$cache->get($key, $cached)) {
127 if (null !== $this->api
->getMaxAge() && $cached->getAge() > $this->api
->getMaxAge()) {
130 $this->api
->getLogger()->debug("Cache-Hit: $this", $this->args
);
131 $value = $cached->getValue();
135 function saveToCache(array $fresh) {
136 if (($cache = $this->api
->getCacheStorage())) {
137 if (isset($this->config
->storage
->cache
->{$this}->ttl
)) {
138 $ttl = $this->config
->storage
->cache
->{$this}->ttl
;
143 $key = $this->getCacheKey();
144 $cache->set($key, new Item($fresh, $ttl));
148 function dropFromCache() {
149 if (($cache = $this->api
->getCacheStorage())) {
150 $key = $this->getCacheKey();