PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Service/WindowsAzure/Storage/Batch.php

https://bitbucket.org/areeves42/openfisma
PHP | 248 lines | 93 code | 30 blank | 125 comment | 12 complexity | 32c8422d4ae09eedf3459c7e989768b9 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_WindowsAzure
  17. * @subpackage Storage
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Service_WindowsAzure_Exception
  24. */
  25. // require_once 'Zend/Service/WindowsAzure/Exception.php';
  26. /**
  27. * @see Zend_Service_WindowsAzure_Storage_BatchStorageAbstract
  28. */
  29. // require_once 'Zend/Service/WindowsAzure/Storage/BatchStorageAbstract.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Service_WindowsAzure
  33. * @subpackage Storage
  34. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Service_WindowsAzure_Storage_Batch
  38. {
  39. /**
  40. * Storage client the batch is defined on
  41. *
  42. * @var Zend_Service_WindowsAzure_Storage_BatchStorageAbstract
  43. */
  44. protected $_storageClient = null;
  45. /**
  46. * For table storage?
  47. *
  48. * @var boolean
  49. */
  50. protected $_forTableStorage = false;
  51. /**
  52. * Base URL
  53. *
  54. * @var string
  55. */
  56. protected $_baseUrl;
  57. /**
  58. * Pending operations
  59. *
  60. * @var unknown_type
  61. */
  62. protected $_operations = array();
  63. /**
  64. * Does the batch contain a single select?
  65. *
  66. * @var boolean
  67. */
  68. protected $_isSingleSelect = false;
  69. /**
  70. * Creates a new Zend_Service_WindowsAzure_Storage_Batch
  71. *
  72. * @param Zend_Service_WindowsAzure_Storage_BatchStorageAbstract $storageClient Storage client the batch is defined on
  73. */
  74. public function __construct(Zend_Service_WindowsAzure_Storage_BatchStorageAbstract $storageClient = null, $baseUrl = '')
  75. {
  76. $this->_storageClient = $storageClient;
  77. $this->_baseUrl = $baseUrl;
  78. $this->_beginBatch();
  79. }
  80. /**
  81. * Get base URL for creating requests
  82. *
  83. * @return string
  84. */
  85. public function getBaseUrl()
  86. {
  87. return $this->_baseUrl;
  88. }
  89. /**
  90. * Starts a new batch operation set
  91. *
  92. * @throws Zend_Service_WindowsAzure_Exception
  93. */
  94. protected function _beginBatch()
  95. {
  96. $this->_storageClient->setCurrentBatch($this);
  97. }
  98. /**
  99. * Cleanup current batch
  100. */
  101. protected function _clean()
  102. {
  103. unset($this->_operations);
  104. $this->_storageClient->setCurrentBatch(null);
  105. $this->_storageClient = null;
  106. unset($this);
  107. }
  108. /**
  109. * Enlist operation in current batch
  110. *
  111. * @param string $path Path
  112. * @param string $queryString Query string
  113. * @param string $httpVerb HTTP verb the request will use
  114. * @param array $headers x-ms headers to add
  115. * @param boolean $forTableStorage Is the request for table storage?
  116. * @param mixed $rawData Optional RAW HTTP data to be sent over the wire
  117. * @throws Zend_Service_WindowsAzure_Exception
  118. */
  119. public function enlistOperation($path = '/', $queryString = '', $httpVerb = Zend_Http_Client::GET, $headers = array(), $forTableStorage = false, $rawData = null)
  120. {
  121. // Set _forTableStorage
  122. if ($forTableStorage) {
  123. $this->_forTableStorage = true;
  124. }
  125. // Set _isSingleSelect
  126. if ($httpVerb == Zend_Http_Client::GET) {
  127. if (count($this->_operations) > 0) {
  128. throw new Zend_Service_WindowsAzure_Exception("Select operations can only be performed in an empty batch transaction.");
  129. }
  130. $this->_isSingleSelect = true;
  131. }
  132. // Clean path
  133. if (strpos($path, '/') !== 0) {
  134. $path = '/' . $path;
  135. }
  136. // Clean headers
  137. if ($headers === null) {
  138. $headers = array();
  139. }
  140. // URL encoding
  141. $path = Zend_Service_WindowsAzure_Storage::urlencode($path);
  142. $queryString = Zend_Service_WindowsAzure_Storage::urlencode($queryString);
  143. // Generate URL
  144. $requestUrl = $this->getBaseUrl() . $path . $queryString;
  145. // Generate $rawData
  146. if ($rawData === null) {
  147. $rawData = '';
  148. }
  149. // Add headers
  150. if ($httpVerb != Zend_Http_Client::GET) {
  151. $headers['Content-ID'] = count($this->_operations) + 1;
  152. if ($httpVerb != Zend_Http_Client::DELETE) {
  153. $headers['Content-Type'] = 'application/atom+xml;type=entry';
  154. }
  155. $headers['Content-Length'] = strlen($rawData);
  156. }
  157. // Generate $operation
  158. $operation = '';
  159. $operation .= $httpVerb . ' ' . $requestUrl . ' HTTP/1.1' . "\n";
  160. foreach ($headers as $key => $value)
  161. {
  162. $operation .= $key . ': ' . $value . "\n";
  163. }
  164. $operation .= "\n";
  165. // Add data
  166. $operation .= $rawData;
  167. // Store operation
  168. $this->_operations[] = $operation;
  169. }
  170. /**
  171. * Commit current batch
  172. *
  173. * @return Zend_Http_Response
  174. * @throws Zend_Service_WindowsAzure_Exception
  175. */
  176. public function commit()
  177. {
  178. // Perform batch
  179. $response = $this->_storageClient->performBatch($this->_operations, $this->_forTableStorage, $this->_isSingleSelect);
  180. // Dispose
  181. $this->_clean();
  182. // Parse response
  183. $errors = null;
  184. preg_match_all('/<message (.*)>(.*)<\/message>/', $response->getBody(), $errors);
  185. // Error?
  186. if (count($errors[2]) > 0) {
  187. throw new Zend_Service_WindowsAzure_Exception('An error has occured while committing a batch: ' . $errors[2][0]);
  188. }
  189. // Return
  190. return $response;
  191. }
  192. /**
  193. * Rollback current batch
  194. */
  195. public function rollback()
  196. {
  197. // Dispose
  198. $this->_clean();
  199. }
  200. /**
  201. * Get operation count
  202. *
  203. * @return integer
  204. */
  205. public function getOperationCount()
  206. {
  207. return count($this->_operations);
  208. }
  209. /**
  210. * Is single select?
  211. *
  212. * @return boolean
  213. */
  214. public function isSingleSelect()
  215. {
  216. return $this->_isSingleSelect;
  217. }
  218. }