/application/models/mappers/Person/GroupMembership.php

https://github.com/sizzlelab/NairobiSizzle · PHP · 321 lines · 118 code · 11 blank · 192 comment · 22 complexity · fe316a8383fa5609b4927b9ec7ebdef8 MD5 · raw file

  1. <?php
  2. /**
  3. * Handles requests to /people/<user id>/@groups/<group id> module of the ASI platform.
  4. *
  5. * @author Joel Mukuthu <joelmukuthu@gmail.com>
  6. * @copyright 2010, Nairobi Sizzle
  7. * @category NairobiSizzle
  8. * @package Core
  9. * @subpackage Models
  10. */
  11. class Application_Model_Mapper_Person_GroupMembership extends Application_Model_Mapper_Person_Abstract {
  12. /**
  13. * Stores an instance of {@link Application_Model_Person_GroupMembership} to
  14. * work with.
  15. *
  16. * @var Application_Model_Person_GroupMembership|null
  17. */
  18. protected $groupMembership = null;
  19. /**
  20. * Class constructor.
  21. *
  22. * @param Application_Model_Person_GroupMembership $groupMembership Group membership
  23. * object to work with.
  24. */
  25. public function __construct(Application_Model_Person_GroupMembership $groupMembership = null) {
  26. if ($groupMembership instanceof Application_Model_Person_GroupMembership) {
  27. $this->setGroupMembership($groupMembership);
  28. }
  29. }
  30. /**
  31. * Set the instance of {@link Application_Model_Person_GroupMembership} to
  32. * work with.
  33. *
  34. * @param Application_Model_Person_GroupMembership $groupMembership
  35. *
  36. * @return Application_Model_Mapper_Person_GroupMembership
  37. */
  38. public function setGroupMembership(Application_Model_Person_GroupMembership $groupMembership) {
  39. $this->groupMembership = $groupMembership;
  40. return $this;
  41. }
  42. /**
  43. * Get the instance of {@link Application_Model_Person_GroupMembership} to
  44. * work with.
  45. *
  46. * @see Application_Model_Mapper_Person_GroupMembership::setGroupMembership()
  47. *
  48. * @return Application_Model_Person_GroupMembership
  49. */
  50. public function getGroupMembership() {
  51. if (!$this->groupMembership) {
  52. $this->setGroupMembership(new Application_Model_Person_GroupMembership());
  53. }
  54. return $this->groupMembership;
  55. }
  56. /**
  57. * Fetches a person's group membership status from ASI. Sends a GET request
  58. * to /people/<user id>/@groups/<group id>.
  59. *
  60. * @param string $userId The person's ID. If not provided, this method will call
  61. * {@link Application_Model_Mapper_Person_Abstract::getPerson()} and then
  62. * {@link Application_Model_Person::getId()} to get the person's ID.
  63. *
  64. * @param string $groupId The group's ID. If not provided, this method will call
  65. * {@link Application_Model_Mapper_Person_GroupMembership::getGroupMembership()}
  66. * and then {@link Application_Model_Person_GroupMembership::getGroupId()}
  67. * to get the group's ID.
  68. *
  69. * @return array Of {@link Application_Model_Person_GroupMembership}s with
  70. * their data set.
  71. *
  72. * @throws Application_Model_Mapper_Person_GroupMembership_Exception If:
  73. * - Person's ID has not been provided (and could not be obtained from a
  74. * {@link Application_Model_Person} object)
  75. * - Group's ID has not been provided (and could not be obtained from a
  76. * {@link Application_Model_Person_GroupMembership} object)
  77. * - The request was not successful.
  78. * - The reques was successful but for some unknown reason data was not
  79. * received.
  80. */
  81. public function fetch($userId = null, $groupId = null) {
  82. $userId = $userId ? (string) $userId : $this->getPerson()->getId();
  83. if (!$userId) {
  84. throw new Application_Model_Exception('Person ID has not been set');
  85. }
  86. $groupMembership = $this->getGroupMembership();
  87. $groupId = $groupId ? (string) $groupId : $groupMembership->getGroupId();
  88. if (!$groupId) {
  89. throw new Application_Model_Exception('Group ID has not been set');
  90. }
  91. $client = $this->getClient();
  92. if ($client->sendRequest("/people/{$userId}/@groups/{$groupId}", 'get')->isSuccessful()) {
  93. $response = $client->getResponseBody();
  94. if (!isset($response['entry'])) {
  95. throw new Application_Model_Mapper_Person_GroupMembership_Exception('Unexpected error: data not recevied');
  96. }
  97. return $groupMembership->setData($response['entry']);
  98. } else {
  99. throw new Application_Model_Mapper_Person_GroupMembership_Exception("Error fetching location security token: " . $client->getResponseMessage(), $client->getResponseCode());
  100. }
  101. }
  102. /**
  103. * Creates a new group membership for a person. In essence this is sending an
  104. * invite/request to join a group. Sends a POST request to /people/<user id>/@groups.
  105. *
  106. * @param string $userId The person's ID. If not provided, this method will call
  107. * {@link Application_Model_Mapper_Person_Abstract::getPerson()} and then
  108. * {@link Application_Model_Person::getId()} to get the person's ID.
  109. *
  110. * @param string $groupId The group's ID. If not provided, this method will call
  111. * {@link Application_Model_Mapper_Person_GroupMembership::getGroupMembership()}
  112. * and then {@link Application_Model_Person_GroupMembership::getGroupId()}
  113. * to get the group's ID.
  114. *
  115. * @return true If the request was completed successfully.
  116. *
  117. * @todo How to handle the various possibilities of outcomes involved with
  118. * this request e.g. if request completes successfully, a 201 or 202 response
  119. * code is returned, meaning different things.
  120. *
  121. * @throws Application_Model_Mapper_Person_GroupMembership_Exception If:
  122. * - Person's ID has not been provided (and could not be obtained from a
  123. * {@link Application_Model_Person} object)
  124. * - Group's ID has not been provided (and could not be obtained from a
  125. * {@link Application_Model_Person_GroupMembership} object)
  126. * - The request was not successful.
  127. * - The reques was successful but for some unknown reason data was not
  128. * received.
  129. */
  130. public function create($userId = null, $groupId = null) {
  131. $userId = $userId ? (string) $userId : $this->getPerson()->getId();
  132. if (!$userId) {
  133. throw new Application_Model_Person_GroupMembership_Mapper_Exception('Person ID has not been set');
  134. }
  135. $groupId = $groupId ? (string) $groupId : $this->getGroupMembership()->getGroupId();
  136. if (!$groupId) {
  137. throw new Application_Model_Mapper_Person_GroupMembership_Exception('Group ID has not been set');
  138. }
  139. $client = $this->getClient();
  140. if ($client->sendRequest("/people/{$userId}/@groups", 'post', "group_id={$groupId}")->isSuccessful()) {
  141. return true;
  142. } else {
  143. $response = $client->getResponseBody();
  144. if (isset($response['messages'])) {
  145. $this->setErrors($response['messages']);
  146. }
  147. throw new Application_Model_Mapper_Person_GroupMembership_Exception('Could not create person\'s group invitation: ' . $client->getResponseMessage(), $client->getResponseCode());
  148. }
  149. }
  150. /**
  151. * Updates a person's group membership status. Sends a POST request to
  152. * /people/<user id>/@groups/<group id>.
  153. *
  154. * @param bool $acceptRequest Whether to accept a group invitation or not. To
  155. * force the mapper to ignore this parameter when sending the request, provide
  156. * a 'null' value.
  157. *
  158. * @param bool $adminStatus Whether to make this person a group admin or not.
  159. * To force the mapper to ignore this parameter when sending the request, provide
  160. * a 'null' value.
  161. *
  162. * @param string $userId The person's ID. If not provided, this method will call
  163. * {@link Application_Model_Mapper_Person_Abstract::getPerson()} and then
  164. * {@link Application_Model_Person::getId()} to get the person's ID.
  165. *
  166. * @param string $groupId The group's ID. If not provided, this method will call
  167. * {@link Application_Model_Mapper_Person_GroupMembership::getGroupMembership()}
  168. * and then {@link Application_Model_Person_GroupMembership::getGroupId()}
  169. * to get the group's ID.
  170. *
  171. * @return true If the request was completed successfully.
  172. *
  173. * @throws Application_Model_Mapper_Person_GroupMembership_Exception If:
  174. * - Person's ID has not been provided (and could not be obtained from a
  175. * {@link Application_Model_Person} object)
  176. * - Group's ID has not been provided (and could not be obtained from a
  177. * {@link Application_Model_Person_GroupMembership} object)
  178. * - The request was not successful. In this case if any error messages
  179. * were returned by ASI, they will be available using
  180. * {@link Application_Model_Mapper_Abstract::getErrors()}.
  181. */
  182. public function update($acceptRequest = null, $adminStatus = null, $userId = null, $groupId = null) {
  183. $groupMembership = $this->getGroupMembership();
  184. $userId = $userId ? (string) $userId : $this->getPerson()->getId();
  185. if (!$userId) {
  186. throw new Application_Model_Person_GroupMembership_Mapper_Exception('Person ID has not been set');
  187. }
  188. $groupId = $groupId ? (string) $groupId : $groupMembership->getId();
  189. if (!$groupId) {
  190. throw new Application_Model_Person_GroupMembership_Mapper_Exception('Group ID has not been set');
  191. }
  192. $acceptRequest = !is_null($acceptRequest) ? $acceptRequest : null;
  193. $adminStatus = !is_null($adminStatus) ? $adminStatus : null;
  194. $data = '';
  195. $data = $acceptRequest ? "accept_request={$acceptRequest}" : '';
  196. $data .= $adminStatus ? $data ? "&admin_status={$adminStatus}" : "admin_status={$adminStatus}" : '';
  197. $client = $this->getClient();
  198. if ($client->sendRequest("/people/{$userId}/@groups/{$groupId}", 'put', $data)->isSuccessful()) {
  199. return true;
  200. } else {
  201. $response = $client->getResponseBody();
  202. if (isset($response['messages'])) {
  203. $this->setErrors($response['messages']);
  204. }
  205. throw new Application_Model_Mapper_Person_GroupMembership_Exception('Could not update person\'s group membership: ' . $client->getResponseMessage(), $client->getResponseCode());
  206. }
  207. }
  208. /**
  209. * Removes a person from a group. Sends a DELETE request to
  210. * /people/<user id>/@groups/<group id>.
  211. *
  212. * @param string $userId The person's ID. If not provided, this method will call
  213. * {@link Application_Model_Mapper_Person_Abstract::getPerson()} and then
  214. * {@link Application_Model_Person::getId()} to get the person's ID.
  215. *
  216. * @param string $groupId The group's ID. If not provided, this method will call
  217. * {@link Application_Model_Mapper_Person_GroupMembership::getGroupMembership()}
  218. * and then {@link Application_Model_Person_GroupMembership::getGroupId()}
  219. * to get the group's ID.
  220. *
  221. * @return true If the request was completed successfully.
  222. *
  223. * @throws Application_Model_Mapper_Person_GroupMembership_Exception If:
  224. * - Person's ID has not been provided (and could not be obtained from a
  225. * {@link Application_Model_Person} object)
  226. * - Group's ID has not been provided (and could not be obtained from a
  227. * {@link Application_Model_Person_GroupMembership} object)
  228. * - The request was not successful. In this case if any error messages
  229. * were returned by ASI, they will be available using
  230. * {@link Application_Model_Mapper_Abstract::getErrors()}.
  231. */
  232. public function delete($userId = null, $groupId = null) {
  233. $userId = $userId ? (string) $userId : $this->getPerson()->getId();
  234. if (!$userId) {
  235. throw new Application_Model_Mapper_Person_GroupMembership_Exception('Person ID has not been set');
  236. }
  237. $groupId = $groupId ? (string) $groupId : $this->getGroupMembership()->getId();
  238. if (!$groupId) {
  239. throw new Application_Model_Mapper_Person_GroupMembership_Exception('Group ID has not been set');
  240. }
  241. $client = $this->getClient();
  242. if ($client->sendRequest("/people/{$userId}/@groups/{$groupId}", 'delete')->isSuccessful()) {
  243. return true;
  244. } else {
  245. $response = $client->getResponseBody();
  246. if (isset($response['messages'])) {
  247. $this->setErrors($response['messages']);
  248. }
  249. throw new Application_Model_Mapper_Person_GroupMembership_Exception('Could not delete person\'s group membership: ' . $client->getResponseMessage(), $client->getResponseCode());
  250. }
  251. }
  252. /**
  253. * Accepts a group invitation. This method passes the request along to
  254. * {@link Application_Model_Mapper_Person_GroupMembership::update()}. As such,
  255. * if you wish to accept a request and also set a person as admin, use
  256. * {@link Application_Model_Mapper_Person_GroupMembership::update()} instead
  257. * to avoid making two requests.
  258. *
  259. * @param string $userId
  260. *
  261. * @param string $groupId
  262. *
  263. * @return true If the request is successful.
  264. */
  265. public function accept($userId = null, $groupId = null) {
  266. return $this->update(true, null, $userId, $groupId);
  267. }
  268. /**
  269. * Declines a group invitation. This method passes the request along to
  270. * {@link Application_Model_Mapper_Person_GroupMembership::update()}.
  271. *
  272. * @param string $userId
  273. *
  274. * @param string $groupId
  275. *
  276. * @return true If the request is successful.
  277. */
  278. public function decline($userId = null, $groupId = null) {
  279. return $this->update(false, null, $userId, $groupId);
  280. }
  281. /**
  282. * Sets a person as a group's admin. This method passes the request along to
  283. * {@link Application_Model_Mapper_Person_GroupMembership::update()}. As such,
  284. * if you wish to accept a request and also set a person as admin, use
  285. * {@link Application_Model_Mapper_Person_GroupMembership::update()} instead
  286. * to avoid making two requests.
  287. *
  288. * @param string $userId
  289. *
  290. * @param string $groupId
  291. *
  292. * @return true If the request is successful.
  293. */
  294. public function setAdmin($userId = null, $groupId = null) {
  295. return $this->update(null, true, $userId, $groupId);
  296. }
  297. /**
  298. * Unsets a person as a group's admin. This method passes the request along to
  299. * {@link Application_Model_Mapper_Person_GroupMembership::update()}.
  300. *
  301. * @param string $userId
  302. *
  303. * @param string $groupId
  304. *
  305. * @return true If the request is successful.
  306. */
  307. public function unsetAdmin($userId = null, $groupId = null) {
  308. return $this->update(null, false, $userId, $groupId);
  309. }
  310. }