PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/github/gists.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 593 lines | 246 code | 75 blank | 272 comment | 39 complexity | a5091381727d056b554e6c534f81824f MD5 | raw file
  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. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * GitHub API Gists class for the Joomla Platform.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage GitHub
  15. * @since 11.3
  16. */
  17. class JGithubGists extends JGithubObject
  18. {
  19. /**
  20. * Method to create a gist.
  21. *
  22. * @param mixed $files Either an array of file paths or a single file path as a string.
  23. * @param boolean $public True if the gist should be public.
  24. * @param string $description The optional description of the gist.
  25. *
  26. * @return object
  27. *
  28. * @since 11.3
  29. */
  30. public function create($files, $public = false, $description = null)
  31. {
  32. // Build the request path.
  33. $path = '/gists';
  34. // Build the request data.
  35. $data = json_encode(
  36. array(
  37. 'files' => $this->buildFileData((array) $files),
  38. 'public' => (bool) $public,
  39. 'description' => $description
  40. )
  41. );
  42. // Send the request.
  43. $response = $this->client->post($this->fetchUrl($path), $data);
  44. // Validate the response code.
  45. if ($response->code != 201)
  46. {
  47. // Decode the error response and throw an exception.
  48. $error = json_decode($response->body);
  49. throw new DomainException($error->message, $response->code);
  50. }
  51. return json_decode($response->body);
  52. }
  53. /**
  54. * Method to create a comment on a gist.
  55. *
  56. * @param integer $gistId The gist number.
  57. * @param string $body The comment body text.
  58. *
  59. * @return object
  60. *
  61. * @since 11.3
  62. */
  63. public function createComment($gistId, $body)
  64. {
  65. // Build the request path.
  66. $path = '/gists/' . (int) $gistId . '/comments';
  67. // Build the request data.
  68. $data = json_encode(
  69. array(
  70. 'body' => $body,
  71. )
  72. );
  73. // Send the request.
  74. $response = $this->client->post($this->fetchUrl($path), $data);
  75. // Validate the response code.
  76. if ($response->code != 201)
  77. {
  78. // Decode the error response and throw an exception.
  79. $error = json_decode($response->body);
  80. throw new DomainException($error->message, $response->code);
  81. }
  82. return json_decode($response->body);
  83. }
  84. /**
  85. * Method to delete a gist.
  86. *
  87. * @param integer $gistId The gist number.
  88. *
  89. * @return void
  90. *
  91. * @since 11.3
  92. */
  93. public function delete($gistId)
  94. {
  95. // Build the request path.
  96. $path = '/gists/' . (int) $gistId;
  97. // Send the request.
  98. $response = $this->client->delete($this->fetchUrl($path));
  99. // Validate the response code.
  100. if ($response->code != 204)
  101. {
  102. // Decode the error response and throw an exception.
  103. $error = json_decode($response->body);
  104. throw new DomainException($error->message, $response->code);
  105. }
  106. }
  107. /**
  108. * Method to delete a comment on a gist.
  109. *
  110. * @param integer $commentId The id of the comment to delete.
  111. *
  112. * @return void
  113. *
  114. * @since 11.3
  115. */
  116. public function deleteComment($commentId)
  117. {
  118. // Build the request path.
  119. $path = '/gists/comments/' . (int) $commentId;
  120. // Send the request.
  121. $response = $this->client->delete($this->fetchUrl($path));
  122. // Validate the response code.
  123. if ($response->code != 204)
  124. {
  125. // Decode the error response and throw an exception.
  126. $error = json_decode($response->body);
  127. throw new DomainException($error->message, $response->code);
  128. }
  129. }
  130. /**
  131. * Method to update a gist.
  132. *
  133. * @param integer $gistId The gist number.
  134. * @param mixed $files Either an array of file paths or a single file path as a string.
  135. * @param boolean $public True if the gist should be public.
  136. * @param string $description The description of the gist.
  137. *
  138. * @return object
  139. *
  140. * @since 11.3
  141. */
  142. public function edit($gistId, $files = null, $public = null, $description = null)
  143. {
  144. // Build the request path.
  145. $path = '/gists/' . (int) $gistId;
  146. // Craete the data object.
  147. $data = new stdClass;
  148. // If a description is set add it to the data object.
  149. if (isset($description))
  150. {
  151. $data->description = $description;
  152. }
  153. // If the public flag is set add it to the data object.
  154. if (isset($public))
  155. {
  156. $data->public = $public;
  157. }
  158. // If a state is set add it to the data object.
  159. if (isset($files))
  160. {
  161. $data->files = $this->buildFileData((array) $files);
  162. }
  163. // Encode the request data.
  164. $data = json_encode($data);
  165. // Send the request.
  166. $response = $this->client->patch($this->fetchUrl($path), $data);
  167. // Validate the response code.
  168. if ($response->code != 200)
  169. {
  170. // Decode the error response and throw an exception.
  171. $error = json_decode($response->body);
  172. throw new DomainException($error->message, $response->code);
  173. }
  174. return json_decode($response->body);
  175. }
  176. /**
  177. * Method to update a comment on a gist.
  178. *
  179. * @param integer $commentId The id of the comment to update.
  180. * @param string $body The new body text for the comment.
  181. *
  182. * @return object
  183. *
  184. * @since 11.3
  185. */
  186. public function editComment($commentId, $body)
  187. {
  188. // Build the request path.
  189. $path = '/gists/comments/' . (int) $commentId;
  190. // Build the request data.
  191. $data = json_encode(
  192. array(
  193. 'body' => $body
  194. )
  195. );
  196. // Send the request.
  197. $response = $this->client->patch($this->fetchUrl($path), $data);
  198. // Validate the response code.
  199. if ($response->code != 200)
  200. {
  201. // Decode the error response and throw an exception.
  202. $error = json_decode($response->body);
  203. throw new DomainException($error->message, $response->code);
  204. }
  205. return json_decode($response->body);
  206. }
  207. /**
  208. * Method to fork a gist.
  209. *
  210. * @param integer $gistId The gist number.
  211. *
  212. * @return object
  213. *
  214. * @since 11.3
  215. */
  216. public function fork($gistId)
  217. {
  218. // Build the request path.
  219. $path = '/gists/' . (int) $gistId . '/fork';
  220. // Send the request.
  221. // TODO: Verify change
  222. $response = $this->client->post($this->fetchUrl($path), '');
  223. // Validate the response code.
  224. if ($response->code != 201)
  225. {
  226. // Decode the error response and throw an exception.
  227. $error = json_decode($response->body);
  228. throw new DomainException($error->message, $response->code);
  229. }
  230. return json_decode($response->body);
  231. }
  232. /**
  233. * Method to get a single gist.
  234. *
  235. * @param integer $gistId The gist number.
  236. *
  237. * @return object
  238. *
  239. * @since 11.3
  240. */
  241. public function get($gistId)
  242. {
  243. // Build the request path.
  244. $path = '/gists/' . (int) $gistId;
  245. // Send the request.
  246. $response = $this->client->get($this->fetchUrl($path));
  247. // Validate the response code.
  248. if ($response->code != 200)
  249. {
  250. // Decode the error response and throw an exception.
  251. $error = json_decode($response->body);
  252. throw new DomainException($error->message, $response->code);
  253. }
  254. return json_decode($response->body);
  255. }
  256. /**
  257. * Method to get a specific comment on a gist.
  258. *
  259. * @param integer $commentId The comment id to get.
  260. *
  261. * @return object
  262. *
  263. * @since 11.3
  264. */
  265. public function getComment($commentId)
  266. {
  267. // Build the request path.
  268. $path = '/gists/comments/' . (int) $commentId;
  269. // Send the request.
  270. $response = $this->client->get($this->fetchUrl($path));
  271. // Validate the response code.
  272. if ($response->code != 200)
  273. {
  274. // Decode the error response and throw an exception.
  275. $error = json_decode($response->body);
  276. throw new DomainException($error->message, $response->code);
  277. }
  278. return json_decode($response->body);
  279. }
  280. /**
  281. * Method to get the list of comments on a gist.
  282. *
  283. * @param integer $gistId The gist number.
  284. * @param integer $page The page number from which to get items.
  285. * @param integer $limit The number of items on a page.
  286. *
  287. * @return array
  288. *
  289. * @since 11.3
  290. */
  291. public function getComments($gistId, $page = 0, $limit = 0)
  292. {
  293. // Build the request path.
  294. $path = '/gists/' . (int) $gistId . '/comments';
  295. // Send the request.
  296. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  297. // Validate the response code.
  298. if ($response->code != 200)
  299. {
  300. // Decode the error response and throw an exception.
  301. $error = json_decode($response->body);
  302. throw new DomainException($error->message, $response->code);
  303. }
  304. return json_decode($response->body);
  305. }
  306. /**
  307. * Method to list gists. If a user is authenticated it will return the user's gists, otherwise
  308. * it will return all public gists.
  309. *
  310. * @param integer $page The page number from which to get items.
  311. * @param integer $limit The number of items on a page.
  312. *
  313. * @return array
  314. *
  315. * @since 11.3
  316. */
  317. public function getList($page = 0, $limit = 0)
  318. {
  319. // Build the request path.
  320. $path = '/gists';
  321. // Send the request.
  322. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  323. // Validate the response code.
  324. if ($response->code != 200)
  325. {
  326. // Decode the error response and throw an exception.
  327. $error = json_decode($response->body);
  328. throw new DomainException($error->message, $response->code);
  329. }
  330. return json_decode($response->body);
  331. }
  332. /**
  333. * Method to get a list of gists belonging to a given user.
  334. *
  335. * @param string $user The name of the GitHub user from which to list gists.
  336. * @param integer $page The page number from which to get items.
  337. * @param integer $limit The number of items on a page.
  338. *
  339. * @return array
  340. *
  341. * @since 11.3
  342. */
  343. public function getListByUser($user, $page = 0, $limit = 0)
  344. {
  345. // Build the request path.
  346. $path = '/users/' . $user . '/gists';
  347. // Send the request.
  348. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  349. // Validate the response code.
  350. if ($response->code != 200)
  351. {
  352. // Decode the error response and throw an exception.
  353. $error = json_decode($response->body);
  354. throw new DomainException($error->message, $response->code);
  355. }
  356. return json_decode($response->body);
  357. }
  358. /**
  359. * Method to get a list of all public gists.
  360. *
  361. * @param integer $page The page number from which to get items.
  362. * @param integer $limit The number of items on a page.
  363. *
  364. * @return array
  365. *
  366. * @since 11.3
  367. */
  368. public function getListPublic($page = 0, $limit = 0)
  369. {
  370. // Build the request path.
  371. $path = '/gists/public';
  372. // Send the request.
  373. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  374. // Validate the response code.
  375. if ($response->code != 200)
  376. {
  377. // Decode the error response and throw an exception.
  378. $error = json_decode($response->body);
  379. throw new DomainException($error->message, $response->code);
  380. }
  381. return json_decode($response->body);
  382. }
  383. /**
  384. * Method to get a list of the authenticated users' starred gists.
  385. *
  386. * @param integer $page The page number from which to get items.
  387. * @param integer $limit The number of items on a page.
  388. *
  389. * @return array
  390. *
  391. * @since 11.3
  392. */
  393. public function getListStarred($page = 0, $limit = 0)
  394. {
  395. // Build the request path.
  396. $path = '/gists/starred';
  397. // Send the request.
  398. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  399. // Validate the response code.
  400. if ($response->code != 200)
  401. {
  402. // Decode the error response and throw an exception.
  403. $error = json_decode($response->body);
  404. throw new DomainException($error->message, $response->code);
  405. }
  406. return json_decode($response->body);
  407. }
  408. /**
  409. * Method to check if a gist has been starred.
  410. *
  411. * @param integer $gistId The gist number.
  412. *
  413. * @return boolean True if the gist is starred.
  414. *
  415. * @since 11.3
  416. */
  417. public function isStarred($gistId)
  418. {
  419. // Build the request path.
  420. $path = '/gists/' . (int) $gistId . '/star';
  421. // Send the request.
  422. $response = $this->client->get($this->fetchUrl($path));
  423. // Validate the response code.
  424. if ($response->code == 204)
  425. {
  426. return true;
  427. }
  428. elseif ($response->code == 404)
  429. {
  430. return false;
  431. }
  432. else
  433. {
  434. // Decode the error response and throw an exception.
  435. $error = json_decode($response->body);
  436. throw new DomainException($error->message, $response->code);
  437. }
  438. }
  439. /**
  440. * Method to star a gist.
  441. *
  442. * @param integer $gistId The gist number.
  443. *
  444. * @return void
  445. *
  446. * @since 11.3
  447. */
  448. public function star($gistId)
  449. {
  450. // Build the request path.
  451. $path = '/gists/' . (int) $gistId . '/star';
  452. // Send the request.
  453. $response = $this->client->put($this->fetchUrl($path), '');
  454. // Validate the response code.
  455. if ($response->code != 204)
  456. {
  457. // Decode the error response and throw an exception.
  458. $error = json_decode($response->body);
  459. throw new DomainException($error->message, $response->code);
  460. }
  461. }
  462. /**
  463. * Method to star a gist.
  464. *
  465. * @param integer $gistId The gist number.
  466. *
  467. * @return void
  468. *
  469. * @since 11.3
  470. */
  471. public function unstar($gistId)
  472. {
  473. // Build the request path.
  474. $path = '/gists/' . (int) $gistId . '/star';
  475. // Send the request.
  476. $response = $this->client->delete($this->fetchUrl($path));
  477. // Validate the response code.
  478. if ($response->code != 204)
  479. {
  480. // Decode the error response and throw an exception.
  481. $error = json_decode($response->body);
  482. throw new DomainException($error->message, $response->code);
  483. }
  484. }
  485. /**
  486. * Method to fetch a data array for transmitting to the GitHub API for a list of files based on
  487. * an input array of file paths or filename and content pairs.
  488. *
  489. * @param array $files The list of file paths or filenames and content.
  490. *
  491. * @return array
  492. *
  493. * @since 11.3
  494. */
  495. protected function buildFileData(array $files)
  496. {
  497. $data = array();
  498. foreach ($files as $key => $file)
  499. {
  500. // If the key isn't numeric, then we are dealing with a file whose content has been supplied
  501. if (!is_numeric($key))
  502. {
  503. $data[$key] = array('content' => $file);
  504. }
  505. // Otherwise, we have been given a path and we have to load the content
  506. // Verify that the each file exists.
  507. elseif (!file_exists($file))
  508. {
  509. throw new InvalidArgumentException('The file ' . $file . ' does not exist.');
  510. }
  511. else
  512. {
  513. $data[basename($file)] = array('content' => file_get_contents($file));
  514. }
  515. }
  516. return $data;
  517. }
  518. }