No Description

BatchPeekMessageResponse.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace AliyunMNS\Responses;
  3. use AliyunMNS\Constants;
  4. use AliyunMNS\Exception\MnsException;
  5. use AliyunMNS\Exception\QueueNotExistException;
  6. use AliyunMNS\Exception\MessageNotExistException;
  7. use AliyunMNS\Responses\BaseResponse;
  8. use AliyunMNS\Common\XMLParser;
  9. use AliyunMNS\Model\Message;
  10. class BatchPeekMessageResponse extends BaseResponse
  11. {
  12. protected $messages;
  13. // boolean, whether the message body will be decoded as base64
  14. protected $base64;
  15. public function __construct($base64 = TRUE)
  16. {
  17. $this->messages = array();
  18. $this->base64 = $base64;
  19. }
  20. public function setBase64($base64)
  21. {
  22. $this->base64 = $base64;
  23. }
  24. public function isBase64()
  25. {
  26. return ($this->base64 == TRUE);
  27. }
  28. public function getMessages()
  29. {
  30. return $this->messages;
  31. }
  32. public function parseResponse($statusCode, $content)
  33. {
  34. $this->statusCode = $statusCode;
  35. if ($statusCode == 200) {
  36. $this->succeed = TRUE;
  37. } else {
  38. $this->parseErrorResponse($statusCode, $content);
  39. }
  40. $xmlReader = $this->loadXmlContent($content);
  41. try {
  42. while ($xmlReader->read())
  43. {
  44. if ($xmlReader->nodeType == \XMLReader::ELEMENT
  45. && $xmlReader->name == 'Message')
  46. {
  47. $this->messages[] = Message::fromXML($xmlReader, $this->base64);
  48. }
  49. }
  50. } catch (\Exception $e) {
  51. throw new MnsException($statusCode, $e->getMessage(), $e);
  52. } catch (\Throwable $t) {
  53. throw new MnsException($statusCode, $t->getMessage());
  54. }
  55. }
  56. public function parseErrorResponse($statusCode, $content, MnsException $exception = NULL)
  57. {
  58. $this->succeed = FALSE;
  59. $xmlReader = $this->loadXmlContent($content);
  60. try {
  61. $result = XMLParser::parseNormalError($xmlReader);
  62. if ($result['Code'] == Constants::QUEUE_NOT_EXIST)
  63. {
  64. throw new QueueNotExistException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
  65. }
  66. if ($result['Code'] == Constants::MESSAGE_NOT_EXIST)
  67. {
  68. throw new MessageNotExistException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
  69. }
  70. throw new MnsException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
  71. } catch (\Exception $e) {
  72. if ($exception != NULL) {
  73. throw $exception;
  74. } elseif($e instanceof MnsException) {
  75. throw $e;
  76. } else {
  77. throw new MnsException($statusCode, $e->getMessage());
  78. }
  79. } catch (\Throwable $t) {
  80. throw new MnsException($statusCode, $t->getMessage());
  81. }
  82. }
  83. }
  84. ?>