Skip to content

Commit 585700c

Browse files
committed
More codestyle fixes
Signed-off-by: Roland Dalmulder <[email protected]>
1 parent 3f60741 commit 585700c

File tree

1 file changed

+108
-86
lines changed
  • administrator/components/com_patchtester/PatchTester/GitHub

1 file changed

+108
-86
lines changed

administrator/components/com_patchtester/PatchTester/GitHub/GitHub.php

Lines changed: 108 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,71 @@ public function __construct(Registry $options = null, Http $client = null)
5151
$this->client = $client ?: HttpFactory::getHttp($options);
5252
}
5353

54+
/**
55+
* Get the HTTP client for this connector.
56+
*
57+
* @return Http
58+
*
59+
* @since 3.0.0
60+
*/
61+
public function getClient()
62+
{
63+
return $this->client;
64+
}
65+
66+
/**
67+
* Get the diff for a pull request.
68+
*
69+
* @param string $user The name of the owner of the GitHub repository.
70+
* @param string $repo The name of the GitHub repository.
71+
* @param integer $pullId The pull request number.
72+
*
73+
* @return Response
74+
*
75+
* @since 3.0.0
76+
*/
77+
public function getDiffForPullRequest($user, $repo, $pullId)
78+
{
79+
// Build the request path.
80+
$path = "/repos/$user/$repo/pulls/" . (int) $pullId;
81+
82+
// Build the request headers.
83+
$headers = array('Accept' => 'application/vnd.github.diff');
84+
85+
$prepared = $this->prepareRequest($path, 0, 0, $headers);
86+
87+
return $this->processResponse(
88+
$this->client->get($prepared['url'], $prepared['headers'])
89+
);
90+
}
91+
92+
/**
93+
* Method to build and return a full request URL for the request.
94+
*
95+
* This method will add appropriate pagination details if necessary and also prepend the API url to have a complete URL for the request.
96+
*
97+
* @param string $path Path to process
98+
* @param integer $page Page to request
99+
* @param integer $limit Number of results to return per page
100+
* @param array $headers The headers to send with the request
101+
*
102+
* @return array Associative array containing the prepared URL and request headers
103+
*
104+
* @since 3.0.0
105+
*/
106+
protected function prepareRequest($path, $page = 0, $limit = 0,
107+
array $headers = array()
108+
) {
109+
$url = $this->fetchUrl($path, $page, $limit);
110+
111+
if ($token = $this->options->get('gh.token', false))
112+
{
113+
$headers['Authorization'] = "token $token";
114+
}
115+
116+
return array('url' => $url, 'headers' => $headers);
117+
}
118+
54119
/**
55120
* Build and return a full request URL.
56121
*
@@ -107,39 +172,32 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
107172
}
108173

109174
/**
110-
* Get the HTTP client for this connector.
111-
*
112-
* @return Http
113-
*
114-
* @since 3.0.0
115-
*/
116-
public function getClient()
117-
{
118-
return $this->client;
119-
}
120-
121-
/**
122-
* Get the diff for a pull request.
175+
* Process the response and return it.
123176
*
124-
* @param string $user The name of the owner of the GitHub repository.
125-
* @param string $repo The name of the GitHub repository.
126-
* @param integer $pullId The pull request number.
177+
* @param Response $response The response.
178+
* @param integer $expectedCode The expected response code.
127179
*
128180
* @return Response
129181
*
130182
* @since 3.0.0
183+
* @throws Exception\UnexpectedResponse
131184
*/
132-
public function getDiffForPullRequest($user, $repo, $pullId)
185+
protected function processResponse(Response $response, $expectedCode = 200)
133186
{
134-
// Build the request path.
135-
$path = "/repos/$user/$repo/pulls/" . (int) $pullId;
136-
137-
// Build the request headers.
138-
$headers = array('Accept' => 'application/vnd.github.diff');
187+
// Validate the response code.
188+
if ($response->code != $expectedCode)
189+
{
190+
// Decode the error response and throw an exception.
191+
$body = json_decode($response->body);
192+
$error = isset($body->error) ? $body->error
193+
: (isset($body->message) ? $body->message : 'Unknown Error');
139194

140-
$prepared = $this->prepareRequest($path, 0, 0, $headers);
195+
throw new Exception\UnexpectedResponse(
196+
$response, $error, $response->code
197+
);
198+
}
141199

142-
return $this->processResponse($this->client->get($prepared['url'], $prepared['headers']));
200+
return $response;
143201
}
144202

