PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/backwpup/sdk/WindowsAzure/ServiceBus/ServiceBusRestProxy.php

https://bitbucket.org/cesarmedrano/cesarmedrano
PHP | 845 lines | 512 code | 82 blank | 251 comment | 22 complexity | 88979ae59e94be823bb8bb26c4b06bd5 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\ServiceBus
  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\ServiceBus;
  24. use WindowsAzure\Common\Internal\ServiceRestProxy;
  25. use WindowsAzure\Common\Internal\Http\HttpCallContext;
  26. use WindowsAzure\Common\Internal\Serialization\XmlSerializer;
  27. use WindowsAzure\Common\Internal\Atom\Content;
  28. use WindowsAzure\Common\Internal\Atom\Entry;
  29. use WindowsAzure\Common\Internal\Atom\Feed;
  30. use WindowsAzure\Common\ServiceException;
  31. use WindowsAzure\ServiceBus\Internal\IServiceBus;
  32. use WindowsAzure\ServiceBus\Models\BrokeredMessage;
  33. use WindowsAzure\ServiceBus\Models\BrokerProperties;
  34. use WindowsAzure\ServiceBus\Models\ListQueuesOptions;
  35. use WindowsAzure\ServiceBus\Models\ListQueuesResult;
  36. use WindowsAzure\ServiceBus\Models\ListSubscriptionsOptions;
  37. use WindowsAzure\ServiceBus\Models\ListSubscriptionsResult;
  38. use WindowsAzure\ServiceBus\Models\ListTopicsOptions;
  39. use WindowsAzure\ServiceBus\Models\ListTopicsResult;
  40. use WindowsAzure\ServiceBus\Models\ListRulesOptions;
  41. use WindowsAzure\ServiceBus\Models\ListRulesResult;
  42. use WindowsAzure\ServiceBus\Models\ListOptions;
  43. use WindowsAzure\ServiceBus\Models\QueueDescription;
  44. use WindowsAzure\ServiceBus\Models\QueueInfo;
  45. use WindowsAzure\ServiceBus\Models\ReceiveMessageOptions;
  46. use WindowsAzure\ServiceBus\Models\RuleDescription;
  47. use WindowsAzure\ServiceBus\Models\RuleInfo;
  48. use WindowsAzure\ServiceBus\Models\SubscriptionDescription;
  49. use WindowsAzure\ServiceBus\Models\SubscriptionInfo;
  50. use WindowsAzure\ServiceBus\Models\TopicDescription;
  51. use WindowsAzure\ServiceBus\Models\TopicInfo;
  52. use WindowsAzure\Common\Internal\Resources;
  53. use WindowsAzure\Common\Internal\Utilities;
  54. use WindowsAzure\Common\Internal\Validate;
  55. /**
  56. * This class constructs HTTP requests and receive HTTP responses
  57. * for Service Bus.
  58. *
  59. * @category Microsoft
  60. * @package WindowsAzure\ServiceBus
  61. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  62. * @copyright 2012 Microsoft Corporation
  63. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  64. * @version Release: @package_version@
  65. * @link https://github.com/WindowsAzure/azure-sdk-for-php
  66. */
  67. class ServiceBusRestProxy extends ServiceRestProxy implements IServiceBus
  68. {
  69. /**
  70. * Creates a ServiceBusRestProxy with specified parameter.
  71. *
  72. * @param IHttpClient $channel The channel to communicate.
  73. * @param string $uri The URI of Service Bus service.
  74. * @param ISerializer $dataSerializer The serializer of the Service Bus.
  75. *
  76. * @return none
  77. */
  78. public function __construct($channel, $uri, $dataSerializer)
  79. {
  80. parent::__construct(
  81. $channel,
  82. $uri,
  83. Resources::EMPTY_STRING,
  84. $dataSerializer
  85. );
  86. }
  87. /**
  88. * Sends a brokered message.
  89. *
  90. * @param type $path The path to send message.
  91. * @param type $brokeredMessage The brokered message.
  92. *
  93. * @return none
  94. */
  95. public function sendMessage($path, $brokeredMessage)
  96. {
  97. $httpCallContext = new HttpCallContext();
  98. $httpCallContext->setMethod(Resources::HTTP_POST);
  99. $httpCallContext->addStatusCode(Resources::STATUS_CREATED);
  100. $httpCallContext->setPath($path);
  101. $contentType = $brokeredMessage->getContentType();
  102. if (!is_null($contentType)) {
  103. $httpCallContext->addHeader(
  104. Resources::CONTENT_TYPE,
  105. $contentType
  106. );
  107. }
  108. $brokerProperties = $brokeredMessage->getBrokerProperties();
  109. if (!is_null($brokerProperties)) {
  110. $httpCallContext->addHeader(
  111. Resources::BROKER_PROPERTIES,
  112. $brokerProperties->toString()
  113. );
  114. }
  115. $customProperties = $brokeredMessage->getProperties();
  116. if (!empty($customProperties)) {
  117. foreach ($customProperties as $key => $value) {
  118. $value = json_encode($value);
  119. $httpCallContext->addHeader($key, $value);
  120. }
  121. }
  122. $httpCallContext->setBody($brokeredMessage->getBody());
  123. $this->sendContext($httpCallContext);
  124. }
  125. /**
  126. * Sends a queue message.
  127. *
  128. * @param string $queueName The name of the queue.
  129. * @param BrokeredMessage $brokeredMessage The brokered message.
  130. *
  131. * @return none
  132. */
  133. public function sendQueueMessage($queueName, $brokeredMessage)
  134. {
  135. $path = sprintf(Resources::SEND_MESSAGE_PATH, $queueName);
  136. $this->sendMessage($path, $brokeredMessage);
  137. }
  138. /**
  139. * Receives a queue message.
  140. *
  141. * @param string $queueName The name of the
  142. * queue.
  143. * @param ReceiveMessageOptions $receiveMessageOptions The options to
  144. * receive the message.
  145. *
  146. * @return BrokeredMessage
  147. */
  148. public function receiveQueueMessage($queueName, $receiveMessageOptions = null)
  149. {
  150. $queueMessagePath = sprintf(Resources::RECEIVE_MESSAGE_PATH, $queueName);
  151. return $this->receiveMessage(
  152. $queueMessagePath,
  153. $receiveMessageOptions
  154. );
  155. }
  156. /**
  157. * Receives a message.
  158. *
  159. * @param string $path The path of the
  160. * message.
  161. * @param ReceivedMessageOptions $receiveMessageOptions The options to
  162. * receive the message.
  163. *
  164. * @return BrokeredMessage
  165. */
  166. public function receiveMessage($path, $receiveMessageOptions = null)
  167. {
  168. if (is_null($receiveMessageOptions)) {
  169. $receiveMessageOptions = new ReceiveMessageOptions();
  170. }
  171. $httpCallContext = new HttpCallContext();
  172. $httpCallContext->setPath($path);
  173. $httpCallContext->addStatusCode(Resources::STATUS_CREATED);
  174. $httpCallContext->addStatusCode(Resources::STATUS_NO_CONTENT);
  175. $httpCallContext->addStatusCode(Resources::STATUS_OK);
  176. $timeout = $receiveMessageOptions->getTimeout();
  177. if (!is_null($timeout)) {
  178. $httpCallContext->addQueryParameter('timeout', $timeout);
  179. }
  180. if ($receiveMessageOptions->getIsReceiveAndDelete()) {
  181. $httpCallContext->setMethod(Resources::HTTP_DELETE);
  182. } else if ($receiveMessageOptions->getIsPeekLock()) {
  183. $httpCallContext->setMethod(Resources::HTTP_POST);
  184. } else {
  185. throw new \InvalidArgumentException(
  186. Resources::INVALID_RECEIVE_MODE_MSG
  187. );
  188. }
  189. $response = $this->sendContext($httpCallContext);
  190. if ($response->getStatus() === Resources::STATUS_NO_CONTENT) {
  191. $brokeredMessage = null;
  192. } else {
  193. $responseHeaders = $response->getHeader();
  194. $brokerProperties = new BrokerProperties();
  195. if (array_key_exists('brokerproperties', $responseHeaders)) {
  196. $brokerProperties = BrokerProperties::create(
  197. $responseHeaders['brokerproperties']
  198. );
  199. }
  200. if (array_key_exists('location', $responseHeaders)) {
  201. $brokerProperties->setLockLocation($responseHeaders['location']);
  202. }
  203. $brokeredMessage = new BrokeredMessage();
  204. $brokeredMessage->setBrokerProperties($brokerProperties);
  205. if (array_key_exists(Resources::CONTENT_TYPE, $responseHeaders)) {
  206. $brokeredMessage->setContentType(
  207. $responseHeaders[Resources::CONTENT_TYPE]
  208. );
  209. }
  210. if (array_key_exists('Date', $responseHeaders)) {
  211. $brokeredMessage->setDate($responseHeaders['Date']);
  212. }
  213. $brokeredMessage->setBody($response->getBody());
  214. foreach (array_keys($responseHeaders) as $headerKey) {
  215. $value = $responseHeaders[$headerKey];
  216. $decodedValue = json_decode($value);
  217. if (is_scalar($decodedValue)) {
  218. $brokeredMessage->setProperty(
  219. $headerKey,
  220. $decodedValue
  221. );
  222. }
  223. }
  224. }
  225. return $brokeredMessage;
  226. }
  227. /**
  228. * Sends a brokered message to a specified topic.
  229. *
  230. * @param string $topicName The name of the topic.
  231. * @param BrokeredMessage $brokeredMessage The brokered message.
  232. *
  233. * @return none
  234. */
  235. public function sendTopicMessage($topicName, $brokeredMessage)
  236. {
  237. $topicMessagePath
  238. = sprintf(Resources::SEND_MESSAGE_PATH, $topicName);
  239. $this->sendMessage($topicMessagePath, $brokeredMessage);
  240. }
  241. /**
  242. * Receives a subscription message.
  243. *
  244. * @param string $topicName The name of the
  245. * topic.
  246. * @param string $subscriptionName The name of the
  247. * subscription.
  248. * @param ReceiveMessageOptions $receiveMessageOptions The options to
  249. * receive the subscription message.
  250. *
  251. * @return BrokeredMessage
  252. */
  253. public function receiveSubscriptionMessage(
  254. $topicName,
  255. $subscriptionName,
  256. $receiveMessageOptions = null
  257. ) {
  258. $messagePath = sprintf(
  259. Resources::RECEIVE_SUBSCRIPTION_MESSAGE_PATH,
  260. $topicName,
  261. $subscriptionName
  262. );
  263. $brokeredMessage = $this->receiveMessage(
  264. $messagePath,
  265. $receiveMessageOptions
  266. );
  267. return $brokeredMessage;
  268. }
  269. /**
  270. * Unlocks a brokered message.
  271. *
  272. * @param BrokeredMessage $brokeredMessage The brokered message.
  273. *
  274. * @return none
  275. */
  276. public function unlockMessage($brokeredMessage)
  277. {
  278. $httpCallContext = new HttpCallContext();
  279. $httpCallContext->setMethod(Resources::HTTP_PUT);
  280. $lockLocation = $brokeredMessage->getLockLocation();
  281. $lockLocationArray = parse_url($lockLocation);
  282. $lockLocationPath = Resources::EMPTY_STRING;
  283. if (array_key_exists(Resources::PHP_URL_PATH, $lockLocationArray)) {
  284. $lockLocationPath = $lockLocationArray[Resources::PHP_URL_PATH];
  285. $lockLocationPath = preg_replace(
  286. '@^\/@',
  287. Resources::EMPTY_STRING,
  288. $lockLocationPath
  289. );
  290. }
  291. $httpCallContext->setPath($lockLocationPath);
  292. $httpCallContext->addStatusCode(Resources::STATUS_OK);
  293. $this->sendContext($httpCallContext);
  294. }
  295. /**
  296. * Deletes a brokered message.
  297. *
  298. * @param BrokeredMessage $brokeredMessage The brokered message.
  299. *
  300. * @return none
  301. */
  302. public function deleteMessage($brokeredMessage)
  303. {
  304. $httpCallContext = new HttpCallContext();
  305. $httpCallContext->setMethod(Resources::HTTP_DELETE);
  306. $lockLocation = $brokeredMessage->getLockLocation();
  307. $lockLocationArray = parse_url($lockLocation);
  308. $lockLocationPath = Resources::EMPTY_STRING;
  309. if (array_key_exists(Resources::PHP_URL_PATH, $lockLocationArray)) {
  310. $lockLocationPath = $lockLocationArray[Resources::PHP_URL_PATH];
  311. $lockLocationPath = preg_replace(
  312. '@^\/@',
  313. Resources::EMPTY_STRING,
  314. $lockLocationPath
  315. );
  316. }
  317. if (empty($lockLocationPath)) {
  318. throw new \InvalidArgumentException(
  319. Resources::MISSING_LOCK_LOCATION_MSG
  320. );
  321. }
  322. $httpCallContext->setPath($lockLocationPath);
  323. $httpCallContext->addStatusCode(Resources::STATUS_OK);
  324. $this->sendContext($httpCallContext);
  325. }
  326. /**
  327. * Creates a queue with a specified queue information.
  328. *
  329. * @param QueueInfo $queueInfo The information of the queue.
  330. *
  331. * @return QueueInfo
  332. */
  333. public function createQueue($queueInfo)
  334. {
  335. $httpCallContext = new HttpCallContext();
  336. $httpCallContext->setMethod(Resources::HTTP_PUT);
  337. $httpCallContext->setPath($queueInfo->getTitle());
  338. $httpCallContext->addHeader(
  339. Resources::CONTENT_TYPE,
  340. Resources::ATOM_ENTRY_CONTENT_TYPE
  341. );
  342. $httpCallContext->addStatusCode(Resources::STATUS_CREATED);
  343. $xmlWriter = new \XMLWriter();
  344. $xmlWriter->openMemory();
  345. $queueInfo->writeXml($xmlWriter);
  346. $body = $xmlWriter->outputMemory();
  347. $httpCallContext->setBody($body);
  348. $response = $this->sendContext($httpCallContext);
  349. $queueInfo = new QueueInfo();
  350. $queueInfo->parseXml($response->getBody());
  351. return $queueInfo;
  352. }
  353. /**
  354. * Deletes a queue.
  355. *
  356. * @param string $queuePath The path of the queue.
  357. *
  358. * @return none
  359. */
  360. public function deleteQueue($queuePath)
  361. {
  362. Validate::isString($queuePath, 'queuePath');
  363. Validate::notNullOrEmpty($queuePath, 'queuePath');
  364. $httpCallContext = new HttpCallContext();
  365. $httpCallContext->setMethod(Resources::HTTP_DELETE);
  366. $httpCallContext->addStatusCode(Resources::STATUS_OK);
  367. $httpCallContext->setPath($queuePath);
  368. $this->sendContext($httpCallContext);
  369. }
  370. /**
  371. * Gets a queue with specified path.
  372. *
  373. * @param string $queuePath The path of the queue.
  374. *
  375. * @return QueueInfo
  376. */
  377. public function getQueue($queuePath)
  378. {
  379. $httpCallContext = new HttpCallContext();
  380. $httpCallContext->setPath($queuePath);
  381. $httpCallContext->setMethod(Resources::HTTP_GET);
  382. $httpCallContext->addStatusCode(Resources::STATUS_OK);
  383. $response = $this->sendContext($httpCallContext);
  384. $queueInfo = new QueueInfo();
  385. $queueInfo->parseXml($response->getBody());
  386. return $queueInfo;
  387. }
  388. /**
  389. * Lists a queue.
  390. *
  391. * @param ListQueuesOptions $listQueuesOptions The options to list the
  392. * queues.
  393. *
  394. * @return ListQueuesResult;
  395. */
  396. public function listQueues($listQueuesOptions = null)
  397. {
  398. $response = $this->_listOptions(
  399. $listQueuesOptions,
  400. Resources::LIST_QUEUES_PATH
  401. );
  402. $listQueuesResult = new ListQueuesResult();
  403. $listQueuesResult->parseXml($response->getBody());
  404. return $listQueuesResult;
  405. }
  406. /**
  407. * The base method of all the list operations.
  408. *
  409. * @param ListOptions $listOptions The options for list operation.
  410. * @param string $path The path of the list operation.
  411. *
  412. * @return none
  413. */
  414. private function _listOptions($listOptions, $path)
  415. {
  416. if (is_null($listOptions)) {
  417. $listOptions = new ListOptions();
  418. }
  419. $httpCallContext = new HttpCallContext();
  420. $httpCallContext->setMethod(Resources::HTTP_GET);
  421. $httpCallContext->setPath($path);
  422. $httpCallContext->addStatusCode(Resources::STATUS_OK);
  423. $top = $listOptions->getTop();
  424. $skip = $listOptions->getSkip();
  425. if (!empty($top)) {
  426. $httpCallContext->addQueryParameter(Resources::QP_TOP, $top);
  427. }
  428. if (!empty($skip)) {
  429. $httpCallContext->addQueryParameter(Resources::QP_SKIP, $skip);
  430. }
  431. return $this->sendContext($httpCallContext);
  432. }
  433. /**
  434. * Creates a topic with specified topic info.
  435. *
  436. * @param TopicInfo $topicInfo The information of the topic.
  437. *
  438. * @return TopicInfo
  439. */
  440. public function createTopic($topicInfo)
  441. {
  442. Validate::notNullOrEmpty($topicInfo, 'topicInfo');
  443. $httpCallContext = new HttpCallContext();
  444. $httpCallContext->setMethod(Resources::HTTP_PUT);
  445. $httpCallContext->setPath($topicInfo->getTitle());
  446. $httpCallContext->addHeader(
  447. Resources::CONTENT_TYPE,
  448. Resources::ATOM_ENTRY_CONTENT_TYPE
  449. );
  450. $httpCallContext->addStatusCode(Resources::STATUS_CREATED);
  451. $topicDescriptionXml = XmlSerializer::objectSerialize(
  452. $topicInfo->getTopicDescription(),
  453. 'TopicDescription'
  454. );
  455. $entry = new Entry();
  456. $content = new Content($topicDescriptionXml);
  457. $content->setType(Resources::XML_CONTENT_TYPE);
  458. $entry->setContent($content);
  459. $entry->setAttribute(
  460. Resources::XMLNS,
  461. Resources::SERVICE_BUS_NAMESPACE
  462. );
  463. $xmlWriter = new \XMLWriter();
  464. $xmlWriter->openMemory();
  465. $entry->writeXml($xmlWriter);
  466. $httpCallContext->setBody($xmlWriter->outputMemory());
  467. $response = $this->sendContext($httpCallContext);
  468. $topicInfo = new TopicInfo();
  469. $topicInfo->parseXml($response->getBody());
  470. return $topicInfo;
  471. }
  472. /**
  473. * Deletes a topic with specified topic path.
  474. *
  475. * @param string $topicPath The path of the topic.
  476. *
  477. * @return none
  478. */
  479. public function deleteTopic($topicPath)
  480. {
  481. $httpCallContext = new HttpCallContext();
  482. $httpCallContext->setMethod(Resources::HTTP_DELETE);
  483. $httpCallContext->setPath($topicPath);
  484. $httpCallContext->addStatusCode(Resources::STATUS_OK);
  485. $this->sendContext($httpCallContext);
  486. }
  487. /**
  488. * Gets a topic.
  489. *
  490. * @param string $topicPath The path of the topic.
  491. *
  492. * @return TopicInfo
  493. */
  494. public function getTopic($topicPath)
  495. {
  496. $httpCallContext = new HttpCallContext();
  497. $httpCallContext->setMethod(Resources::HTTP_GET);
  498. $httpCallContext->setPath($topicPath);
  499. $httpCallContext->addStatusCode(Resources::STATUS_OK);
  500. $response = $this->sendContext($httpCallContext);
  501. $topicInfo = new TopicInfo();
  502. $topicInfo->parseXml($response->getBody());
  503. return $topicInfo;
  504. }
  505. /**
  506. * Lists topics.
  507. *
  508. * @param ListTopicsOptions $listTopicsOptions The options to list
  509. * the topics.
  510. *
  511. * @return ListTopicsResults
  512. */
  513. public function listTopics($listTopicsOptions = null)
  514. {
  515. $response = $this->_listOptions(
  516. $listTopicsOptions,
  517. Resources::LIST_TOPICS_PATH
  518. );
  519. $listTopicsResult = new ListTopicsResult();
  520. $listTopicsResult->parseXml($response->getBody());
  521. return $listTopicsResult;
  522. }
  523. /**
  524. * Creates a subscription with specified topic path and
  525. * subscription info.
  526. *
  527. * @param string $topicPath The path of
  528. * the topic.
  529. * @param SubscriptionInfo $subscriptionInfo The information
  530. * of the subscription.
  531. *
  532. * @return SubscriptionInfo
  533. */
  534. public function createSubscription($topicPath, $subscriptionInfo)
  535. {
  536. $httpCallContext = new HttpCallContext();
  537. $httpCallContext->setMethod(Resources::HTTP_PUT);
  538. $subscriptionPath = sprintf(
  539. Resources::SUBSCRIPTION_PATH,
  540. $topicPath,
  541. $subscriptionInfo->getTitle()
  542. );
  543. $httpCallContext->setPath($subscriptionPath);
  544. $httpCallContext->addHeader(
  545. Resources::CONTENT_TYPE,
  546. Resources::ATOM_ENTRY_CONTENT_TYPE
  547. );
  548. $httpCallContext->addStatusCode(Resources::STATUS_CREATED);
  549. $subscriptionDescriptionXml = XmlSerializer::objectSerialize(
  550. $subscriptionInfo->getSubscriptionDescription(),
  551. 'SubscriptionDescription'
  552. );
  553. $entry = new Entry();
  554. $content = new Content($subscriptionDescriptionXml);
  555. $content->setType(Resources::XML_CONTENT_TYPE);
  556. $entry->setContent($content);
  557. $entry->setAttribute(
  558. Resources::XMLNS,
  559. Resources::SERVICE_BUS_NAMESPACE
  560. );
  561. $xmlWriter = new \XMLWriter();
  562. $xmlWriter->openMemory();
  563. $entry->writeXml($xmlWriter);
  564. $httpCallContext->setBody($xmlWriter->outputMemory());
  565. $response = $this->sendContext($httpCallContext);
  566. $subscriptionInfo = new SubscriptionInfo();
  567. $subscriptionInfo->parseXml($response->getBody());
  568. return $subscriptionInfo;
  569. }
  570. /**
  571. * Deletes a subscription.
  572. *
  573. * @param string $topicPath The path of the topic.
  574. * @param string $subscriptionName The name of the subscription.
  575. *
  576. * @return none
  577. */
  578. public function deleteSubscription($topicPath, $subscriptionName)
  579. {
  580. $httpCallContext = new HttpCallContext();
  581. $httpCallContext->setMethod(Resources::HTTP_DELETE);
  582. $httpCallContext->addStatusCode(Resources::STATUS_OK);
  583. $subscriptionPath = sprintf(
  584. Resources::SUBSCRIPTION_PATH,
  585. $topicPath,
  586. $subscriptionName
  587. );
  588. $httpCallContext->setPath($subscriptionPath);
  589. $this->sendContext($httpCallContext);
  590. }
  591. /**
  592. * Gets a subscription.
  593. *
  594. * @param string $topicPath The path of the topic.
  595. * @param string $subscriptionName The name of the subscription.
  596. *
  597. * @return SubscriptionInfo
  598. */
  599. public function getSubscription($topicPath, $subscriptionName)
  600. {
  601. $httpCallContext = new HttpCallContext();
  602. $httpCallContext->setMethod(Resources::HTTP_GET);
  603. $httpCallContext->addStatusCode(Resources::STATUS_OK);
  604. $subscriptionPath = sprintf(
  605. Resources::SUBSCRIPTION_PATH,
  606. $topicPath,
  607. $subscriptionName
  608. );
  609. $httpCallContext->setPath($subscriptionPath);
  610. $response = $this->sendContext($httpCallContext);
  611. $subscriptionInfo = new SubscriptionInfo();
  612. $subscriptionInfo->parseXml($response->getBody());
  613. return $subscriptionInfo;
  614. }
  615. /**
  616. * Lists subscription.
  617. *
  618. * @param string $topicPath The path of
  619. * the topic.
  620. * @param ListSubscriptionsOptions $listSubscriptionsOptions The options
  621. * to list the subscription.
  622. *
  623. * @return ListSubscriptionsResult
  624. */
  625. public function listSubscriptions(
  626. $topicPath,
  627. $listSubscriptionsOptions = null
  628. ) {
  629. $listSubscriptionsPath = sprintf(
  630. Resources::LIST_SUBSCRIPTIONS_PATH,
  631. $topicPath
  632. );
  633. $response = $this->_listOptions(
  634. $listSubscriptionsOptions,
  635. $listSubscriptionsPath
  636. );
  637. $listSubscriptionsResult = new ListSubscriptionsResult();
  638. $listSubscriptionsResult->parseXml($response->getBody());
  639. return $listSubscriptionsResult;
  640. }
  641. /**
  642. * Creates a rule.
  643. *
  644. * @param string $topicPath The path of the topic.
  645. * @param string $subscriptionName The name of the subscription.
  646. * @param RuleInfo $ruleInfo The information of the rule.
  647. *
  648. * @return RuleInfo
  649. */
  650. public function createRule($topicPath, $subscriptionName, $ruleInfo)
  651. {
  652. $httpCallContext = new HttpCallContext();
  653. $httpCallContext->setMethod(Resources::HTTP_PUT);
  654. $httpCallContext->addStatusCode(Resources::STATUS_CREATED);
  655. $httpCallContext->addHeader(
  656. Resources::CONTENT_TYPE,
  657. Resources::ATOM_ENTRY_CONTENT_TYPE
  658. );
  659. $rulePath = sprintf(
  660. Resources::RULE_PATH,
  661. $topicPath,
  662. $subscriptionName,
  663. $ruleInfo->getTitle()
  664. );
  665. $ruleDescriptionXml = XmlSerializer::objectSerialize(
  666. $ruleInfo->getRuleDescription(),
  667. 'RuleDescription'
  668. );
  669. $entry = new Entry();
  670. $content = new Content($ruleDescriptionXml);
  671. $content->setType(Resources::XML_CONTENT_TYPE);
  672. $entry->setContent($content);
  673. $entry->setAttribute(
  674. Resources::XMLNS,
  675. Resources::SERVICE_BUS_NAMESPACE
  676. );
  677. $xmlWriter = new \XMLWriter();
  678. $xmlWriter->openMemory();
  679. $entry->writeXml($xmlWriter);
  680. $httpCallContext->setBody($xmlWriter->outputMemory());
  681. $httpCallContext->setPath($rulePath);
  682. $response = $this->sendContext($httpCallContext);
  683. $ruleInfo = new ruleInfo();
  684. $ruleInfo->parseXml($response->getBody());
  685. return $ruleInfo;
  686. }
  687. /**
  688. * Deletes a rule.
  689. *
  690. * @param string $topicPath The path of the topic.
  691. * @param string $subscriptionName The name of the subscription.
  692. * @param string $ruleName The name of the rule.
  693. *
  694. * @return none
  695. */
  696. public function deleteRule($topicPath, $subscriptionName, $ruleName)
  697. {
  698. $httpCallContext = new HttpCallContext();
  699. $httpCallContext->addStatusCode(Resources::STATUS_OK);
  700. $httpCallContext->setMethod(Resources::HTTP_DELETE);
  701. $rulePath = sprintf(
  702. Resources::RULE_PATH,
  703. $topicPath,
  704. $subscriptionName,
  705. $ruleName
  706. );
  707. $httpCallContext->setPath($rulePath);
  708. $this->sendContext($httpCallContext);
  709. }
  710. /**
  711. * Gets a rule.
  712. *
  713. * @param string $topicPath The path of the topic.
  714. * @param string $subscriptionName The name of the subscription.
  715. * @param string $ruleName The name of the rule.
  716. *
  717. * @return RuleInfo
  718. */
  719. public function getRule($topicPath, $subscriptionName, $ruleName)
  720. {
  721. $httpCallContext = new HttpCallContext();
  722. $httpCallContext->setMethod(Resources::HTTP_GET);
  723. $httpCallContext->addStatusCode(Resources::STATUS_OK);
  724. $rulePath = sprintf(
  725. Resources::RULE_PATH,
  726. $topicPath,
  727. $subscriptionName,
  728. $ruleName
  729. );
  730. $httpCallContext->setPath($rulePath);
  731. $response = $this->sendContext($httpCallContext);
  732. $ruleInfo = new RuleInfo();
  733. $ruleInfo->parseXml($response->getBody());
  734. return $ruleInfo;
  735. }
  736. /**
  737. * Lists rules.
  738. *
  739. * @param string $topicPath The path of the topic.
  740. * @param string $subscriptionName The name of the subscription.
  741. * @param ListRulesOptions $listRulesOptions The options to list the rules.
  742. *
  743. * @return ListRuleResult
  744. */
  745. public function listRules(
  746. $topicPath,
  747. $subscriptionName,
  748. $listRulesOptions = null
  749. ) {
  750. $listRulesPath = sprintf(
  751. Resources::LIST_RULES_PATH,
  752. $topicPath,
  753. $subscriptionName
  754. );
  755. $response = $this->_listOptions(
  756. $listRulesOptions,
  757. $listRulesPath
  758. );
  759. $listRulesResult = new ListRulesResult();
  760. $listRulesResult->parseXml($response->getBody());
  761. return $listRulesResult;
  762. }
  763. }