d4afd19eeb3c375408162728293622a2f2e2b12c
3 namespace app\Github\API
;
6 use app\Github\Storage\Item
;
19 * @var \app\Gituhub\API
39 * Queue this call to the API client
41 abstract function enqueue(callable
$callback);
47 function __construct(API
$api, array $args = null) {
49 $this->config
= $this->api
->getConfig();
50 $this->url
= new Url($this->config
->api
->url
, null, 0);
55 if (isset($this->config
->api
->call
->{$this}->args
)) {
56 $this->args +
= $this->config
->api
->call
->{$this}->args
->toArray();
60 function __invoke(callable
$callback) {
61 if ($this->readFromCache($cached)) {
62 call_user_func_array($callback, $cached);
64 $this->enqueue($callback);
73 function __toString() {
74 $parts = explode("\\", get_class($this));
75 return strtolower(end($parts));
82 return $this->api
->getClient()->send();
86 * Get associated cache storage
87 * @param int $ttl out param of configure ttl
90 function getCache(&$ttl = null) {
91 if (isset($this->config
->storage
->cache
->{$this}->ttl
)) {
92 $ttl = $this->config
->storage
->cache
->{$this}->ttl
;
94 return $this->api
->getCacheStorage();
97 function getCacheKey() {
99 unset($args["fresh"]);
100 if (isset($args["page"]) && !strcmp($args["page"], "1")) {
101 unset($args["page"]);
104 return sprintf("%s:%s:%s", $this->api
->getToken(), $this,
105 new QueryString($args));
108 function readFromCache(array &$value = null) {
109 if (!empty($this->args
["fresh"])) {
112 if (!($cache = $this->api
->getCacheStorage())) {
115 if (!strlen($key = $this->getCacheKey())) {
118 if (!$cache->get($key, $cached)) {
121 if (null !== $this->api
->getMaxAge() && $cached->getAge() > $this->api
->getMaxAge()) {
124 $this->api
->getLogger()->debug("Cache-Hit: $this", $this->args
);
125 $value = $cached->getValue();
129 function saveToCache(array $fresh) {
130 if (($cache = $this->api
->getCacheStorage())) {
131 if (isset($this->config
->storage
->cache
->{$this}->ttl
)) {
132 $ttl = $this->config
->storage
->cache
->{$this}->ttl
;
137 $key = $this->getCacheKey();
138 $cache->set($key, new Item($fresh, $ttl));
142 function dropFromCache() {
143 if (($cache = $this->api
->getCacheStorage())) {
144 $key = $this->getCacheKey();