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