PageRenderTime 92ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/backwpup/sdk/WindowsAzure/ServiceRuntime/Internal/Protocol1RuntimeGoalStateClient.php

https://bitbucket.org/cesarmedrano/cesarmedrano
PHP | 225 lines | 85 code | 33 blank | 107 comment | 7 complexity | 742232633be34d8194f94b6c86b3fab6 MD5 | raw file
  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package WindowsAzure\ServiceRuntime\Internal
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/windowsazure/azure-sdk-for-php
  22. */
  23. namespace WindowsAzure\ServiceRuntime\Internal;
  24. /**
  25. * An implementation for the protocol runtime goal state client.
  26. *
  27. * @category Microsoft
  28. * @package WindowsAzure\ServiceRuntime\Internal
  29. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  30. * @copyright 2012 Microsoft Corporation
  31. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  32. * @version Release: @package_version@
  33. * @link https://github.com/windowsazure/azure-sdk-for-php
  34. */
  35. class Protocol1RuntimeGoalStateClient implements IRuntimeGoalStateClient
  36. {
  37. /**
  38. * @var Protocol1RuntimeCurrentStateClient
  39. */
  40. private $_currentStateClient;
  41. /**
  42. * @var IGoalStateDeserializer
  43. */
  44. private $_goalStateDeserializer;
  45. /**
  46. * @var IGoalStateDeserializer
  47. */
  48. private $_roleEnvironmentDeserializer;
  49. /**
  50. * @var IInputChannel
  51. */
  52. private $_inputChannel;
  53. /**
  54. * @var string
  55. */
  56. private $_endpoint;
  57. /**
  58. * @var CurrentGoalState
  59. */
  60. private $_currentGoalState;
  61. /**
  62. * @var RoleEnvironmentData
  63. */
  64. private $_currentEnvironmentData;
  65. /**
  66. * @var bool
  67. */
  68. private $_keepOpen;
  69. /**
  70. * Constructor
  71. *
  72. * @param Protocol1RuntimeCurrentStateClient $currentStateClient The
  73. * current state client.
  74. * @param IGoalStateDeserializer $goalStateDeserializer The
  75. * goal state deserializer.
  76. * @param IRoleEnvironmentDeserializer $roleEnvironmentDeserializer The
  77. * role environment deserializer.
  78. * @param IInputChannel $inputChannel The
  79. * input channel.
  80. */
  81. public function __construct($currentStateClient, $goalStateDeserializer,
  82. $roleEnvironmentDeserializer, $inputChannel
  83. ) {
  84. $this->_currentStateClient = $currentStateClient;
  85. $this->_goalStateDeserializer = $goalStateDeserializer;
  86. $this->_roleEnvironmentDeserializer = $roleEnvironmentDeserializer;
  87. $this->_inputChannel = $inputChannel;
  88. $this->_listeners = array();
  89. $this->_currentGoalState = null;
  90. $this->_currentEnvironmentData = null;
  91. $this->_keepOpen = false;
  92. }
  93. /**
  94. * Gets the current goal state.
  95. *
  96. * @return GoalState
  97. */
  98. public function getCurrentGoalState()
  99. {
  100. $this->_ensureGoalStateRetrieved();
  101. return $this->_currentGoalState;
  102. }
  103. /**
  104. * Gets the role environment data.
  105. *
  106. * @return RoleEnvironmentData
  107. */
  108. public function getRoleEnvironmentData()
  109. {
  110. $this->_ensureGoalStateRetrieved();
  111. if (is_null($this->_currentEnvironmentData)) {
  112. $current = $this->_currentGoalState;
  113. if (is_null($current->getEnvironmentPath())) {
  114. throw new RoleEnvironmentNotAvailableException(
  115. 'No role environment data for the current goal state'
  116. );
  117. }
  118. $environmentStream = $this->_inputChannel->getInputStream(
  119. $current->getEnvironmentPath()
  120. );
  121. $this->_currentEnvironmentData = $this->_roleEnvironmentDeserializer
  122. ->deserialize($environmentStream);
  123. }
  124. return $this->_currentEnvironmentData;
  125. }
  126. /**
  127. * Sets the endpoint.
  128. *
  129. * @param string $endpoint Sets the endpoint.
  130. *
  131. * @return none
  132. */
  133. public function setEndpoint($endpoint)
  134. {
  135. $this->_endpoint = $endpoint;
  136. }
  137. /**
  138. * Gets the endpoint.
  139. *
  140. * @return string
  141. */
  142. public function getEndpoint()
  143. {
  144. return $this->_endpoint;
  145. }
  146. /**
  147. * Sets the keep open state.
  148. *
  149. * @param string $keepOpen Sets the keep open state.
  150. *
  151. * @return none
  152. */
  153. public function setKeepOpen($keepOpen)
  154. {
  155. $this->_keepOpen = $keepOpen;
  156. }
  157. /**
  158. * Gets the keep open state.
  159. *
  160. * @return bool
  161. */
  162. public function getKeepOpen()
  163. {
  164. return $this->_keepOpen;
  165. }
  166. /**
  167. * Ensures that the goal state is retrieved.
  168. *
  169. * @return none
  170. */
  171. private function _ensureGoalStateRetrieved()
  172. {
  173. if (is_null($this->_currentGoalState) || !$this->_keepOpen) {
  174. $inputStream = $this->_inputChannel->getInputStream($this->_endpoint);
  175. $this->_goalStateDeserializer->initialize($inputStream);
  176. }
  177. $goalState = $this->_goalStateDeserializer->deserialize();
  178. if (is_null($goalState)) {
  179. return;
  180. }
  181. $this->_currentGoalState = $goalState;
  182. if (!is_null($goalState->getEnvironmentPath())) {
  183. $this->_currentEnvironmentData = null;
  184. }
  185. $this->_currentStateClient->setEndpoint(
  186. $this->_currentGoalState->getCurrentStateEndpoint()
  187. );
  188. if (!$this->_keepOpen) {
  189. $this->_inputChannel->closeInputStream();
  190. }
  191. }
  192. }