PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Plivo/Resources/SubAccount/SubAccountInterface.php

http://github.com/plivo/plivohelper-php
PHP | 213 lines | 132 code | 27 blank | 54 comment | 6 complexity | b0ef8e0792e512227b7b2c73e1ce4e31 MD5 | raw file
  1. <?php
  2. namespace Plivo\Resources\SubAccount;
  3. use Plivo\Exceptions\PlivoValidationException;
  4. use Plivo\Exceptions\PlivoResponseException;
  5. use Plivo\BaseClient;
  6. use Plivo\Resources\ResourceInterface;
  7. use Plivo\Resources\ResponseDelete;
  8. use Plivo\Resources\ResponseUpdate;
  9. use Plivo\Util\ArrayOperations;
  10. /**
  11. * Class SubAccountInterface
  12. * @package Plivo\Resources\SubAccount
  13. */
  14. class SubAccountInterface extends ResourceInterface
  15. {
  16. /**
  17. * SubAccountInterface constructor.
  18. * @param BaseClient $plivoClient
  19. * @param $authId
  20. */
  21. function __construct(BaseClient $plivoClient, $authId)
  22. {
  23. parent::__construct($plivoClient);
  24. $this->pathParams = [
  25. 'authId' => $authId
  26. ];
  27. $this->uri = "Account/".$authId."/Subaccount/";
  28. }
  29. /**
  30. * Create a new subaccount
  31. * @param string $name Name of the subaccount
  32. * @param boolean|null $enabled Specify if the subaccount should be enabled or
  33. * not. Takes a value of True or False. Defaults to False
  34. * @return JSON Output
  35. */
  36. public function create($name, $enabled = null)
  37. {
  38. if (is_null($name)) {
  39. throw new PlivoValidationException(
  40. "Mandatory parameters cannot be null");
  41. }
  42. $data = [
  43. 'name' => $name,
  44. 'enabled' => $enabled
  45. ];
  46. $response = $this->client->update(
  47. $this->uri,
  48. $data
  49. );
  50. $responseContents = $response->getContent();
  51. if(!array_key_exists("error",$responseContents)){
  52. return new SubAccountCreateResponse(
  53. $responseContents['message'],
  54. $responseContents['auth_id'],
  55. $responseContents['auth_token'],
  56. $responseContents['api_id'],
  57. $response->getStatusCode()
  58. );
  59. } else {
  60. throw new PlivoResponseException(
  61. $responseContents['error'],
  62. 0,
  63. null,
  64. $response->getContent(),
  65. $response->getStatusCode()
  66. );
  67. }
  68. }
  69. /**
  70. * Modify a subaccount
  71. *
  72. * @param string $subAuthId The auth ID of the subaccount to modify
  73. * @param string $name Name of the subaccount
  74. * @param bool|null $enabled Specify if the subaccount should be enabled or
  75. * not. Takes a value of True or False.
  76. * @return ResponseUpdate
  77. */
  78. public function update($subAuthId, $name = null, $enabled = null)
  79. {
  80. $data = [
  81. 'name' => $name,
  82. 'enabled' => $enabled
  83. ];
  84. $response = $this->client->update(
  85. $this->uri . $subAuthId . '/',
  86. $data
  87. );
  88. $responseContents = $response->getContent();
  89. if(!array_key_exists("error",$responseContents)){
  90. return new ResponseUpdate(
  91. $responseContents['api_id'],
  92. $responseContents['message'],
  93. $response->getStatusCode()
  94. );
  95. } else {
  96. throw new PlivoResponseException(
  97. $responseContents['error'],
  98. 0,
  99. null,
  100. $response->getContent(),
  101. $response->getStatusCode()
  102. );
  103. }
  104. }
  105. /**
  106. * Delete a subaccount
  107. *
  108. * @param string $subAuthId The auth ID of the subaccount to delete
  109. * @param string|null $cascade Specify if the applications, phone numbers and endpoints
  110. * associated with this subaccount be deleted or not. Takes a value of True or False.
  111. * @return ResponseDelete
  112. */
  113. public function delete($subAuthId, $cascade = null)
  114. {
  115. $data = [
  116. 'cascade' => $cascade
  117. ];
  118. $response = $this->client->delete(
  119. $this->uri . $subAuthId . '/',
  120. $data
  121. );
  122. return new ResponseDelete($response->getStatusCode());
  123. }
  124. /**
  125. * You can call this method to retrieve details of a subaccount like auth_id,
  126. * auth_token, etc. Returns an object representing your Plivo subaccount.
  127. *
  128. * @param string $subAuthId The auth ID of the subaccount to retrieve.
  129. * @return SubAccount
  130. * @throws PlivoValidationException
  131. */
  132. public function get($subAuthId)
  133. {
  134. if (ArrayOperations::checkNull([$subAuthId])) {
  135. throw
  136. new PlivoValidationException(
  137. 'subauth id is mandatory');
  138. }
  139. $response = $this->client->fetch(
  140. $this->uri . $subAuthId . '/',
  141. []
  142. );
  143. return new SubAccount(
  144. $this->client,
  145. $response->getContent(),
  146. $this->pathParams['authId'],
  147. $subAuthId);
  148. }
  149. /**
  150. * You can get details of all subaccounts associated with your main
  151. * Plivo account. We return a list of all subaccounts.
  152. *
  153. * @param integer|null $limit Used to display the number of results per
  154. * page. The maximum number of results that can be fetched is 20.
  155. * @param integer|null $offset Denotes the number of value items by which
  156. * the results should be offset. Eg:- If the result contains a 1000 values
  157. * and limit is set to 10 and offset is set to 705, then values 706 through
  158. * 715 are displayed in the results. This parameter is also used for
  159. * pagination of the results.
  160. * @return SubAccountList
  161. */
  162. protected function getList($limit = null, $offset = null)
  163. {
  164. $params = [
  165. 'limit' => $limit,
  166. 'offset' => $offset
  167. ];
  168. $response = $this->client->fetch(
  169. $this->uri,
  170. $params
  171. );
  172. $subAccounts = [];
  173. foreach ($response->getContent()['objects'] as $subAccount) {
  174. $newSubAccount = new SubAccount(
  175. $this->client,
  176. $subAccount,
  177. $this->pathParams['authId'],
  178. $subAccount['auth_id']);
  179. array_push($subAccounts, $newSubAccount);
  180. }
  181. return new SubAccountList(
  182. $this->client,
  183. $response->getContent()['meta'],
  184. $subAccounts);
  185. }
  186. }