No Description

ReceiveMessageResponse.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Traits\MessagePropertiesForReceive;
  10. class ReceiveMessageResponse extends BaseResponse
  11. {
  12. use MessagePropertiesForReceive;
  13. // boolean, whether the message body will be decoded as base64
  14. private $base64;
  15. public function __construct($base64 = TRUE)
  16. {
  17. $this->base64 = $base64;
  18. }
  19. public function setBase64($base64)
  20. {
  21. $this->base64 = $base64;
  22. }
  23. public function isBase64()
  24. {
  25. return ($this->base64 == TRUE);
  26. }
  27. public function parseResponse($statusCode, $content)
  28. {
  29. $this->statusCode = $statusCode;
  30. if ($statusCode == 200) {
  31. $this->succeed = TRUE;
  32. } else {
  33. $this->parseErrorResponse($statusCode, $content);
  34. }
  35. $xmlReader = $this->loadXmlContent($content);
  36. try {
  37. $this->readMessagePropertiesForReceiveXML($xmlReader, $this->base64);
  38. } catch (\Exception $e) {
  39. throw new MnsException($statusCode, $e->getMessage(), $e);
  40. } catch (\Throwable $t) {
  41. throw new MnsException($statusCode, $t->getMessage());
  42. }
  43. }
  44. public function parseErrorResponse($statusCode, $content, MnsException $exception = NULL)
  45. {
  46. $this->succeed = FALSE;
  47. $xmlReader = $this->loadXmlContent($content);
  48. try {
  49. $result = XMLParser::parseNormalError($xmlReader);
  50. if ($result['Code'] == Constants::QUEUE_NOT_EXIST)
  51. {
  52. throw new QueueNotExistException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
  53. }
  54. if ($result['Code'] == Constants::MESSAGE_NOT_EXIST)
  55. {
  56. throw new MessageNotExistException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
  57. }
  58. throw new MnsException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
  59. } catch (\Exception $e) {
  60. if ($exception != NULL) {
  61. throw $exception;
  62. } elseif($e instanceof MnsException) {
  63. throw $e;
  64. } else {
  65. throw new MnsException($statusCode, $e->getMessage());
  66. }
  67. } catch (\Throwable $t) {
  68. throw new MnsException($statusCode, $t->getMessage());
  69. }
  70. }
  71. }
  72. ?>