Result.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace OSS\Result;
  3. use OSS\Core\OssException;
  4. use OSS\Http\ResponseCore;
  5. /**
  6. * Class Result, The result class of The operation of the base class, different requests in dealing with the return of data have different logic,
  7. * The specific parsing logic postponed to subclass implementation
  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. * Get 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. * Get the returned data, different request returns the data format is different
  44. *
  45. * $return mixed
  46. */
  47. public function getData()
  48. {
  49. return $this->parsedData;
  50. }
  51. /**
  52. * Subclass implementation, different requests return data has different analytical logic, implemented by subclasses
  53. *
  54. * @return mixed
  55. */
  56. abstract protected function parseDataFromResponse();
  57. /**
  58. * Whether the operation is successful
  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. * Try to get the error message from body
  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. * Try to get the error Code from body
  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. * Judging from the return http status code, [200-299] that is 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. * Return the original return data
  139. *
  140. * @return ResponseCore
  141. */
  142. public function getRawResponse()
  143. {
  144. return $this->rawResponse;
  145. }
  146. /**
  147. * Indicate whether the request is successful
  148. */
  149. protected $isOk = false;
  150. /**
  151. * Data parsed by subclasses
  152. */
  153. protected $parsedData = null;
  154. /**
  155. * Store the original Response returned by the auth function
  156. *
  157. * @var ResponseCore
  158. */
  159. protected $rawResponse;
  160. }