1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage GitHub
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * GitHub API Account class for the Joomla Platform.
14 *
15 * @since 12.3
16 * @deprecated 4.0 Use the `joomla/github` package via Composer instead
17 */
18 class JGithubAccount extends JGithubObject
19 {
20 /**
21 * Method to create an authorisation.
22 *
23 * @param array $scopes A list of scopes that this authorisation is in.
24 * @param string $note A note to remind you what the OAuth token is for.
25 * @param string $url A URL to remind you what app the OAuth token is for.
26 *
27 * @deprecated use authorization->create()
28 *
29 * @return object
30 *
31 * @since 12.3
32 * @throws DomainException
33 */
34 public function createAuthorisation(array $scopes = array(), $note = '', $url = '')
35 {
36 // Build the request path.
37 $path = '/authorizations';
38
39 $data = json_encode(
40 array('scopes' => $scopes, 'note' => $note, 'note_url' => $url)
41 );
42
43 // Send the request.
44 $response = $this->client->post($this->fetchUrl($path), $data);
45
46 // Validate the response code.
47 if ($response->code != 201)
48 {
49 // Decode the error response and throw an exception.
50 $error = json_decode($response->body);
51 throw new DomainException($error->message, $response->code);
52 }
53
54 return json_decode($response->body);
55 }
56
57 /**
58 * Method to delete an authorisation
59 *
60 * @param integer $id ID of the authorisation to delete
61 *
62 * @deprecated use authorization->delete()
63 *
64 * @return object
65 *
66 * @since 12.3
67 * @throws DomainException
68 */
69 public function deleteAuthorisation($id)
70 {
71 // Build the request path.
72 $path = '/authorizations/' . $id;
73
74 // Send the request.
75 $response = $this->client->delete($this->fetchUrl($path));
76
77 // Validate the response code.
78 if ($response->code != 204)
79 {
80 // Decode the error response and throw an exception.
81 $error = json_decode($response->body);
82 throw new DomainException($error->message, $response->code);
83 }
84
85 return json_decode($response->body);
86 }
87
88 /**
89 * Method to edit an authorisation.
90 *
91 * @param integer $id ID of the authorisation to edit
92 * @param array $scopes Replaces the authorisation scopes with these.
93 * @param array $addScopes A list of scopes to add to this authorisation.
94 * @param array $removeScopes A list of scopes to remove from this authorisation.
95 * @param string $note A note to remind you what the OAuth token is for.
96 * @param string $url A URL to remind you what app the OAuth token is for.
97 *
98 * @deprecated use authorization->edit()
99 *
100 * @return object
101 *
102 * @since 12.3
103 * @throws DomainException
104 * @throws RuntimeException
105 */
106 public function editAuthorisation($id, array $scopes = array(), array $addScopes = array(), array $removeScopes = array(), $note = '', $url = '')
107 {
108 // Check if more than one scopes array contains data
109 $scopesCount = 0;
110
111 if (!empty($scopes))
112 {
113 $scope = 'scopes';
114 $scopeData = $scopes;
115 $scopesCount++;
116 }
117
118 if (!empty($addScopes))
119 {
120 $scope = 'add_scopes';
121 $scopeData = $addScopes;
122 $scopesCount++;
123 }
124
125 if (!empty($removeScopes))
126 {
127 $scope = 'remove_scopes';
128 $scopeData = $removeScopes;
129 $scopesCount++;
130 }
131
132 // Only allowed to send data for one scope parameter
133 if ($scopesCount >= 2)
134 {
135 throw new RuntimeException('You can only send one scope key in this request.');
136 }
137
138 // Build the request path.
139 $path = '/authorizations/' . $id;
140
141 $data = json_encode(
142 array(
143 $scope => $scopeData,
144 'note' => $note,
145 'note_url' => $url,
146 )
147 );
148
149 // Send the request.
150 $response = $this->client->patch($this->fetchUrl($path), $data);
151
152 // Validate the response code.
153 if ($response->code != 200)
154 {
155 // Decode the error response and throw an exception.
156 $error = json_decode($response->body);
157 throw new DomainException($error->message, $response->code);
158 }
159
160 return json_decode($response->body);
161 }
162
163 /**
164 * Method to get details about an authorised application for the authenticated user.
165 *
166 * @param integer $id ID of the authorisation to retrieve
167 *
168 * @deprecated use authorization->get()
169 *
170 * @return object
171 *
172 * @since 12.3
173 * @note This method will only accept Basic Authentication
174 * @throws DomainException
175 */
176 public function getAuthorisation($id)
177 {
178 // Build the request path.
179 $path = '/authorizations/' . $id;
180
181 // Send the request.
182 $response = $this->client->get($this->fetchUrl($path));
183
184 // Validate the response code.
185 if ($response->code != 200)
186 {
187 // Decode the error response and throw an exception.
188 $error = json_decode($response->body);
189 throw new DomainException($error->message, $response->code);
190 }
191
192 return json_decode($response->body);
193 }
194
195 /**
196 * Method to get the authorised applications for the authenticated user.
197 *
198 * @deprecated use authorization->getList()
199 *
200 * @return object
201 *
202 * @since 12.3
203 * @throws DomainException
204 * @note This method will only accept Basic Authentication
205 */
206 public function getAuthorisations()
207 {
208 // Build the request path.
209 $path = '/authorizations';
210
211 // Send the request.
212 $response = $this->client->get($this->fetchUrl($path));
213
214 // Validate the response code.
215 if ($response->code != 200)
216 {
217 // Decode the error response and throw an exception.
218 $error = json_decode($response->body);
219 throw new DomainException($error->message, $response->code);
220 }
221
222 return json_decode($response->body);
223 }
224
225 /**
226 * Method to get the rate limit for the authenticated user.
227 *
228 * @deprecated use authorization->getRateLimit()
229 *
230 * @return object
231 *
232 * @since 12.3
233 * @throws DomainException
234 */
235 public function getRateLimit()
236 {
237 // Build the request path.
238 $path = '/rate_limit';
239
240 // Send the request.
241 $response = $this->client->get($this->fetchUrl($path));
242
243 // Validate the response code.
244 if ($response->code != 200)
245 {
246 // Decode the error response and throw an exception.
247 $error = json_decode($response->body);
248 throw new DomainException($error->message, $response->code);
249 }
250
251 return json_decode($response->body);
252 }
253 }
254