/wp-content/plugins/backwpup/sdk/OpenCloud/database.php

https://bitbucket.org/cesarmedrano/cesarmedrano · PHP · 176 lines · 77 code · 19 blank · 80 comment · 10 complexity · ab65b2fc5ffad2f10f09c303866acd52 MD5 · raw file

  1. <?php
  2. /**
  3. * A database in the Cloud Databases service
  4. *
  5. * @copyright 2012-2013 Rackspace Hosting, Inc.
  6. * See COPYING for licensing information
  7. *
  8. * @package phpOpenCloud
  9. * @version 1.0
  10. * @author Glen Campbell <glen.campbell@rackspace.com>
  11. */
  12. namespace OpenCloud\DbService;
  13. /**
  14. * This class represents a Database in the Rackspace "Red Dwarf"
  15. * database-as-a-service product.
  16. *
  17. * @author Glen Campbell <glen.campbell@rackspace.com>
  18. */
  19. class Database extends \OpenCloud\Base {
  20. public
  21. $name;
  22. private
  23. $_instance;
  24. /**
  25. * Creates a new database object
  26. *
  27. * Unlike other objects (Servers, DataObjects, etc.), passing a database
  28. * name to the constructor does *not* pull information from the database.
  29. * For example, if you pass an ID to the `Server()` constructor, it will
  30. * attempt to retrieve the information on that server from the service,
  31. * and will return an error if it is not found. However, the Cloud
  32. * Databases service does not permit retrieval of information on
  33. * individual databases (only via Collection), and thus passing in a
  34. * name via the `$info` parameter only creates an in-memory object that
  35. * is not necessarily tied to an actual database.
  36. *
  37. * @param Instance $instance the parent DbService\Instance of the database
  38. * @param mixed $info if an array or object, treated as properties to set;
  39. * if a string, treated as the database name
  40. * @return void
  41. * @throws DatabaseNameError if `$info` is not a string, object, or array
  42. */
  43. public function __construct(Instance $instance, $info=NULL) {
  44. $this->_instance = $instance;
  45. if (is_object($info) || is_array($info))
  46. foreach($info as $property => $value)
  47. $this->$property = $value;
  48. elseif (is_string($info))
  49. $this->name = $info;
  50. elseif (isset($info))
  51. throw new DatabaseNameError(
  52. _('Database parameter must be an object, array, or string'));
  53. }
  54. /**
  55. * Returns the Url of the Database
  56. *
  57. * @api
  58. * @return string
  59. */
  60. public function Url() {
  61. if (!isset($this->name))
  62. throw new DatabaseNameError(
  63. _('The database does not have a Url yet'));
  64. return stripslashes($this->Instance()->Url('databases'))
  65. . '/' .$this->name;
  66. }
  67. /**
  68. * Returns the Instance of the database
  69. *
  70. * @return Instance
  71. */
  72. public function Instance() {
  73. return $this->_instance;
  74. }
  75. /**
  76. * Returns the related service
  77. *
  78. * @return \OpenCloud\DbService
  79. */
  80. public function Service() {
  81. return $this->Instance()->Service();
  82. }
  83. /**
  84. * Creates a new database
  85. *
  86. * @api
  87. * @param array $params array of attributes to set prior to Create
  88. * @return \OpenCloud\HttpResponse
  89. */
  90. public function Create($params=array()) {
  91. // target the /databases subresource
  92. $url = $this->Instance()->Url('databases');
  93. if (isset($params['name']))
  94. $this->name = $params['name'];
  95. $json = json_encode($this->CreateJson($params));
  96. if ($this->CheckJsonError())
  97. return FALSE;
  98. // POST it off
  99. $response = $this->Service()->Request(
  100. $url,
  101. 'POST',
  102. array(),
  103. $json
  104. );
  105. // check the response code
  106. if ($response->HttpStatus() != 202)
  107. throw new DatabaseCreateError(sprintf(
  108. _('Error creating database [%s], status [%d] response [%s]'),
  109. $this->name, $response->HttpStatus(), $response->HttpBody()));
  110. // refresh and return
  111. return $response;
  112. }
  113. /**
  114. * Updates an existing database
  115. *
  116. * @param array $params ignored
  117. * @throws DatabaseUpdateError always; updates are not permitted
  118. * @return void
  119. */
  120. public function Update($params=array()) {
  121. throw new DatabaseUpdateError(
  122. _('Updates are not currently permitted on Database objects'));
  123. }
  124. /**
  125. * Deletes a database
  126. *
  127. * @api
  128. * @return \OpenCloud\HttpResponseb
  129. */
  130. public function Delete() {
  131. $resp = $this->Service()->Request($this->Url(), 'DELETE');
  132. if ($resp->HttpStatus() != 202)
  133. throw new DatabaseDeleteError(sprintf(
  134. _('Error deleting database [%s], status [%d] response [%s]'),
  135. $this->name,
  136. $resp->HttpStatus(),
  137. $resp->HttpBody()));
  138. return $resp;
  139. }
  140. /********** PRIVATE METHODS **********/
  141. /**
  142. * Returns the JSON object for creating the database
  143. */
  144. private function CreateJson($params=array()) {
  145. $obj = new \stdClass();
  146. $obj->databases = array();
  147. $obj->databases[0] = new \stdClass();
  148. // set the name
  149. if (!isset($this->name))
  150. throw new DatabaseNameError(
  151. _('Database name is required'));
  152. $obj->databases[0]->name = $this->name;
  153. foreach($params as $key => $value)
  154. $obj->databases[0]->$key = $value;
  155. return $obj;
  156. }
  157. } // class Database