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
*
*/
private $reject;
- /**
- * @var \Closure
- */
- private $update;
-
/**
* Create a deferred promise for the response of $request
*
}
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);
}
}