From ebd7f811ee4432579babb07ca24585a3520f32eb Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 10 Feb 2021 13:59:37 -0500 Subject: [PATCH] Code style fixes --- src/AnimeClient/API/APIRequestBuilder.php | 68 ------------------- .../API/Anilist/RequestBuilder.php | 7 +- src/AnimeClient/API/Kitsu/RequestBuilder.php | 68 +++++++++++++++++++ src/AnimeClient/AnimeClient.php | 5 ++ 4 files changed, 77 insertions(+), 71 deletions(-) diff --git a/src/AnimeClient/API/APIRequestBuilder.php b/src/AnimeClient/API/APIRequestBuilder.php index 99d6274b..ae714548 100644 --- a/src/AnimeClient/API/APIRequestBuilder.php +++ b/src/AnimeClient/API/APIRequestBuilder.php @@ -293,74 +293,6 @@ abstract class APIRequestBuilder { return $this; } - /** - * Create a GraphQL query and return the Request object - * - * @param string $name - * @param array $variables - * @return Request - */ - public function queryRequest(string $name, array $variables = []): Request - { - $file = realpath("{$this->filePath}/Queries/{$name}.graphql"); - if ( ! file_exists($file)) - { - throw new LogicException('GraphQL query file does not exist.'); - } - - $query = file_get_contents($file); - $body = [ - 'query' => $query - ]; - - if ( ! empty($variables)) - { - $body['variables'] = []; - foreach($variables as $key => $val) - { - $body['variables'][$key] = $val; - } - } - - return $this->setUpRequest('POST', $this->baseUrl, [ - 'body' => $body, - ]); - } - - /** - * Create a GraphQL mutation request, and return the Request object - * - * @param string $name - * @param array $variables - * @return Request - * @throws Throwable - */ - public function mutateRequest (string $name, array $variables = []): Request - { - $file = "{$this->filePath}/Mutations/{$name}.graphql"; - if ( ! file_exists($file)) - { - throw new LogicException('GraphQL mutation file does not exist.'); - } - - $query = file_get_contents($file); - $body = [ - 'query' => $query - ]; - - if (!empty($variables)) { - $body['variables'] = []; - foreach ($variables as $key => $val) - { - $body['variables'][$key] = $val; - } - } - - return $this->setUpRequest('POST', $this->baseUrl, [ - 'body' => $body, - ]); - } - /** * Create the full request url * diff --git a/src/AnimeClient/API/Anilist/RequestBuilder.php b/src/AnimeClient/API/Anilist/RequestBuilder.php index a6958a12..a7f4c1e8 100644 --- a/src/AnimeClient/API/Anilist/RequestBuilder.php +++ b/src/AnimeClient/API/Anilist/RequestBuilder.php @@ -31,6 +31,7 @@ use const Aviat\AnimeClient\USER_AGENT; use Aviat\AnimeClient\API\APIRequestBuilder; use LogicException; +use Throwable; final class RequestBuilder extends APIRequestBuilder { use ContainerAware; @@ -117,7 +118,7 @@ final class RequestBuilder extends APIRequestBuilder { */ public function runQuery(string $name, array $variables = []): array { - $file = realpath(__DIR__ . "/Queries/{$name}.graphql"); + $file = __DIR__ . "/Queries/{$name}.graphql"; if ( ! file_exists($file)) { throw new LogicException('GraphQL query file does not exist.'); @@ -150,8 +151,8 @@ final class RequestBuilder extends APIRequestBuilder { */ public function mutateRequest (string $name, array $variables = []): Request { - $file = realpath(__DIR__ . "/Mutations/{$name}.graphql"); - if (!file_exists($file)) + $file = __DIR__ . "/Mutations/{$name}.graphql"; + if ( ! file_exists($file)) { throw new LogicException('GraphQL mutation file does not exist.'); } diff --git a/src/AnimeClient/API/Kitsu/RequestBuilder.php b/src/AnimeClient/API/Kitsu/RequestBuilder.php index a69b6853..74c02f8f 100644 --- a/src/AnimeClient/API/Kitsu/RequestBuilder.php +++ b/src/AnimeClient/API/Kitsu/RequestBuilder.php @@ -200,6 +200,74 @@ final class RequestBuilder extends APIRequestBuilder { return $response; } + /** + * Create a GraphQL query and return the Request object + * + * @param string $name + * @param array $variables + * @return Request + */ + public function queryRequest(string $name, array $variables = []): Request + { + $file = realpath("{$this->filePath}/Queries/{$name}.graphql"); + if ( ! file_exists($file)) + { + throw new LogicException('GraphQL query file does not exist.'); + } + + $query = file_get_contents($file); + $body = [ + 'query' => $query + ]; + + if ( ! empty($variables)) + { + $body['variables'] = []; + foreach($variables as $key => $val) + { + $body['variables'][$key] = $val; + } + } + + return $this->setUpRequest('POST', $this->baseUrl, [ + 'body' => $body, + ]); + } + + /** + * Create a GraphQL mutation request, and return the Request object + * + * @param string $name + * @param array $variables + * @return Request + * @throws Throwable + */ + public function mutateRequest (string $name, array $variables = []): Request + { + $file = "{$this->filePath}/Mutations/{$name}.graphql"; + if ( ! file_exists($file)) + { + throw new LogicException('GraphQL mutation file does not exist.'); + } + + $query = file_get_contents($file); + $body = [ + 'query' => $query + ]; + + if (!empty($variables)) { + $body['variables'] = []; + foreach ($variables as $key => $val) + { + $body['variables'][$key] = $val; + } + } + + return $this->setUpRequest('POST', $this->baseUrl, [ + 'body' => $body, + ]); + } + /** * Make a request * diff --git a/src/AnimeClient/AnimeClient.php b/src/AnimeClient/AnimeClient.php index 1b3d42b7..df1ce722 100644 --- a/src/AnimeClient/AnimeClient.php +++ b/src/AnimeClient/AnimeClient.php @@ -48,6 +48,11 @@ function loadConfig(string $path): array $output = []; $files = glob("{$path}/*.toml"); + if ( ! is_array($files)) + { + return []; + } + foreach ($files as $file) { $key = str_replace('.toml', '', basename($file));