update to PHP-8.1
[m6w6/seekat] / lib / API / Call / Result.php
index 8afc57e452e053a97c70adb9ce5d7691d7e38b18..92b7dae4572a6bdbec243c57fa822b7bdbd946e9 100644 (file)
@@ -7,45 +7,38 @@ use http\Header;
 use seekat\API;
 use seekat\Exception\RequestException;
 
-final 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 {
-               $hit = $response->getHeader("X-Cache-Time") ? "cached" : "enqueued";
-               $this->api->getLogger()->info("$hit -> response", [
-                       "url"  => (string) $this->api->getUrl(),
-                       "info" => $response->getInfo(),
-               ]);
-
                $links = $this->checkResponseMeta($response);
                $type = $this->checkResponseType($response);
+               $data = $this->checkResponseBody($response, $type);
 
-               try {
-                       $data = $type->parseBody($response->getBody());
-               } catch (\Exception $e) {
-                       $this->api->getLogger()->error("response -> error: ".$e->getMessage(), [
-                               "url" => (string) $this->api->getUrl(),
-                       ]);
-
-                       throw $e;
-               }
-
-               $this->api = $this->api->with(compact("type", "data", "links"));
+               $this->api->getLogger()->info("response -> info", [
+                       "type" => $type->getType(),
+                       "links" => $links->getRelations(),
+               ]);
+               $this->api->getLogger()->debug("response -> data", [
+                       "data" => $data,
+               ]);
 
-               return $this->api;
+               return $this->api = $this->api->with(compact("type", "data", "links"));
        }
 
        /**
-        * @param Response $response
-        * @return null|API\Links
         * @throws RequestException
         */
-       private function checkResponseMeta(Response $response) {
+       private function checkResponseMeta(Response $response) : API\Links {
                if ($response->getResponseCode() >= 400) {
                        $e = new RequestException($response);
 
@@ -56,16 +49,17 @@ final class Result
                        throw $e;
                }
 
-               if (($link = $response->getHeader("Link", Header::class))) {
-                       $links = new API\Links($link);
-               } else {
-                       $links = null;
+               if (!($link = $response->getHeader("Link", Header::class))) {
+                       $link = null;
                }
 
-               return $links;
+               return new API\Links($link);
        }
 
-       private function checkResponseType(Response $response) {
+       /**
+        * @throws RequestException
+        */
+       private function checkResponseType(Response $response) : API\ContentType {
                if (!($type = $response->getHeader("Content-Type", Header::class))) {
                        $e = new RequestException($response);
 
@@ -76,6 +70,23 @@ final class Result
                        throw $e;
                }
 
-               return new API\ContentType($type);
+               return new API\ContentType($this->api->getVersion(), $type->value);
+       }
+
+       /**
+        * @throws \Exception
+        */
+       private function checkResponseBody(Response $response, API\ContentType $type) : mixed {
+               try {
+                       $data = $type->decode($response->getBody());
+               } catch (\Exception $e) {
+                       $this->api->getLogger()->error("response -> error: ".$e->getMessage(), [
+                               "url" => (string) $this->api->getUrl(),
+                       ]);
+
+                       throw $e;
+               }
+
+               return $data;
        }
 }