No Description

ListTopicResponse.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace AliyunMNS\Responses;
  3. use AliyunMNS\Exception\MnsException;
  4. use AliyunMNS\Responses\BaseResponse;
  5. use AliyunMNS\Common\XMLParser;
  6. class ListTopicResponse extends BaseResponse
  7. {
  8. private $topicNames;
  9. private $nextMarker;
  10. public function __construct()
  11. {
  12. $this->topicNames = array();
  13. $this->nextMarker = NULL;
  14. }
  15. public function isFinished()
  16. {
  17. return $this->nextMarker == NULL;
  18. }
  19. public function getTopicNames()
  20. {
  21. return $this->topicNames;
  22. }
  23. public function getNextMarker()
  24. {
  25. return $this->nextMarker;
  26. }
  27. public function parseResponse($statusCode, $content)
  28. {
  29. $this->statusCode = $statusCode;
  30. if ($statusCode != 200) {
  31. $this->parseErrorResponse($statusCode, $content);
  32. return;
  33. }
  34. $this->succeed = TRUE;
  35. $xmlReader = $this->loadXmlContent($content);
  36. try
  37. {
  38. while ($xmlReader->read())
  39. {
  40. if ($xmlReader->nodeType == \XMLReader::ELEMENT)
  41. {
  42. switch ($xmlReader->name) {
  43. case 'TopicURL':
  44. $xmlReader->read();
  45. if ($xmlReader->nodeType == \XMLReader::TEXT)
  46. {
  47. $topicName = $this->getTopicNameFromTopicURL($xmlReader->value);
  48. $this->topicNames[] = $topicName;
  49. }
  50. break;
  51. case 'NextMarker':
  52. $xmlReader->read();
  53. if ($xmlReader->nodeType == \XMLReader::TEXT)
  54. {
  55. $this->nextMarker = $xmlReader->value;
  56. }
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. catch (\Exception $e)
  63. {
  64. throw new MnsException($statusCode, $e->getMessage(), $e);
  65. }
  66. catch (\Throwable $t)
  67. {
  68. throw new MnsException($statusCode, $t->getMessage());
  69. }
  70. }
  71. private function getTopicNameFromTopicURL($topicURL)
  72. {
  73. $pieces = explode("/", $topicURL);
  74. if (count($pieces) == 5)
  75. {
  76. return $pieces[4];
  77. }
  78. return "";
  79. }
  80. public function parseErrorResponse($statusCode, $content, MnsException $exception = NULL)
  81. {
  82. $this->succeed = FALSE;
  83. $xmlReader = $this->loadXmlContent($content);
  84. try
  85. {
  86. $result = XMLParser::parseNormalError($xmlReader);
  87. throw new MnsException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
  88. }
  89. catch (\Exception $e)
  90. {
  91. if ($exception != NULL)
  92. {
  93. throw $exception;
  94. }
  95. elseif ($e instanceof MnsException)
  96. {
  97. throw $e;
  98. }
  99. else
  100. {
  101. throw new MnsException($statusCode, $e->getMessage());
  102. }
  103. }
  104. catch (\Throwable $t)
  105. {
  106. throw new MnsException($statusCode, $t->getMessage());
  107. }
  108. }
  109. }
  110. ?>