X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fseekat;a=blobdiff_plain;f=lib%2FAPI%2FCall%2FDeferred.php;h=bfe8c27e4da46dacbcb9f558615a21a7a1ee02ad;hp=304ec363bf1d7acdbf59ecf80b2e418c12b217a8;hb=2121556150be871684b5046af7cf250b8219128d;hpb=cac6bea94e6cde142c951566fa6387ffa54eb3cb diff --git a/lib/API/Call/Deferred.php b/lib/API/Call/Deferred.php index 304ec36..bfe8c27 100644 --- a/lib/API/Call/Deferred.php +++ b/lib/API/Call/Deferred.php @@ -2,14 +2,11 @@ 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; -final class Deferred -{ +final class Deferred { /** * The response importer * @@ -65,11 +62,6 @@ final class Deferred */ private $reject; - /** - * @var \Closure - */ - private $update; - /** * Create a deferred promise for the response of $request * @@ -100,51 +92,93 @@ final class Deferred } 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 { - $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(), ]); - /* 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 */ - private function complete() { + private function complete(string $by = "enqueued") { 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 { - ($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); } }