From: Michael Wallner Date: Mon, 16 Jan 2017 08:35:00 +0000 (+0100) Subject: refactor X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fseekat;a=commitdiff_plain;h=8f7bfbf9cb5340c1fc8a6e3d34724a4fe09bf66c refactor --- diff --git a/lib/API/Call/Result.php b/lib/API/Call/Result.php index acbc0a8..8afc57e 100644 --- a/lib/API/Call/Result.php +++ b/lib/API/Call/Result.php @@ -7,7 +7,7 @@ use http\Header; use seekat\API; use seekat\Exception\RequestException; -class Result +final class Result { private $api; @@ -16,51 +16,66 @@ class Result } function __invoke(Response $response) : API { - $url = $this->api->getUrl(); - $log = $this->api->getLogger(); - $log->info(($response->getHeader("X-Cache-Time") ? "cached" : "enqueued")." -> response", [ - "url" => (string) $url, + $hit = $response->getHeader("X-Cache-Time") ? "cached" : "enqueued"; + $this->api->getLogger()->info("$hit -> response", [ + "url" => (string) $this->api->getUrl(), "info" => $response->getInfo(), ]); - if ($response->getResponseCode() >= 400) { - $e = new RequestException($response); + $links = $this->checkResponseMeta($response); + $type = $this->checkResponseType($response); - $log->critical(__FUNCTION__.": ".$e->getMessage(), [ - "url" => (string) $url, + try { + $data = $type->parseBody($response->getBody()); + } catch (\Exception $e) { + $this->api->getLogger()->error("response -> error: ".$e->getMessage(), [ + "url" => (string) $this->api->getUrl(), ]); throw $e; } - if (!($type = $response->getHeader("Content-Type", Header::class))) { + $this->api = $this->api->with(compact("type", "data", "links")); + + return $this->api; + } + + /** + * @param Response $response + * @return null|API\Links + * @throws RequestException + */ + private function checkResponseMeta(Response $response) { + if ($response->getResponseCode() >= 400) { $e = new RequestException($response); - $log->error( - __FUNCTION__.": Empty Content-Type -> ".$e->getMessage(), [ - "url" => (string) $url, + + $this->api->getLogger()->critical("response -> error: ".$e->getMessage(), [ + "url" => (string) $this->api->getUrl(), ]); + throw $e; } - try { - $type = new API\ContentType($type); - $data = $type->parseBody($response->getBody()); + if (($link = $response->getHeader("Link", Header::class))) { + $links = new API\Links($link); + } else { + $links = null; + } - if (($link = $response->getHeader("Link", Header::class))) { - $links = new API\Links($link); - } else { - $links = null; - } + return $links; + } - $this->api = $this->api->with(compact("type", "data", "links")); - } catch (\Exception $e) { - $log->error(__FUNCTION__.": ".$e->getMessage(), [ - "url" => (string) $url + private function checkResponseType(Response $response) { + if (!($type = $response->getHeader("Content-Type", Header::class))) { + $e = new RequestException($response); + + $this->api->getLogger()->error("response -> error: Empty Content-Type -> ".$e->getMessage(), [ + "url" => (string) $this->api->getUrl(), ]); throw $e; } - return $this->api; + return new API\ContentType($type); } }