No Description

BatchSendMessageResponse.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace AliyunMNS\Responses;
  3. use AliyunMNS\Constants;
  4. use AliyunMNS\Exception\MnsException;
  5. use AliyunMNS\Exception\BatchSendFailException;
  6. use AliyunMNS\Exception\QueueNotExistException;
  7. use AliyunMNS\Exception\InvalidArgumentException;
  8. use AliyunMNS\Exception\MalformedXMLException;
  9. use AliyunMNS\Responses\BaseResponse;
  10. use AliyunMNS\Common\XMLParser;
  11. use AliyunMNS\Model\SendMessageResponseItem;
  12. use AliyunMNS\Traits\MessageIdAndMD5;
  13. class BatchSendMessageResponse extends BaseResponse
  14. {
  15. protected $sendMessageResponseItems;
  16. public function __construct()
  17. {
  18. $this->sendMessageResponseItems = array();
  19. }
  20. public function getSendMessageResponseItems()
  21. {
  22. return $this->sendMessageResponseItems;
  23. }
  24. public function parseResponse($statusCode, $content)
  25. {
  26. $this->statusCode = $statusCode;
  27. if ($statusCode == 201) {
  28. $this->succeed = TRUE;
  29. } else {
  30. $this->parseErrorResponse($statusCode, $content);
  31. }
  32. $xmlReader = $this->loadXmlContent($content);
  33. try {
  34. while ($xmlReader->read())
  35. {
  36. if ($xmlReader->nodeType == \XMLReader::ELEMENT && $xmlReader->name == 'Message') {
  37. $this->sendMessageResponseItems[] = SendMessageResponseItem::fromXML($xmlReader);
  38. }
  39. }
  40. } catch (\Exception $e) {
  41. throw new MnsException($statusCode, $e->getMessage(), $e);
  42. } catch (\Throwable $t) {
  43. throw new MnsException($statusCode, $t->getMessage());
  44. }
  45. }
  46. public function parseErrorResponse($statusCode, $content, MnsException $exception = NULL)
  47. {
  48. $this->succeed = FALSE;
  49. $xmlReader = $this->loadXmlContent($content);
  50. try {
  51. while ($xmlReader->read())
  52. {
  53. if ($xmlReader->nodeType == \XMLReader::ELEMENT) {
  54. switch ($xmlReader->name) {
  55. case Constants::ERROR:
  56. $this->parseNormalErrorResponse($xmlReader);
  57. break;
  58. default: // case Constants::Messages
  59. $this->parseBatchSendErrorResponse($xmlReader);
  60. break;
  61. }
  62. }
  63. }
  64. } catch (\Exception $e) {
  65. if ($exception != NULL) {
  66. throw $exception;
  67. } elseif($e instanceof MnsException) {
  68. throw $e;
  69. } else {
  70. throw new MnsException($statusCode, $e->getMessage());
  71. }
  72. } catch (\Throwable $t) {
  73. throw new MnsException($statusCode, $t->getMessage());
  74. }
  75. }
  76. private function parseBatchSendErrorResponse($xmlReader)
  77. {
  78. $ex = new BatchSendFailException($this->statusCode, "BatchSendMessage Failed For Some Messages");
  79. while ($xmlReader->read())
  80. {
  81. if ($xmlReader->nodeType == \XMLReader::ELEMENT && $xmlReader->name == 'Message') {
  82. $ex->addSendMessageResponseItem( SendMessageResponseItem::fromXML($xmlReader));
  83. }
  84. }
  85. throw $ex;
  86. }
  87. private function parseNormalErrorResponse($xmlReader)
  88. {
  89. $result = XMLParser::parseNormalError($xmlReader);
  90. if ($result['Code'] == Constants::QUEUE_NOT_EXIST)
  91. {
  92. throw new QueueNotExistException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
  93. }
  94. if ($result['Code'] == Constants::INVALID_ARGUMENT)
  95. {
  96. throw new InvalidArgumentException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
  97. }
  98. if ($result['Code'] == Constants::MALFORMED_XML)
  99. {
  100. throw new MalformedXMLException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
  101. }
  102. throw new MnsException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
  103. }
  104. }
  105. ?>