PHP8
[m6w6/seekat] / lib / API / Call / Deferred.php
index 304ec363bf1d7acdbf59ecf80b2e418c12b217a8..bfe8c27e4da46dacbcb9f558615a21a7a1ee02ad 100644 (file)
@@ -2,14 +2,11 @@
 
 namespace seekat\API\Call;
 
 
 namespace seekat\API\Call;
 
-use http\{
-       Client, Client\Request, Client\Response
-};
+use http\{Client, Client\Request, Client\Response};
 use Psr\Log\LoggerInterface;
 use seekat\API;
 
 use Psr\Log\LoggerInterface;
 use seekat\API;
 
-final class Deferred
-{
+final class Deferred {
        /**
         * The response importer
         *
        /**
         * The response importer
         *
@@ -65,11 +62,6 @@ final class Deferred
         */
        private $reject;
 
         */
        private $reject;
 
-       /**
-        * @var \Closure
-        */
-       private $update;
-
        /**
         * Create a deferred promise for the response of $request
         *
        /**
         * Create a deferred promise for the response of $request
         *
@@ -100,51 +92,93 @@ final class Deferred
        }
 
        function __invoke() {
        }
 
        function __invoke() {
-               if ($this->cache->load($this->request, $cached)) {
-                       $this->logger->info("deferred -> cached", [
-                               "method" => $this->request->getRequestMethod(),
-                               "url" => $this->request->getRequestUrl(),
-                       ]);
+               if (!$this->cached($cached)) {
+                       $this->refresh($cached);
+               }
 
 
-                       $this->response = $cached;
-                       $this->complete();
+               return $this->promise;
+       }
+
+       /**
+        * Peek into cache
+        *
+        * @param Response $cached
+        * @return bool
+        */
+       private function cached(Response &$cached = null) : bool {
+               $fresh = $this->cache->load($this->request, $cachedResponse);
+
+               if (!$cachedResponse) {
+                       return false;
                } else {
                } else {
-                       $this->client->enqueue($this->request, function(Response $response) use($cached) {
-                               if ($response->getResponseCode() == 304) {
-                                       $this->response = $cached;
-                               } else {
-                                       $this->response = $response;
-                               }
-                               $this->complete();
-                               return true;
-                       });
-                       $this->logger->info("deferred -> enqueued", [
+                       $cached = $cachedResponse;
+
+                       $this->logger->info("deferred -> cached", [
                                "method" => $this->request->getRequestMethod(),
                                "url" => $this->request->getRequestUrl(),
                        ]);
                                "method" => $this->request->getRequestMethod(),
                                "url" => $this->request->getRequestUrl(),
                        ]);
-                       /* start off */
-                       $this->client->once();
+
+
+                       if (!$fresh) {
+                               $this->logger->info("cached -> stale", [
+                                       "method" => $this->request->getRequestMethod(),
+                                       "url"    => $this->request->getRequestUrl(),
+                               ]);
+                               return false;
+                       }
                }
 
                }
 
-               return $this->promise;
+               $this->response = $cached;
+               $this->complete("cached");
+               return true;
+       }
+
+       /**
+        * Refresh
+        *
+        * @param Response|null $cached
+        */
+       private function refresh(Response $cached = null) {
+               $this->client->enqueue($this->request, function(Response $response) use($cached) {
+                       $this->response = $response;
+                       $this->complete();
+                       return true;
+               });
+
+               $this->logger->info(($cached ? "stale" : "deferred") . " -> enqueued", [
+                       "method" => $this->request->getRequestMethod(),
+                       "url" => $this->request->getRequestUrl(),
+               ]);
+
+               /* start off */
+               $this->client->once();
        }
 
        /**
         * Completion callback
         */
        }
 
        /**
         * Completion callback
         */
-       private function complete() {
+       private function complete(string $by = "enqueued") {
                if ($this->response) {
                if ($this->response) {
-                       try {
-                               $api = ($this->result)($this->response);
-
-                               $this->cache->save($this->request, $this->response);
+                       $this->logger->info("$by -> response", [
+                               "url"  => $this->request->getRequestUrl(),
+                               "info" => $this->response->getInfo(),
+                       ]);
 
 
-                               ($this->resolve)($api);
+                       try {
+                               $this->cache->update($this->request, $this->response);
+                               ($this->resolve)(($this->result)($this->response));
                        } catch (\Throwable $e) {
                                ($this->reject)($e);
                        }
                } else {
                        } catch (\Throwable $e) {
                                ($this->reject)($e);
                        }
                } else {
-                       ($this->reject)($this->client->getTransferInfo($this->request)->error);
+                       $info = $this->client->getTransferInfo($this->request);
+
+                       $this->logger->warning("$by -> no response", [
+                               "url"  => $this->request->getRequestUrl(),
+                               "info" => $info
+                       ]);
+
+                       ($this->reject)($info->error);
                }
        }
 
                }
        }