update to PHP-8.1
[m6w6/seekat] / lib / API / Call / Result.php
index acbc0a81651319357e0a587bb08363063f6eb3a9..92b7dae4572a6bdbec243c57fa822b7bdbd946e9 100644 (file)
@@ -7,60 +7,86 @@ use http\Header;
 use seekat\API;
 use seekat\Exception\RequestException;
 
-class Result
-{
+final class Result {
        private $api;
 
        function __construct(API $api) {
                $this->api = $api;
        }
 
+       /**
+        * @param Response $response
+        * @return API
+        * @throws RequestException
+        */
        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,
-                       "info" => $response->getInfo(),
+               $links = $this->checkResponseMeta($response);
+               $type = $this->checkResponseType($response);
+               $data = $this->checkResponseBody($response, $type);
+
+               $this->api->getLogger()->info("response -> info", [
+                       "type" => $type->getType(),
+                       "links" => $links->getRelations(),
+               ]);
+               $this->api->getLogger()->debug("response -> data", [
+                       "data" => $data,
                ]);
 
+               return $this->api = $this->api->with(compact("type", "data", "links"));
+       }
+
+       /**
+        * @throws RequestException
+        */
+       private function checkResponseMeta(Response $response) : API\Links {
                if ($response->getResponseCode() >= 400) {
                        $e = new RequestException($response);
 
-                       $log->critical(__FUNCTION__.": ".$e->getMessage(), [
-                               "url" => (string) $url,
+                       $this->api->getLogger()->critical("response -> error: ".$e->getMessage(), [
+                               "url" => (string) $this->api->getUrl(),
                        ]);
 
                        throw $e;
                }
 
+               if (!($link = $response->getHeader("Link", Header::class))) {
+                       $link = null;
+               }
+
+               return new API\Links($link);
+       }
+
+       /**
+        * @throws RequestException
+        */
+       private function checkResponseType(Response $response) : API\ContentType {
                if (!($type = $response->getHeader("Content-Type", Header::class))) {
                        $e = new RequestException($response);
-                       $log->error(
-                               __FUNCTION__.": Empty Content-Type -> ".$e->getMessage(), [
-                               "url" => (string) $url,
+
+                       $this->api->getLogger()->error("response -> error: Empty Content-Type -> ".$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;
-                       }
+               return new API\ContentType($this->api->getVersion(), $type->value);
+       }
 
-                       $this->api = $this->api->with(compact("type", "data", "links"));
+       /**
+        * @throws \Exception
+        */
+       private function checkResponseBody(Response $response, API\ContentType $type) : mixed {
+               try {
+                       $data = $type->decode($response->getBody());
                } catch (\Exception $e) {
-                       $log->error(__FUNCTION__.": ".$e->getMessage(), [
-                               "url" => (string) $url
+                       $this->api->getLogger()->error("response -> error: ".$e->getMessage(), [
+                               "url" => (string) $this->api->getUrl(),
                        ]);
 
                        throw $e;
                }
 
-               return $this->api;
+               return $data;
        }
 }