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\Package; |
10
|
|
|
|
11
|
|
|
use Joomla\Github\AbstractPackage; |
12
|
|
|
use Joomla\Http\Exception\UnexpectedResponseException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* GitHub API Gists class for the Joomla Framework. |
16
|
|
|
* |
17
|
|
|
* @documentation http://developer.github.com/v3/gists |
18
|
|
|
* |
19
|
|
|
* @since 1.0 |
20
|
|
|
* |
21
|
|
|
* @property-read Gists\Comments $comments GitHub API object for gist comments. |
22
|
|
|
*/ |
23
|
|
|
class Gists extends AbstractPackage |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Create a gist. |
27
|
|
|
* |
28
|
|
|
* @param mixed $files Either an array of file paths or a single file path as a string. |
29
|
|
|
* @param boolean $public True if the gist should be public. |
30
|
|
|
* @param string $description The optional description of the gist. |
31
|
|
|
* |
32
|
|
|
* @return object |
33
|
|
|
* |
34
|
|
|
* @since 1.0 |
35
|
|
|
* @throws \DomainException |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function create($files, $public = false, $description = null) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
// Build the request path. |
40
|
|
|
$path = '/gists'; |
41
|
|
|
|
42
|
|
|
// Build the request data. |
43
|
|
|
$data = json_encode( |
44
|
|
|
array( |
45
|
|
|
'files' => $this->buildFileData((array) $files), |
46
|
|
|
'public' => (bool) $public, |
47
|
|
|
'description' => $description |
48
|
|
|
) |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
// Send the request. |
52
|
|
|
return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Delete a gist. |
57
|
|
|
* |
58
|
|
|
* @param integer $gistId The gist number. |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
* |
62
|
|
|
* @since 1.0 |
63
|
|
|
* @throws \DomainException |
64
|
|
|
*/ |
65
|
|
|
public function delete($gistId) |
66
|
|
|
{ |
67
|
|
|
// Build the request path. |
68
|
|
|
$path = '/gists/' . (int) $gistId; |
69
|
|
|
|
70
|
|
|
// Send the request. |
71
|
|
|
$this->processResponse($this->client->delete($this->fetchUrl($path)), 204); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Edit a gist. |
76
|
|
|
* |
77
|
|
|
* @param integer $gistId The gist number. |
78
|
|
|
* @param mixed $files Either an array of file paths or a single file path as a string. |
79
|
|
|
* @param boolean $public True if the gist should be public. |
80
|
|
|
* @param string $description The description of the gist. |
81
|
|
|
* |
82
|
|
|
* @return object |
83
|
|
|
* |
84
|
|
|
* @since 1.0 |
85
|
|
|
* @throws \DomainException |
86
|
|
|
*/ |
87
|
|
|
public function edit($gistId, $files = null, $public = null, $description = null) |
88
|
|
|
{ |
89
|
|
|
// Build the request path. |
90
|
|
|
$path = '/gists/' . (int) $gistId; |
91
|
|
|
|
92
|
|
|
// Create the data object. |
93
|
|
|
$data = new \stdClass; |
94
|
|
|
|
95
|
|
|
// If a description is set add it to the data object. |
96
|
|
|
if (isset($description)) |
97
|
|
|
{ |
98
|
|
|
$data->description = $description; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// If the public flag is set add it to the data object. |
102
|
|
|
if (isset($public)) |
103
|
|
|
{ |
104
|
|
|
$data->public = $public; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// If a state is set add it to the data object. |
108
|
|
|
if (isset($files)) |
109
|
|
|
{ |
110
|
|
|
$data->files = $this->buildFileData((array) $files); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Encode the request data. |
114
|
|
|
$data = json_encode($data); |
115
|
|
|
|
116
|
|
|
// Send the request. |
117
|
|
|
return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Fork a gist. |
122
|
|
|
* |
123
|
|
|
* @param integer $gistId The gist number. |
124
|
|
|
* |
125
|
|
|
* @return object |
126
|
|
|
* |
127
|
|
|
* @since 1.0 |
128
|
|
|
* @throws \DomainException |
129
|
|
|
*/ |
130
|
|
|
public function fork($gistId) |
131
|
|
|
{ |
132
|
|
|
// Build the request path. |
133
|
|
|
$path = '/gists/' . (int) $gistId . '/forks'; |
134
|
|
|
|
135
|
|
|
// Send the request. |
136
|
|
|
return $this->processResponse($this->client->post($this->fetchUrl($path), ''), 201); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get a single gist. |
141
|
|
|
* |
142
|
|
|
* @param integer $gistId The gist number. |
143
|
|
|
* |
144
|
|
|
* @return object |
145
|
|
|
* |
146
|
|
|
* @since 1.0 |
147
|
|
|
* @throws \DomainException |
148
|
|
|
*/ |
149
|
|
|
public function get($gistId) |
150
|
|
|
{ |
151
|
|
|
// Build the request path. |
152
|
|
|
$path = '/gists/' . (int) $gistId; |
153
|
|
|
|
154
|
|
|
// Send the request. |
155
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* List gist commits. |
160
|
|
|
* |
161
|
|
|
* @param integer $gistId The gist number. |
162
|
|
|
* @param integer $page The page number from which to get items. |
163
|
|
|
* @param integer $limit The number of items on a page. |
164
|
|
|
* |
165
|
|
|
* @return array |
166
|
|
|
* |
167
|
|
|
* @since 1.4.0 |
168
|
|
|
* @throws \DomainException |
169
|
|
|
*/ |
170
|
|
|
public function getCommitList($gistId, $page = 0, $limit = 0) |
171
|
|
|
{ |
172
|
|
|
// Build the request path. |
173
|
|
|
$path = '/gists/' . (int) $gistId . '/commits'; |
174
|
|
|
|
175
|
|
|
// Send the request. |
176
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* List gist forks. |
181
|
|
|
* |
182
|
|
|
* @param integer $gistId The gist number. |
183
|
|
|
* @param integer $page The page number from which to get items. |
184
|
|
|
* @param integer $limit The number of items on a page. |
185
|
|
|
* |
186
|
|
|
* @return array |
187
|
|
|
* |
188
|
|
|
* @since 1.4.0 |
189
|
|
|
* @throws \DomainException |
190
|
|
|
*/ |
191
|
|
|
public function getForkList($gistId, $page = 0, $limit = 0) |
192
|
|
|
{ |
193
|
|
|
// Build the request path. |
194
|
|
|
$path = '/gists/' . (int) $gistId . '/forks'; |
195
|
|
|
|
196
|
|
|
// Send the request. |
197
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* List gists. |
202
|
|
|
* |
203
|
|
|
* If a user is authenticated it will return the user's gists, otherwise |
204
|
|
|
* it will return all public gists. |
205
|
|
|
* |
206
|
|
|
* @param integer $page The page number from which to get items. |
207
|
|
|
* @param integer $limit The number of items on a page. |
208
|
|
|
* |
209
|
|
|
* @return array |
210
|
|
|
* |
211
|
|
|
* @since 1.0 |
212
|
|
|
* @throws \DomainException |
213
|
|
|
*/ |
214
|
|
|
public function getList($page = 0, $limit = 0) |
215
|
|
|
{ |
216
|
|
|
// Build the request path. |
217
|
|
|
$path = '/gists'; |
218
|
|
|
|
219
|
|
|
// Send the request. |
220
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* List a user’s gists. |
225
|
|
|
* |
226
|
|
|
* @param string $user The name of the GitHub user from which to list gists. |
227
|
|
|
* @param integer $page The page number from which to get items. |
228
|
|
|
* @param integer $limit The number of items on a page. |
229
|
|
|
* @param \DateTime $since Only gists updated at or after this time are returned. |
230
|
|
|
* |
231
|
|
|
* @return array |
232
|
|
|
* |
233
|
|
|
* @since 1.0 |
234
|
|
|
* @throws \DomainException |
235
|
|
|
*/ |
236
|
|
View Code Duplication |
public function getListByUser($user, $page = 0, $limit = 0, \DateTime $since = null) |
|
|
|
|
237
|
|
|
{ |
238
|
|
|
// Build the request path. |
239
|
|
|
$path = '/users/' . $user . '/gists'; |
240
|
|
|
$path .= ($since) ? '?since=' . $since->format(\DateTime::RFC3339) : ''; |
241
|
|
|
|
242
|
|
|
// Send the request. |
243
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* List all public gists. |
248
|
|
|
* |
249
|
|
|
* @param integer $page The page number from which to get items. |
250
|
|
|
* @param integer $limit The number of items on a page. |
251
|
|
|
* @param \DateTime $since Only gists updated at or after this time are returned. |
252
|
|
|
* |
253
|
|
|
* @return array |
254
|
|
|
* |
255
|
|
|
* @since 1.0 |
256
|
|
|
* @throws \DomainException |
257
|
|
|
*/ |
258
|
|
View Code Duplication |
public function getListPublic($page = 0, $limit = 0, \DateTime $since = null) |
|
|
|
|
259
|
|
|
{ |
260
|
|
|
// Build the request path. |
261
|
|
|
$path = '/gists/public'; |
262
|
|
|
$path .= ($since) ? '?since=' . $since->format(\DateTime::RFC3339) : ''; |
263
|
|
|
|
264
|
|
|
// Send the request. |
265
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* List starred gists. |
270
|
|
|
* |
271
|
|
|
* @param integer $page The page number from which to get items. |
272
|
|
|
* @param integer $limit The number of items on a page. |
273
|
|
|
* @param \DateTime $since Only gists updated at or after this time are returned. |
274
|
|
|
* |
275
|
|
|
* @return array |
276
|
|
|
* |
277
|
|
|
* @since 1.0 |
278
|
|
|
* @throws \DomainException |
279
|
|
|
*/ |
280
|
|
View Code Duplication |
public function getListStarred($page = 0, $limit = 0, \DateTime $since = null) |
|
|
|
|
281
|
|
|
{ |
282
|
|
|
// Build the request path. |
283
|
|
|
$path = '/gists/starred'; |
284
|
|
|
$path .= ($since) ? '?since=' . $since->format(\DateTime::RFC3339) : ''; |
285
|
|
|
|
286
|
|
|
// Send the request. |
287
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Get a specific revision of a gist. |
292
|
|
|
* |
293
|
|
|
* @param integer $gistId The gist number. |
294
|
|
|
* @param string $sha The SHA for the revision to get. |
295
|
|
|
* |
296
|
|
|
* @return object |
297
|
|
|
* |
298
|
|
|
* @since 1.4.0 |
299
|
|
|
* @throws \DomainException |
300
|
|
|
*/ |
301
|
|
|
public function getRevision($gistId, $sha) |
302
|
|
|
{ |
303
|
|
|
// Build the request path. |
304
|
|
|
$path = '/gists/' . (int) $gistId . '/' . $sha; |
305
|
|
|
|
306
|
|
|
// Send the request. |
307
|
|
|
return $this->processResponse($this->client->get($this->fetchUrl($path))); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Check if a gist is starred. |
312
|
|
|
* |
313
|
|
|
* @param integer $gistId The gist number. |
314
|
|
|
* |
315
|
|
|
* @return boolean True if gist is starred |
316
|
|
|
* |
317
|
|
|
* @since 1.0 |
318
|
|
|
* @throws UnexpectedResponseException |
319
|
|
|
*/ |
320
|
|
|
public function isStarred($gistId) |
321
|
|
|
{ |
322
|
|
|
// Build the request path. |
323
|
|
|
$path = '/gists/' . (int) $gistId . '/star'; |
324
|
|
|
|
325
|
|
|
// Send the request. |
326
|
|
|
$response = $this->client->get($this->fetchUrl($path)); |
327
|
|
|
|
328
|
|
|
// Validate the response code. |
329
|
|
|
if ($response->code == 204) |
330
|
|
|
{ |
331
|
|
|
return true; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
if ($response->code == 404) |
335
|
|
|
{ |
336
|
|
|
return false; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
// Decode the error response and throw an exception. |
340
|
|
|
$error = json_decode($response->body); |
341
|
|
|
$message = isset($error->message) ? $error->message : 'Invalid response received from GitHub.'; |
342
|
|
|
throw new UnexpectedResponseException($response, $message, $response->code); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Star a gist. |
347
|
|
|
* |
348
|
|
|
* @param integer $gistId The gist number. |
349
|
|
|
* |
350
|
|
|
* @return void |
351
|
|
|
* |
352
|
|
|
* @since 1.0 |
353
|
|
|
* @throws \DomainException |
354
|
|
|
*/ |
355
|
|
|
public function star($gistId) |
356
|
|
|
{ |
357
|
|
|
// Build the request path. |
358
|
|
|
$path = '/gists/' . (int) $gistId . '/star'; |
359
|
|
|
|
360
|
|
|
// Send the request. |
361
|
|
|
$this->processResponse($this->client->put($this->fetchUrl($path), ''), 204); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Unstar a gist. |
366
|
|
|
* |
367
|
|
|
* @param integer $gistId The gist number. |
368
|
|
|
* |
369
|
|
|
* @return void |
370
|
|
|
* |
371
|
|
|
* @since 1.0 |
372
|
|
|
* @throws \DomainException |
373
|
|
|
*/ |
374
|
|
|
public function unstar($gistId) |
375
|
|
|
{ |
376
|
|
|
// Build the request path. |
377
|
|
|
$path = '/gists/' . (int) $gistId . '/star'; |
378
|
|
|
|
379
|
|
|
// Send the request. |
380
|
|
|
$this->processResponse($this->client->delete($this->fetchUrl($path)), 204); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Method to fetch a data array for transmitting to the GitHub API for a list of files based on |
385
|
|
|
* an input array of file paths or filename and content pairs. |
386
|
|
|
* |
387
|
|
|
* @param array $files The list of file paths or filenames and content. |
388
|
|
|
* |
389
|
|
|
* @return array |
390
|
|
|
* |
391
|
|
|
* @since 1.0 |
392
|
|
|
* @throws \InvalidArgumentException |
393
|
|
|
*/ |
394
|
|
|
protected function buildFileData(array $files) |
395
|
|
|
{ |
396
|
|
|
$data = array(); |
397
|
|
|
|
398
|
|
|
foreach ($files as $key => $file) |
399
|
|
|
{ |
400
|
|
|
if (!is_numeric($key)) |
401
|
|
|
{ |
402
|
|
|
// If the key isn't numeric, then we are dealing with a file whose content has been supplied |
403
|
|
|
$data[$key] = array('content' => $file); |
404
|
|
|
} |
405
|
|
|
elseif (!file_exists($file)) |
406
|
|
|
{ |
407
|
|
|
// Otherwise, we have been given a path and we have to load the content |
408
|
|
|
// Verify that the each file exists. |
409
|
|
|
throw new \InvalidArgumentException('The file ' . $file . ' does not exist.'); |
410
|
|
|
} |
411
|
|
|
else |
412
|
|
|
{ |
413
|
|
|
$data[basename($file)] = array('content' => file_get_contents($file)); |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
return $data; |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
|
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.