Skip to content

Commit

Permalink
More codestyle fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Roland Dalmulder <contact@rolandd.com>
  • Loading branch information
roland-d committed Mar 4, 2021
1 parent 3f60741 commit 585700c
Showing 1 changed file with 108 additions and 86 deletions.
194 changes: 108 additions & 86 deletions administrator/components/com_patchtester/PatchTester/GitHub/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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'])
);
}

/**
Expand All @@ -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'])
);
}

/**
Expand All @@ -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.
Expand All @@ -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.
*
Expand Down Expand Up @@ -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'])
);
}

/**
Expand All @@ -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'])
);
}

/**
Expand Down

0 comments on commit 585700c

Please sign in to comment.