refactored 80%
[pharext/pharext.org] / app / Github / API / Call.php
index 32bf1cb0c89bd7e46aa9c0ad70727c55fb8afd06..4e0513db60b73a66212f9ac5a7e24c1df871fa5b 100644 (file)
@@ -3,10 +3,14 @@
 namespace app\Github\API;
 
 use app\Github\API;
+use app\Github\Storage\Item;
+use http\Client\Response;
 use http\QueryString;
 use http\Url;
 use merry\Config;
 
+use React\Promise;
+
 abstract class Call
 {
        /**
@@ -35,9 +39,19 @@ abstract class Call
        protected $query;
        
        /**
-        * Queue this call to the API client
+        * @var \React\Promise\Deferred
+        */
+       protected $deferred;
+       
+       /**
+        * @return Request
+        */
+       abstract protected function request();
+       
+       /**
+        * @return array
         */
-       abstract function enqueue(callable $callback);
+       abstract protected function response(Response $response);
        
        /**
         * @param API $api
@@ -47,6 +61,7 @@ abstract class Call
                $this->api = $api;
                $this->config = $this->api->getConfig();
                $this->url = new Url($this->config->api->url, null, 0);
+               $this->deferred = new Promise\Deferred;
                
                if ($args) {
                        $this->args = $args;
@@ -56,13 +71,26 @@ abstract class Call
                }
        }
        
-       function __invoke(callable $callback) {
-               if ($this->readFromCache($cached)) {
-                       call_user_func_array($callback, $cached);
+       /**
+        * @return \React\Promise\Promise
+        */
+       function __invoke() {
+               if ($this->readFromCache($this->result)) {
+                       return new Promise\FulfilledPromise($this->result);
                } else {
-                       $this->enqueue($callback);
+                       $this->api->getClient()->enqueue(
+                               $this->request(),
+                               function($response) {
+                                       try {
+                                               $this->deferred->resolve($this->response($response));
+                                       } catch (\Exception $e) {
+                                               $this->deferred->reject($e);
+                                       }
+                                       return true;
+                               }
+                       );
+                       return $this->deferred->promise();
                }
-               return $this;
        }
        
        /**
@@ -74,13 +102,6 @@ abstract class Call
                return strtolower(end($parts));
        }
        
-       /**
-        * Call Client::send()
-        */
-       function send() {
-               return $this->api->getClient()->send();
-       }
-       
        /**
         * Get associated cache storage
         * @param int $ttl out param of configure ttl
@@ -96,17 +117,33 @@ abstract class Call
        function getCacheKey() {
                $args = $this->args;
                unset($args["fresh"]);
+               if (isset($args["page"]) && !strcmp($args["page"], "1")) {
+                       unset($args["page"]);
+               }
                ksort($args);
                return sprintf("%s:%s:%s", $this->api->getToken(), $this, 
                        new QueryString($args));
        }
 
-       function readFromCache(array &$cached = null, &$ttl = null) {
-               if (empty($this->args["fresh"]) && ($cache = $this->api->getCacheStorage())) {
-                       $key = $this->getCacheKey();
-                       return $cache->get($key, $cached, $ttl);
+       function readFromCache(array &$value = null) {
+               if (!empty($this->args["fresh"])) {
+                       return false;
+               }
+               if (!($cache = $this->api->getCacheStorage())) {
+                       return false;
+               }
+               if (!strlen($key = $this->getCacheKey())) {
+                       return false;
+               }
+               if (!$cache->get($key, $cached)) {
+                       return false;
+               }
+               if (null !== $this->api->getMaxAge() && $cached->getAge() > $this->api->getMaxAge()) {
+                       return false;
                }
-               return false;
+               $this->api->getLogger()->debug("Cache-Hit: $this", $this->args);
+               $value = $cached->getValue();
+               return true;
        }
        
        function saveToCache(array $fresh) {
@@ -118,7 +155,7 @@ abstract class Call
                        }
                        
                        $key = $this->getCacheKey();
-                       $cache->set($key, $fresh, $ttl);
+                       $cache->set($key, new Item($fresh, $ttl));
                }
        }