From 585700cd6cf2aa4ba8cbf668608d619b223277b5 Mon Sep 17 00:00:00 2001 From: Roland Dalmulder Date: Thu, 4 Mar 2021 19:56:39 +0100 Subject: [PATCH] More codestyle fixes Signed-off-by: Roland Dalmulder --- .../PatchTester/GitHub/GitHub.php | 194 ++++++++++-------- 1 file changed, 108 insertions(+), 86 deletions(-) diff --git a/administrator/components/com_patchtester/PatchTester/GitHub/GitHub.php b/administrator/components/com_patchtester/PatchTester/GitHub/GitHub.php index e692ed8f..d48ef3f0 100644 --- a/administrator/components/com_patchtester/PatchTester/GitHub/GitHub.php +++ b/administrator/components/com_patchtester/PatchTester/GitHub/GitHub.php @@ -51,6 +51,71 @@ public function __construct(Registry $options = null, Http $client = null) $this->client = $client ?: HttpFactory::getHttp($options); } + /** + * Get the HTTP client for this connector. + * + * @return Http + * + * @since 3.0.0 + */ + public function getClient() + { + return $this->client; + } + + /** + * Get the diff for a pull request. + * + * @param string $user The name of the owner of the GitHub repository. + * @param string $repo The name of the GitHub repository. + * @param integer $pullId The pull request number. + * + * @return Response + * + * @since 3.0.0 + */ + public function getDiffForPullRequest($user, $repo, $pullId) + { + // Build the request path. + $path = "/repos/$user/$repo/pulls/" . (int) $pullId; + + // Build the request headers. + $headers = array('Accept' => 'application/vnd.github.diff'); + + $prepared = $this->prepareRequest($path, 0, 0, $headers); + + return $this->processResponse( + $this->client->get($prepared['url'], $prepared['headers']) + ); + } + + /** + * Method to build and return a full request URL for the request. + * + * This method will add appropriate pagination details if necessary and also prepend the API url to have a complete URL for the request. + * + * @param string $path Path to process + * @param integer $page Page to request + * @param integer $limit Number of results to return per page + * @param array $headers The headers to send with the request + * + * @return array Associative array containing the prepared URL and request headers + * + * @since 3.0.0 + */ + protected function prepareRequest($path, $page = 0, $limit = 0, + array $headers = array() + ) { + $url = $this->fetchUrl($path, $page, $limit); + + if ($token = $this->options->get('gh.token', false)) + { + $headers['Authorization'] = "token $token"; + } + + return array('url' => $url, 'headers' => $headers); + } + /** * Build and return a full request URL. * @@ -107,39 +172,32 @@ protected function fetchUrl($path, $page = 0, $limit = 0) } /** - * Get the HTTP client for this connector. - * - * @return Http - * - * @since 3.0.0 - */ - public function getClient() - { - return $this->client; - } - - /** - * Get the diff for a pull request. + * Process the response and return it. * - * @param string $user The name of the owner of the GitHub repository. - * @param string $repo The name of the GitHub repository. - * @param integer $pullId The pull request number. + * @param Response $response The response. + * @param integer $expectedCode The expected response code. * * @return Response * * @since 3.0.0 + * @throws Exception\UnexpectedResponse */ - public function getDiffForPullRequest($user, $repo, $pullId) + protected function processResponse(Response $response, $expectedCode = 200) { - // Build the request path. - $path = "/repos/$user/$repo/pulls/" . (int) $pullId; - - // Build the request headers. - $headers = array('Accept' => 'application/vnd.github.diff'); + // Validate the response code. + if ($response->code != $expectedCode) + { + // Decode the error response and throw an exception. + $body = json_decode($response->body); + $error = isset($body->error) ? $body->error + : (isset($body->message) ? $body->message : 'Unknown Error'); - $prepared = $this->prepareRequest($path, 0, 0, $headers); + throw new Exception\UnexpectedResponse( + $response, $error, $response->code + ); + } - return $this->processResponse($this->client->get($prepared['url'], $prepared['headers'])); + return $response; } /** @@ -168,7 +226,9 @@ public function getFileContents($user, $repo, $path, $ref = null) $prepared['url'] = (string) $url; } - return $this->processResponse($this->client->get($prepared['url'], $prepared['headers'])); + return $this->processResponse( + $this->client->get($prepared['url'], $prepared['headers']) + ); } /** @@ -189,7 +249,9 @@ public function getFilesForPullRequest($user, $repo, $pullId) $prepared = $this->prepareRequest($path); - return $this->processResponse($this->client->get($prepared['url'], $prepared['headers'])); + return $this->processResponse( + $this->client->get($prepared['url'], $prepared['headers']) + ); } /** @@ -206,12 +268,16 @@ public function getFilesForPullRequest($user, $repo, $pullId) */ public function getOpenIssues($user, $repo, $page = 0, $limit = 0) { - $prepared = $this->prepareRequest("/repos/$user/$repo/issues", $page, $limit); + $prepared = $this->prepareRequest( + "/repos/$user/$repo/issues", $page, $limit + ); - return $this->processResponse($this->client->get($prepared['url'], $prepared['headers'])); + return $this->processResponse( + $this->client->get($prepared['url'], $prepared['headers']) + ); } - /** + /** * Get a list of the open pull requests for a repository. * * @param string $user The name of the owner of the GitHub repository. @@ -225,11 +291,15 @@ public function getOpenIssues($user, $repo, $page = 0, $limit = 0) */ public function getOpenPulls($user, $repo, $page = 0, $limit = 0) { - $prepared = $this->prepareRequest("/repos/$user/$repo/pulls", $page, $limit); + $prepared = $this->prepareRequest( + "/repos/$user/$repo/pulls", $page, $limit + ); - return $this->processResponse($this->client->get($prepared['url'], $prepared['headers'])); + return $this->processResponse( + $this->client->get($prepared['url'], $prepared['headers']) + ); } - + /** * Get an option from the connector. * @@ -263,7 +333,9 @@ public function getPullRequest($user, $repo, $pullId) $prepared = $this->prepareRequest($path); - return $this->processResponse($this->client->get($prepared['url'], $prepared['headers'])); + return $this->processResponse( + $this->client->get($prepared['url'], $prepared['headers']) + ); } /** @@ -277,59 +349,9 @@ public function getRateLimit() { $prepared = $this->prepareRequest('/rate_limit'); - return $this->processResponse($this->client->get($prepared['url'], $prepared['headers'])); - } - - /** - * Process the response and return it. - * - * @param Response $response The response. - * @param integer $expectedCode The expected response code. - * - * @return Response - * - * @since 3.0.0 - * @throws Exception\UnexpectedResponse - */ - protected function processResponse(Response $response, $expectedCode = 200) - { - // Validate the response code. - if ($response->code != $expectedCode) - { - // Decode the error response and throw an exception. - $body = json_decode($response->body); - $error = isset($body->error) ? $body->error : (isset($body->message) ? $body->message : 'Unknown Error'); - - throw new Exception\UnexpectedResponse($response, $error, $response->code); - } - - return $response; - } - - /** - * Method to build and return a full request URL for the request. - * - * This method will add appropriate pagination details if necessary and also prepend the API url to have a complete URL for the request. - * - * @param string $path Path to process - * @param integer $page Page to request - * @param integer $limit Number of results to return per page - * @param array $headers The headers to send with the request - * - * @return array Associative array containing the prepared URL and request headers - * - * @since 3.0.0 - */ - protected function prepareRequest($path, $page = 0, $limit = 0, array $headers = array()) - { - $url = $this->fetchUrl($path, $page, $limit); - - if ($token = $this->options->get('gh.token', false)) - { - $headers['Authorization'] = "token $token"; - } - - return array('url' => $url, 'headers' => $headers); + return $this->processResponse( + $this->client->get($prepared['url'], $prepared['headers']) + ); } /**