145203
/**
@@ -168,7 +226,9 @@ public function getFileContents($user, $repo, $path, $ref = null)
168226
$prepared['url'] = (string) $url;
169227
}
170228

171-
return $this->processResponse($this->client->get($prepared['url'], $prepared['headers']));
229+
return $this->processResponse(
230+
$this->client->get($prepared['url'], $prepared['headers'])
231+
);
172232
}
173233

174234
/**
@@ -189,7 +249,9 @@ public function getFilesForPullRequest($user, $repo, $pullId)
189249

190250
$prepared = $this->prepareRequest($path);
191251

192-
return $this->processResponse($this->client->get($prepared['url'], $prepared['headers']));
252+
return $this->processResponse(
253+
$this->client->get($prepared['url'], $prepared['headers'])
254+
);
193255
}
194256

195257
/**
@@ -206,12 +268,16 @@ public function getFilesForPullRequest($user, $repo, $pullId)
206268
*/
207269
public function getOpenIssues($user, $repo, $page = 0, $limit = 0)
208270
{
209-
$prepared = $this->prepareRequest("/repos/$user/$repo/issues", $page, $limit);
271+
$prepared = $this->prepareRequest(
272+
"/repos/$user/$repo/issues", $page, $limit
273+
);
210274

211-
return $this->processResponse($this->client->get($prepared['url'], $prepared['headers']));
275+
return $this->processResponse(
276+
$this->client->get($prepared['url'], $prepared['headers'])
277+
);
212278
}
213279

214-
/**
280+
/**
215281
* Get a list of the open pull requests for a repository.
216282
*
217283
* @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)
225291
*/
226292
public function getOpenPulls($user, $repo, $page = 0, $limit = 0)
227293
{
228-
$prepared = $this->prepareRequest("/repos/$user/$repo/pulls", $page, $limit);
294+
$prepared = $this->prepareRequest(
295+
"/repos/$user/$repo/pulls", $page, $limit
296+
);
229297

230-
return $this->processResponse($this->client->get($prepared['url'], $prepared['headers']));
298+
return $this->processResponse(
299+
$this->client->get($prepared['url'], $prepared['headers'])
300+
);
231301
}
232-
302+
233303
/**
234304
* Get an option from the connector.
235305
*
@@ -263,7 +333,9 @@ public function getPullRequest($user, $repo, $pullId)
263333

264334
$prepared = $this->prepareRequest($path);
265335

266-
return $this->processResponse($this->client->get($prepared['url'], $prepared['headers']));
336+
return $this->processResponse(
337+
$this->client->get($prepared['url'], $prepared['headers'])
338+
);
267339
}
268340

269341
/**
@@ -277,59 +349,9 @@ public function getRateLimit()
277349
{
278350
$prepared = $this->prepareRequest('/rate_limit');
279351

280-
return $this->processResponse($this->client->get($prepared['url'], $prepared['headers']));
281-
}
282-
283-
/**
284-
* Process the response and return it.
285-
*
286-
* @param Response $response The response.
287-
* @param integer $expectedCode The expected response code.
288-
*
289-
* @return Response
290-
*
291-
* @since 3.0.0
292-
* @throws Exception\UnexpectedResponse
293-
*/
294-
protected function processResponse(Response $response, $expectedCode = 200)
295-
{
296-
// Validate the response code.
297-
if ($response->code != $expectedCode)
298-
{
299-
// Decode the error response and throw an exception.
300-
$body = json_decode($response->body);
301-
$error = isset($body->error) ? $body->error : (isset($body->message) ? $body->message : 'Unknown Error');
302-
303-
throw new Exception\UnexpectedResponse($response, $error, $response->code);
304-
}
305-
306-
return $response;
307-
}
308-
309-
/**
310-
* Method to build and return a full request URL for the request.
311-
*
312-
* This method will add appropriate pagination details if necessary and also prepend the API url to have a complete URL for the request.
313-
*
314-
* @param string $path Path to process
315-
* @param integer $page Page to request
316-
* @param integer $limit Number of results to return per page
317-
* @param array $headers The headers to send with the request
318-
*
319-
* @return array Associative array containing the prepared URL and request headers
320-
*
321-
* @since 3.0.0
322-
*/
323-
protected function prepareRequest($path, $page = 0, $limit = 0, array $headers = array())
324-
{
325-
$url = $this->fetchUrl($path, $page, $limit);
326-
327-
if ($token = $this->options->get('gh.token', false))
328-
{
329-
$headers['Authorization'] = "token $token";
330-
}
331-
332-
return array('url' => $url, 'headers' => $headers);
352+
return $this->processResponse(
353+
$this->client->get($prepared['url'], $prepared['headers'])
354+
);
333355
}
334356

335357
/**

0 commit comments

Comments
 (0)