Skip to content

Commit cfb8dcd

Browse files
committed
Refactoring and cleanup
1 parent 578f7d8 commit cfb8dcd

File tree

11 files changed

+108
-177
lines changed

11 files changed

+108
-177
lines changed

lib/Github/Api/Api.php

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
use Github\Client;
66

77
/**
8-
* Abstract class for Github_Api classes
8+
* Abstract class for Api classes
99
*
10-
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
11-
* @license MIT License
10+
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
11+
* @author Joseph Bielawski <stloyd@gmail.com>
1212
*/
1313
abstract class Api implements ApiInterface
1414
{
1515
/**
1616
* The client
17+
*
1718
* @var Client
1819
*/
1920
protected $client;
@@ -27,65 +28,39 @@ public function __construct(Client $client)
2728
}
2829

2930
/**
30-
* Call any path, GET method
31-
* Ex: $api->get('repos/show/my-username/my-repo')
32-
*
33-
* @param string $path the GitHub path
34-
* @param array $parameters GET parameters
35-
* @param array $requestOptions reconfigure the request
36-
* @return array data returned
31+
* {@inheritDoc}
3732
*/
3833
protected function get($path, array $parameters = array(), $requestOptions = array())
3934
{
4035
return $this->client->get($path, $parameters, $requestOptions);
4136
}
4237

4338
/**
44-
* Call any path, POST method
45-
* Ex: $api->post('repos/show/my-username', array('email' => 'my-new-email@provider.org'))
46-
*
47-
* @param string $path the GitHub path
48-
* @param array $parameters POST parameters
49-
* @param array $requestOptions reconfigure the request
50-
* @return array data returned
39+
* {@inheritDoc}
5140
*/
5241
protected function post($path, array $parameters = array(), $requestOptions = array())
5342
{
5443
return $this->client->post($path, $parameters, $requestOptions);
5544
}
5645

5746
/**
58-
* Call any path, PATCH method
59-
* Ex: $api->patch('repos/show/my-username/my-repo')
60-
*
61-
* @param string $path the GitHub path
62-
* @param array $parameters PATCH parameters
63-
* @param array $requestOptions reconfigure the request
64-
* @return array data returned
47+
* {@inheritDoc}
6548
*/
6649
protected function patch($path, array $parameters = array(), $requestOptions = array())
6750
{
6851
return $this->client->patch($path, $parameters, $requestOptions);
6952
}
7053

7154
/**
72-
* Call any path, PUT method
73-
*
74-
* @param string $path the GitHub path
75-
* @param array $requestOptions reconfigure the request
76-
* @return array data returned
55+
* {@inheritDoc}
7756
*/
7857
protected function put($path, array $parameters = array(), $requestOptions = array())
7958
{
8059
return $this->client->put($path, $parameters, $requestOptions);
8160
}
8261

