You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
3.9 KiB

6 years ago
  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. }