No Description

PeekMessageResponse.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\MessagePropertiesForPeek;
  10. class PeekMessageResponse extends BaseResponse
  11. {
  12. use MessagePropertiesForPeek;
  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->readMessagePropertiesForPeekXML($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. ?>