PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Joomla/Github/Package/Issues/Milestones.php

https://github.com/piotr-cz/joomla-framework
PHP | 236 lines | 97 code | 33 blank | 106 comment | 17 complexity | 1d8e5f129698c8621e62a4852725f92e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Part of the Joomla Framework Github Package
  4. *
  5. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. namespace Joomla\Github\Package\Issues;
  9. use Joomla\Github\AbstractPackage;
  10. /**
  11. * GitHub API Milestones class for the Joomla Framework.
  12. *
  13. * @documentation http://developer.github.com/v3/issues/milestones/
  14. *
  15. * @since 1.0
  16. */
  17. class Milestones extends AbstractPackage
  18. {
  19. /**
  20. * Method to get the list of milestones for a repo.
  21. *
  22. * @param string $user The name of the owner of the GitHub repository.
  23. * @param string $repo The name of the GitHub repository.
  24. * @param string $state The milestone state to retrieved. Open (default) or closed.
  25. * @param string $sort Sort can be due_date (default) or completeness.
  26. * @param string $direction Direction is asc or desc (default).
  27. * @param integer $page The page number from which to get items.
  28. * @param integer $limit The number of items on a page.
  29. *
  30. * @throws \DomainException
  31. * @since 1.0
  32. *
  33. * @return array
  34. */
  35. public function getList($user, $repo, $state = 'open', $sort = 'due_date', $direction = 'desc', $page = 0, $limit = 0)
  36. {
  37. // Build the request path.
  38. $path = '/repos/' . $user . '/' . $repo . '/milestones?';
  39. $path .= 'state=' . $state;
  40. $path .= '&sort=' . $sort;
  41. $path .= '&direction=' . $direction;
  42. // Send the request.
  43. $response = $this->client->get($this->fetchUrl($path, $page, $limit));
  44. // Validate the response code.
  45. if ($response->code != 200)
  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 get a specific milestone.
  55. *
  56. * @param string $user The name of the owner of the GitHub repository.
  57. * @param string $repo The name of the GitHub repository.
  58. * @param integer $milestoneId The milestone id to get.
  59. *
  60. * @throws \DomainException
  61. * @return object
  62. *
  63. * @since 1.0
  64. */
  65. public function get($user, $repo, $milestoneId)
  66. {
  67. // Build the request path.
  68. $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId;
  69. // Send the request.
  70. $response = $this->client->get($this->fetchUrl($path));
  71. // Validate the response code.
  72. if ($response->code != 200)
  73. {
  74. // Decode the error response and throw an exception.
  75. $error = json_decode($response->body);
  76. throw new \DomainException($error->message, $response->code);
  77. }
  78. return json_decode($response->body);
  79. }
  80. /**
  81. * Method to create a milestone for a repository.
  82. *
  83. * @param string $user The name of the owner of the GitHub repository.
  84. * @param string $repo The name of the GitHub repository.
  85. * @param integer $title The title of the milestone.
  86. * @param string $state Can be open (default) or closed.
  87. * @param string $description Optional description for milestone.
  88. * @param string $due_on Optional ISO 8601 time.
  89. *
  90. * @throws \DomainException
  91. * @return object
  92. *
  93. * @since 1.0
  94. */
  95. public function create($user, $repo, $title, $state = null, $description = null, $due_on = null)
  96. {
  97. // Build the request path.
  98. $path = '/repos/' . $user . '/' . $repo . '/milestones';
  99. // Build the request data.
  100. $data = array(
  101. 'title' => $title
  102. );
  103. if (!is_null($state))
  104. {
  105. $data['state'] = $state;
  106. }
  107. if (!is_null($description))
  108. {
  109. $data['description'] = $description;
  110. }
  111. if (!is_null($due_on))
  112. {
  113. $data['due_on'] = $due_on;
  114. }
  115. $data = json_encode($data);
  116. // Send the request.
  117. $response = $this->client->post($this->fetchUrl($path), $data);
  118. // Validate the response code.
  119. if ($response->code != 201)
  120. {
  121. // Decode the error response and throw an exception.
  122. $error = json_decode($response->body);
  123. throw new \DomainException($error->message, $response->code);
  124. }
  125. return json_decode($response->body);
  126. }
  127. /**
  128. * Method to update a milestone.
  129. *
  130. * @param string $user The name of the owner of the GitHub repository.
  131. * @param string $repo The name of the GitHub repository.
  132. * @param integer $milestoneId The id of the comment to update.
  133. * @param integer $title Optional title of the milestone.
  134. * @param string $state Can be open (default) or closed.
  135. * @param string $description Optional description for milestone.
  136. * @param string $due_on Optional ISO 8601 time.
  137. *
  138. * @throws \DomainException
  139. * @return object
  140. *
  141. * @since 1.0
  142. */
  143. public function edit($user, $repo, $milestoneId, $title = null, $state = null, $description = null, $due_on = null)
  144. {
  145. // Build the request path.
  146. $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId;
  147. // Build the request data.
  148. $data = array();
  149. if (!is_null($title))
  150. {
  151. $data['title'] = $title;
  152. }
  153. if (!is_null($state))
  154. {
  155. $data['state'] = $state;
  156. }
  157. if (!is_null($description))
  158. {
  159. $data['description'] = $description;
  160. }
  161. if (!is_null($due_on))
  162. {
  163. $data['due_on'] = $due_on;
  164. }
  165. $data = json_encode($data);
  166. // Send the request.
  167. $response = $this->client->patch($this->fetchUrl($path), $data);
  168. // Validate the response code.
  169. if ($response->code != 200)
  170. {
  171. // Decode the error response and throw an exception.
  172. $error = json_decode($response->body);
  173. throw new \DomainException($error->message, $response->code);
  174. }
  175. return json_decode($response->body);
  176. }
  177. /**
  178. * Method to delete a milestone.
  179. *
  180. * @param string $user The name of the owner of the GitHub repository.
  181. * @param string $repo The name of the GitHub repository.
  182. * @param integer $milestoneId The id of the milestone to delete.
  183. *
  184. * @throws \DomainException
  185. * @return void
  186. *
  187. * @since 1.0
  188. */
  189. public function delete($user, $repo, $milestoneId)
  190. {
  191. // Build the request path.
  192. $path = '/repos/' . $user . '/' . $repo . '/milestones/' . (int) $milestoneId;
  193. // Send the request.
  194. $response = $this->client->delete($this->fetchUrl($path));
  195. // Validate the response code.
  196. if ($response->code != 204)
  197. {
  198. // Decode the error response and throw an exception.
  199. $error = json_decode($response->body);
  200. throw new \DomainException($error->message, $response->code);
  201. }
  202. }
  203. }