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