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

/src/TrafficManagement.php

https://gitlab.com/Blueprint-Marketing/dyn-php
PHP | 271 lines | 141 code | 42 blank | 88 comment | 22 complexity | 934c155a37b59e5451fe7a18634b1441 MD5 | raw file
  1. <?php
  2. namespace Dyn;
  3. use Dyn\TrafficManagement\Api\Client as ApiClient;
  4. use Dyn\TrafficManagement\Zone;
  5. use Zend\Http\Client as HttpClient;
  6. class TrafficManagement
  7. {
  8. /**
  9. * The API client instance, used for all API communication
  10. *
  11. * @var ApiClient
  12. */
  13. protected $apiClient;
  14. /**
  15. * Customer name (for login)
  16. *
  17. * @var string
  18. */
  19. protected $customerName;
  20. /**
  21. * Username (for login)
  22. *
  23. * @var string
  24. */
  25. protected $username;
  26. /**
  27. * Password (for login)
  28. *
  29. * @var string
  30. */
  31. protected $password;
  32. /**
  33. * The Zend HTTP Client instance or configuration
  34. *
  35. * @var array|Zend\Http\Client
  36. */
  37. protected $httpClient;
  38. /**
  39. * @param string $customerName
  40. * @param string $username
  41. * @param string $password
  42. * @param array|Zend\Http\Client $httpClient
  43. */
  44. public function __construct($customerName, $username, $password, $httpClient = null)
  45. {
  46. $this->customerName = $customerName;
  47. $this->username = $username;
  48. $this->password = $password;
  49. if ($httpClient) {
  50. if (!is_array($httpClient) && !($httpClient instanceof HttpClient)) {
  51. throw new \RuntimeException('Invalid Http client parameter supplied');
  52. }
  53. $this->httpClient = $httpClient;
  54. }
  55. }
  56. /**
  57. * Setter for API client
  58. *
  59. * @param ApiClient $apiClient
  60. */
  61. public function setApiClient(ApiClient $apiClient)
  62. {
  63. $this->apiClient = $apiClient;
  64. return $this;
  65. }
  66. /**
  67. * Returns an instance of the API client, creating it if required
  68. *
  69. * If a custom instance of the Zend Http Client was supplied to this class'
  70. * constructor it will be used. This allows for custom functionality (such as
  71. * working through a HTTP proxy) if needed.
  72. *
  73. * @return ApiClient
  74. */
  75. public function getApiClient()
  76. {
  77. if (!$this->apiClient) {
  78. $this->apiClient = new ApiClient($this->httpClient);
  79. }
  80. return $this->apiClient;
  81. }
  82. /**
  83. * Creates an API session using the existing credentials
  84. *
  85. * If successful, the API token is stored in the API client for use in future requests
  86. *
  87. * @return boolean
  88. */
  89. public function createSession()
  90. {
  91. $result = $this->getApiClient()->post(
  92. '/Session/',
  93. array(
  94. 'customer_name' => $this->customerName,
  95. 'user_name' => $this->username,
  96. 'password' => $this->password
  97. )
  98. );
  99. if ($result && $result->isComplete()) {
  100. $token = $result->data->token;
  101. $this->getApiClient()->setToken($token);
  102. return true;
  103. }
  104. return false;
  105. }
  106. /**
  107. * Refresh the current session token, extending its expiry
  108. *
  109. * @return boolean
  110. */
  111. public function refreshSession()
  112. {
  113. $result = $this->getApiClient()->put(
  114. '/Session/',
  115. array(
  116. 'token' => $this->getApiClient()->getToken()
  117. )
  118. );
  119. return ($result && $result->isComplete());
  120. }
  121. /**
  122. * Removes the API session and clears the token from the API client
  123. *
  124. * @return boolean
  125. */
  126. public function deleteSession()
  127. {
  128. $result = $this->getApiClient()->delete('/Session/');
  129. if ($result && $result->isComplete()) {
  130. $this->getApiClient()->clearToken();
  131. return true;
  132. }
  133. return false;
  134. }
  135. /**
  136. * Returns the specified zone, or false if it doesn't exist.
  137. *
  138. * @param string $zoneName e.g. 'example.com'
  139. * @return Zone|false
  140. */
  141. public function getZone($zoneName)
  142. {
  143. $apiClient = $this->getApiClient();
  144. $result = $apiClient->get('/Zone/'.$zoneName);
  145. if ($result && $result->isComplete()) {
  146. $zone = new Zone($apiClient);
  147. $zone->setName($result->data->zone)
  148. ->setType($result->data->zone_type)
  149. ->setSerialStyle($result->data->serial_style)
  150. ->setSerial($result->data->serial);
  151. return $zone;
  152. }
  153. return false;
  154. }
  155. /**
  156. * Returns an array of all zones from the account
  157. *
  158. * @return array|false
  159. */
  160. public function getZones()
  161. {
  162. $apiClient = $this->getApiClient();
  163. $result = $apiClient->get('/Zone/', array('detail' => 'y'));
  164. if ($result && $result->isComplete()) {
  165. $zones = array();
  166. if (count($result->data) > 0) {
  167. foreach ($result->data as $zoneData) {
  168. $zone = new Zone($apiClient);
  169. $zone->setName($zoneData->zone)
  170. ->setType($zoneData->zone_type)
  171. ->setSerialStyle($zoneData->serial_style)
  172. ->setSerial($zoneData->serial);
  173. $zones[] = $zone;
  174. }
  175. }
  176. return $zones;
  177. }
  178. return false;
  179. }
  180. /**
  181. * Create a new zone
  182. *
  183. * @param string $zoneName E.g. example.com
  184. * @param string $rname Email address contact for the zone
  185. * @param integer $defaultTtl Default TTL (in seconds)
  186. * @param string $serialStyle
  187. * @return Zone|false|Dyn\TrafficManagement\Api\Response
  188. */
  189. public function createZone($zoneName, $rname, $defaultTtl, $serialStyle = 'increment')
  190. {
  191. $zone = new Zone($this->getApiClient());
  192. $zone->setName($zoneName)
  193. ->setRname($rname)
  194. ->setDefaultTtl($defaultTtl)
  195. ->setSerialStyle($serialStyle);
  196. $params = array(
  197. 'rname' => $rname,
  198. 'ttl' => $defaultTtl,
  199. 'serial_style' => $serialStyle
  200. );
  201. $result = $this->apiClient->post('/Zone/'.$zoneName, $params);
  202. if ($result && $result->isOk()) {
  203. if ($result->isComplete()) {
  204. return $zone;
  205. } else {
  206. return $result;
  207. }
  208. }
  209. return false;
  210. }
  211. /**
  212. * Delete the specified zone
  213. *
  214. * @param Zone $zone
  215. * @return boolean|Dyn\TrafficManagement\Api\Response
  216. */
  217. public function deleteZone($zone)
  218. {
  219. $result = $this->apiClient->delete('/Zone/'.$zone->getName());
  220. if ($result && $result->isOk()) {
  221. if ($result->isComplete()) {
  222. return true;
  223. } else {
  224. return $result;
  225. }
  226. }
  227. return false;
  228. }
  229. }