Result.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace OSS\Result;
  3. use OSS\Core\OssException;
  4. use OSS\Http\ResponseCore;
  5. /**
  6. * Class Result, 操作结果类的基类,不同的请求在处理返回数据的时候有不同的逻辑,
  7. * 具体的解析逻辑推迟到子类实现
  8. *
  9. * @package OSS\Model
  10. */
  11. abstract class Result
  12. {
  13. /**
  14. * Result constructor.
  15. * @param $response ResponseCore
  16. * @throws OssException
  17. */
  18. public function __construct($response)
  19. {
  20. if ($response === null) {
  21. throw new OssException("raw response is null");
  22. }
  23. $this->rawResponse = $response;
  24. $this->parseResponse();
  25. }
  26. /**
  27. * 获取requestId
  28. *
  29. * @return string
  30. */
  31. public function getRequestId()
  32. {
  33. if (isset($this->rawResponse) &&
  34. isset($this->rawResponse->header) &&
  35. isset($this->rawResponse->header['x-oss-request-id'])
  36. ) {
  37. return $this->rawResponse->header['x-oss-request-id'];
  38. } else {
  39. return '';
  40. }
  41. }
  42. /**
  43. * 得到返回数据,不同的请求返回数据格式不同
  44. *
  45. * $return mixed
  46. */
  47. public function getData()
  48. {
  49. return $this->parsedData;
  50. }
  51. /**
  52. * 由子类实现,不同的请求返回数据有不同的解析逻辑,由子类实现
  53. *
  54. * @return mixed
  55. */
  56. abstract protected function parseDataFromResponse();
  57. /**
  58. * 操作是否成功
  59. *
  60. * @return mixed
  61. */
  62. public function isOK()
  63. {
  64. return $this->isOk;
  65. }
  66. /**
  67. * @throws OssException
  68. */
  69. public function parseResponse()
  70. {
  71. $this->isOk = $this->isResponseOk();
  72. if ($this->isOk) {
  73. $this->parsedData = $this->parseDataFromResponse();
  74. } else {
  75. $httpStatus = strval($this->rawResponse->status);
  76. $requestId = strval($this->getRequestId());
  77. $code = $this->retrieveErrorCode($this->rawResponse->body);
  78. $message = $this->retrieveErrorMessage($this->rawResponse->body);
  79. $body = $this->rawResponse->body;
  80. $details = array(
  81. 'status' => $httpStatus,
  82. 'request-id' => $requestId,
  83. 'code' => $code,
  84. 'message' => $message,
  85. 'body' => $body
  86. );
  87. throw new OssException($details);
  88. }
  89. }
  90. /**
  91. * 尝试从body中获取错误Message
  92. *
  93. * @param $body
  94. * @return string
  95. */
  96. private function retrieveErrorMessage($body)
  97. {
  98. if (empty($body) || false === strpos($body, '<?xml')) {
  99. return '';
  100. }
  101. $xml = simplexml_load_string($body);
  102. if (isset($xml->Message)) {
  103. return strval($xml->Message);
  104. }
  105. return '';
  106. }
  107. /**
  108. * 尝试从body中获取错误Code
  109. *
  110. * @param $body
  111. * @return string
  112. */
  113. private function retrieveErrorCode($body)
  114. {
  115. if (empty($body) || false === strpos($body, '<?xml')) {
  116. return '';
  117. }
  118. $xml = simplexml_load_string($body);
  119. if (isset($xml->Code)) {
  120. return strval($xml->Code);
  121. }
  122. return '';
  123. }
  124. /**
  125. * 根据返回http状态码判断,[200-299]即认为是OK
  126. *
  127. * @return bool
  128. */
  129. protected function isResponseOk()
  130. {
  131. $status = $this->rawResponse->status;
  132. if ((int)(intval($status) / 100) == 2) {
  133. return true;
  134. }
  135. return false;
  136. }
  137. /**
  138. * 返回原始的返回数据
  139. *
  140. * @return ResponseCore
  141. */
  142. public function getRawResponse()
  143. {
  144. return $this->rawResponse;
  145. }
  146. /**
  147. * 标示请求是否成功
  148. */
  149. protected $isOk = false;
  150. /**
  151. * 由子类解析过的数据
  152. */
  153. protected $parsedData = null;
  154. /**
  155. * 存放auth函数返回的原始Response
  156. *
  157. * @var ResponseCore
  158. */
  159. protected $rawResponse;
  160. }