1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of the Joomla Framework Github Package |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\Github; |
10
|
|
|
|
11
|
|
|
use Joomla\Http\Exception\UnexpectedResponseException; |
12
|
|
|
use Joomla\Http\Response; |
13
|
|
|
use Joomla\Uri\Uri; |
14
|
|
|
use Joomla\Registry\Registry; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* GitHub API object class for the Joomla Framework. |
18
|
|
|
* |
19
|
|
|
* @since 1.0 |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractGithubObject |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Registry Options for the GitHub object. |
25
|
|
|
* @since 1.0 |
26
|
|
|
*/ |
27
|
|
|
protected $options; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Http The HTTP client object to use in sending HTTP requests. |
31
|
|
|
* @since 1.0 |
32
|
|
|
*/ |
33
|
|
|
protected $client; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string The package the object resides in |
37
|
|
|
* @since 1.0 |
38
|
|
|
*/ |
39
|
|
|
protected $package = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Array containing the allowed hook events |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
* @since 1.5.2 |
46
|
|
|
* @see https://developer.github.com/webhooks/#events |
47
|
|
|
* @note From 1.4.0 to 1.5.1 this was named $events, it was renamed due to naming conflicts with package subclasses |
48
|
|
|
*/ |
49
|
|
|
protected $hookEvents = array( |
50
|
|
|
'*', |
51
|
|
|
'commit_comment', |
52
|
|
|
'create', |
53
|
|
|
'delete', |
54
|
|
|
'deployment', |
55
|
|
|
'deployment_status', |
56
|
|
|
'fork', |
57
|
|
|
'gollum', |
58
|
|
|
'issue_comment', |
59
|
|
|
'issues', |
60
|
|
|
'member', |
61
|
|
|
'membership', |
62
|
|
|
'page_build', |
63
|
|
|
'public', |
64
|
|
|
'pull_request_review_comment', |
65
|
|
|
'pull_request', |
66
|
|
|
'push', |
67
|
|
|
'repository', |
68
|
|
|
'release', |
69
|
|
|
'status', |
70
|
|
|
'team_add', |
71
|
|
|
'watch', |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Constructor. |
76
|
|
|
* |
77
|
|
|
* @param Registry $options GitHub options object. |
78
|
|
|
* @param Http $client The HTTP client object. |
79
|
|
|
* |
80
|
|
|
* @since 1.0 |
81
|
|
|
*/ |
82
|
|
|
public function __construct(Registry $options = null, Http $client = null) |
83
|
|
|
{ |
84
|
|
|
$this->options = isset($options) ? $options : new Registry; |
85
|
|
|
$this->client = isset($client) ? $client : new Http($this->options); |
86
|
|
|
|
87
|
|
|
$this->package = get_class($this); |
88
|
|
|
$this->package = substr($this->package, strrpos($this->package, '\\') + 1); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Method to build and return a full request URL for the request. This method will |
93
|
|
|
* add appropriate pagination details if necessary and also prepend the API url |
94
|
|
|
* to have a complete URL for the request. |
95
|
|
|
* |
96
|
|
|
* @param string $path URL to inflect |
97
|
|
|
* @param integer $page Page to request |
98
|
|
|
* @param integer $limit Number of results to return per page |
99
|
|
|
* |
100
|
|
|
* @return string The request URL. |
101
|
|
|
* |
102
|
|
|
* @since 1.0 |
103
|
|
|
*/ |
104
|
|
|
protected function fetchUrl($path, $page = 0, $limit = 0) |
105
|
|
|
{ |
106
|
|
|
// Get a new Uri object focusing the api url and given path. |
107
|
|
|
$uri = new Uri($this->options->get('api.url') . $path); |
108
|
|
|
|
109
|
|
|
if ($this->options->get('gh.token', false)) |
110
|
|
|
{ |
111
|
|
|
// Use oAuth authentication - @todo set in request header ? |
112
|
|
|
$uri->setVar('access_token', $this->options->get('gh.token')); |
113
|
|
|
} |
114
|
|
|
else |
115
|
|
|
{ |
116
|
|
|
// Use basic authentication |
117
|
|
|
if ($this->options->get('api.username', false)) |
118
|
|
|
{ |
119
|
|
|
$uri->setUser($this->options->get('api.username')); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($this->options->get('api.password', false)) |
123
|
|
|
{ |
124
|
|
|
$uri->setPass($this->options->get('api.password')); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// If we have a defined page number add it to the JUri object. |
129
|
|
|
if ($page > 0) |
130
|
|
|
{ |
131
|
|
|
$uri->setVar('page', (int) $page); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// If we have a defined items per page add it to the JUri object. |
135
|
|
|
if ($limit > 0) |
136
|
|
|
{ |
137
|
|
|
$uri->setVar('per_page', (int) $limit); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return (string) $uri; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Process the response and decode it. |
145
|
|
|
* |
146
|
|
|
* @param Response $response The response. |
147
|
|
|
* @param integer $expectedCode The expected "good" code. |
148
|
|
|
* |
149
|
|
|
* @return mixed |
150
|
|
|
* |
151
|
|
|
* @since 1.0 |
152
|
|
|
* @throws UnexpectedResponseException |
153
|
|
|
*/ |
154
|
|
|
protected function processResponse(Response $response, $expectedCode = 200) |
155
|
|
|
{ |
156
|
|
|
// Validate the response code. |
157
|
|
View Code Duplication |
if ($response->code != $expectedCode) |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
// Decode the error response and throw an exception. |
160
|
|
|
$error = json_decode($response->body); |
161
|
|
|
$message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; |
162
|
|
|
throw new UnexpectedResponseException($response, $message, $response->code); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return json_decode($response->body); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.