8362
/**
84-
* Call any path, DELETE method
85-
*
86-
* @param string $path the GitHub path
87-
* @param array $requestOptions reconfigure the request
88-
* @return array data returned
63+
* {@inheritDoc}
8964
*/
9065
protected function delete($path, array $parameters = array(), $requestOptions = array())
9166
{

lib/Github/Api/ApiInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace Github\Api;
44

5+
/**
6+
* Api interface
7+
*
8+
* @author Joseph Bielawski <stloyd@gmail.com>
9+
*/
510
interface ApiInterface
611
{
712
}

lib/Github/Api/Commit.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
* Getting information on specific commits,
77
* the diffs they introduce, the files they've changed.
88
*
9-
* @link http://develop.github.com/p/commits.html
10-
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
11-
* @license MIT License
9+
* @link http://developer.github.com/v3/repos/commits/
10+
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
11+
* @author Joseph Bielawski <stloyd@gmail.com>
1212
*/
1313
class Commit extends Api
1414
{
1515
/**
1616
* List commits by username, repo and branch
17-
* http://developer.github.com/v3/repos/commits/
17+
* @link http://developer.github.com/v3/repos/commits/
1818
*
1919
* @param string $username the username
2020
* @param string $repo the repo
@@ -30,7 +30,7 @@ public function getBranchCommits($username, $repo, $branch)
3030

3131
/**
3232
* List commits by username, repo, branch and path
33-
* http://developer.github.com/v3/repos/commits/
33+
* @link http://developer.github.com/v3/repos/commits/
3434
*
3535
* @param string $username the username
3636
* @param string $repo the repo
@@ -47,7 +47,7 @@ public function getFileCommits($username, $repo, $branch, $path)
4747

4848
/**
4949
* Show a specific commit
50-
* http://developer.github.com/v3/repos/commits/
50+
* @link http://developer.github.com/v3/repos/commits/#get-a-single-commit
5151
*
5252
* @param string $username the username
5353
* @param string $repo the repo
@@ -61,6 +61,7 @@ public function getCommit($username, $repo, $sha)
6161

6262
/**
6363
* Fetch branch sha from branch name
64+
* @link http://developer.github.com/v3/git/refs/#get-a-reference
6465
*
6566
* @param string $username
6667
* @param string $repoName
@@ -69,8 +70,12 @@ public function getCommit($username, $repo, $sha)
6970
*/
7071
private function getBranchSha($username, $repoName, $branchName)
7172
{
72-
$branchInfo = $this->get('repos/'.urlencode($username).'/'.urlencode($repoName).'/git/trees/'.urlencode($branchName));
73+
$info = $this->get('repos/'.urlencode($username).'/'.urlencode($repoName).'/git/refs/'.urlencode($branchName));
7374

74-
return isset($branchInfo['sha']) ? $branchInfo['sha'] : null;
75+
if (!isset($info['ref'])) {
76+
return null;
77+
}
78+
79+
return $info['object']['sha'];
7580
}
7681
}

lib/Github/Api/Organization.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Github\Api;
44

5+
use Github\Exception\InvalidArgumentException;
6+
57
/**
68
* Searching organizations, getting organization information
79
* and managing authenticated organization account information.
@@ -30,35 +32,32 @@ public function show($name)
3032

3133
/**
3234
* List all repositories across all the organizations that you can access
33-
* http://develop.github.com/p/orgs.html
35+
* @link http://developer.github.com/v3/repos/#list-organization-repositories
3436
*
3537
* @param string $name the user name
38+
* @param string $type the type of repositories
3639
* @return array the repositories
3740
*/
38-
public function getAllRepos($name)
41+
public function getAllRepos($name, $type = 'all')
3942
{
40-
$response = $this->get('organizations/repositories');
41-
42-
return $response['repositories'];
43+
return $this->get('orgs/'.urlencode($name).'/repos?type='.urlencode($type));
4344
}
4445

4546
/**
4647
* List all public repositories of any other organization
47-
* http://develop.github.com/p/orgs.html
48+
* @link http://developer.github.com/v3/repos/#list-organization-repositories
4849
*
4950
* @param string $name the organization name
5051
* @return array the repositories
5152
*/
5253
public function getPublicRepos($name)
5354
{
54-
$response = $this->get('organizations/'.urlencode($name).'/public_repositories');
55-
56-
return $response['repositories'];
55+
return $this->getAllRepos($name, 'public');
5756
}
5857

5958
/**
6059
* List all public members of that organization
61-
* @link http://developer.github.com/v3/orgs/members/
60+
* @link http://developer.github.com/v3/orgs/members/#list-members
6261
*
6362
* @param string $name the organization name
6463
* @return array the members
@@ -70,7 +69,7 @@ public function getPublicMembers($name)
7069

7170
/**
7271
* Check that user is in that organization
73-
* @link http://developer.github.com/v3/orgs/members/
72+
* @link http://developer.github.com/v3/orgs/members/#get-member
7473
*
7574
* @param string $name the organization name
7675
* @param string $user the user
@@ -83,7 +82,7 @@ public function getPublicMember($name, $user)
8382

8483
/**
8584
* List all teams of that organization
86-
* @link http://developer.github.com/v3/orgs/teams/
85+
* @link http://developer.github.com/v3/orgs/teams/#list-teams
8786
*
8887
* @param string $name the organization name
8988
* @return array the teams
@@ -95,7 +94,7 @@ public function getTeams($name)
9594

9695
/**
9796
* Get team with given id of that organization
98-
* @link http://developer.github.com/v3/orgs/teams/
97+
* @link http://developer.github.com/v3/orgs/teams/#get-team
9998
*
10099
* @param string $name the organization name
101100
* @param string $id id of team
@@ -108,21 +107,21 @@ public function getTeam($name, $id)
108107

109108
/**
110109
* Add a team to that organization
111-
* @link http://developer.github.com/v3/orgs/teams/
110+
* @link http://developer.github.com/v3/orgs/teams/#create-team
112111
*
113-
* @param string $organization the organization name
114-
* @param string $team name of the new team
115-
* @param string $permission its permission [PULL|PUSH|ADMIN]
116-
* @param array $repositories (optional) its repositories names
112+
* @param string $organization the organization name
113+
* @param string $team name of the new team
114+
* @param string $permission its permission [PULL|PUSH|ADMIN]
115+
* @param array $repositories (optional) its repositories names
117116
*
118-
* @return array the teams
117+
* @return array the teams
119118
*
120-
* @throws \InvalidArgumentException
119+
* @throws InvalidArgumentException
121120
*/
122121
public function addTeam($organization, $team, $permission = 'pull', array $repositories = array())
123122
{
124123
if (!in_array($permission, array(self::ADMIN, self::PUSH, self::PULL))) {
125-
throw new \InvalidArgumentException("Invalid value for the permission variable");
124+
throw new InvalidArgumentException("Invalid value for the permission variable");
126125
}
127126

128127
return $this->post('orgs/'.urlencode($organization).'/teams', array(

lib/Github/Client.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
/**
1010
* Simple yet very cool PHP Github client
1111
*
12-
* @tutorial http://github.com/ornicar/php-github-api/blob/master/README.markdown
13-
* @version 3.2
14-
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
15-
* @license MIT License
12+
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
13+
* @author Joseph Bielawski <stloyd@gmail.com>
1614
*
17-
* Website: http://github.com/ornicar/php-github-api
18-
* Tickets: http://github.com/ornicar/php-github-api/issues
15+
* Website: http://github.com/KnpLabs/php-github-api
1916
*/
2017
class Client
2118
{
@@ -59,7 +56,7 @@ class Client
5956
protected $apis = array();
6057

6158
/**
62-
* Instanciate a new GitHub client
59+
* Instantiate a new GitHub client
6360
*
6461
* @param HttpClientInterface $httpClient custom http client
6562
*/
@@ -79,16 +76,19 @@ public function authenticate($login, $secret, $method = null)
7976
{
8077
$this->getHttpClient()->setOption('auth_method', $method ?: self::AUTH_URL_TOKEN);
8178

82-
if (self::AUTH_HTTP_PASSWORD) {
83-
$this->getHttpClient()->setOption('login', $login)
84-
->setOption('password', $secret);
79+
if ($method === self::AUTH_HTTP_PASSWORD) {
80+
$this
81+
->getHttpClient()
82+
->setOption('login', $login)
83+
->setOption('password', $secret)
84+
;
8585
} else {
8686
$this->getHttpClient()->setOption('token', $secret);
8787
}
8888
}
8989

9090
/**
91-
* Deauthenticate a user for all next requests
91+
* De-authenticate a user for all next requests
9292
*/
9393
public function deAuthenticate()
9494
{
@@ -287,7 +287,7 @@ public function getPullRequestApi()
287287
* @param string $name the API name
288288
* @param ApiInterface $api the API instance
289289
*
290-
* @return self
290+
* @return Client
291291
*/
292292
public function setApi($name, ApiInterface $instance)
293293
{
@@ -307,11 +307,17 @@ public function getApi($name)
307307
return $this->apis[$name];
308308
}
309309

310+
/**
311+
* Clears used headers
312+
*/
310313
public function clearHeaders()
311314
{
312315
$this->setHeaders(array());
313316
}
314317

318+
/**
319+
* @param array $headers
320+
*/
315321
public function setHeaders($headers)
316322
{
317323
$this->headers = $headers;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Github\Exception;
4+
5+
/**
6+
* ApiLimitExceedException
7+
*
8+
* @author Joseph Bielawski <stloyd@gmail.com>
9+
*/
10+
class ApiLimitExceedException extends \RuntimeException
11+
{
12+
public function __construct($limit)
13+
{
14+
parent::__construct('You have reached GitHub hour limit! Actual limit is: '. $limit);
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Github\Exception;
4+
5+
/**
6+
* InvalidArgumentException
7+
*
8+
* @author Joseph Bielawski <stloyd@gmail.com>
9+
*/
10+
class InvalidArgumentException extends \InvalidArgumentException
11+
{
12+
13+
}

0 commit comments

Comments
 (0)