Response   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
dl 0
loc 170
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getError() 0 3 1
A success() 0 3 1
A getErrorCode() 0 4 1
A fail() 0 3 1
A getContent() 0 3 1
A getData() 0 3 1
A getResponse() 0 3 1
A getOriginal() 0 3 1
B __construct() 0 32 6
A getRequest() 0 3 1
1
<?php
2
3
namespace Vicens\Alidayu;
4
5
use \Psr\Http\Message\ResponseInterface;
6
use Vicens\Alidayu\Request\AbstractRequest;
7
8
class Response
9
{
10
11
    /**
12
     * 原响应实例
13
     * @var ResponseInterface
14
     */
15
    protected $response;
16
17
    /**
18
     * @var AbstractRequest
19
     */
20
    protected $request;
21
22
    /**
23
     * 响应内容
24
     * @var array
25
     */
26
    protected $content;
27
28
    /**
29
     * 原始影响内容
30
     * @var string
31
     */
32
    protected $original;
33
34
    /**
35
     * 响应的数据
36
     * @var array|string|bool|mixed
37
     */
38
    protected $data;
39
40
    /**
41
     * 是否成功
42
     * @var bool
43
     */
44
    protected $success = false;
45
46
    /**
47
     * 错误消息
48
     * @var string|null
49
     */
50
    protected $error;
51
52
    /**
53
     * 错误代码
54
     * @var string|null
55
     */
56
    protected $errorCode;
57
58
    /**
59
     * Response constructor.
60
     * @param ResponseInterface $response
61
     * @param AbstractRequest $request
62
     */
63
    public function __construct(ResponseInterface $response, AbstractRequest $request)
64
    {
65
        $this->response = $response;
66
67
        $this->request = $request;
68
69
        $this->original = $response->getBody()->getContents();
70
71
        $this->content = json_decode($this->original, true);
72
73
        if (array_key_exists('error_response', $this->content)) {
74
75
            $error = $this->content['error_response'];
76
77
            if (array_key_exists('sub_code', $error)) {
78
                $this->errorCode = $error['sub_code'];
79
            } elseif (array_key_exists('code', $error)) {
80
                $this->errorCode = $error['code'];
81
            }
82
83
            if (array_key_exists('sub_msg', $error)) {
84
                $this->error = $error['sub_msg'];
85
            } elseif (array_key_exists('msg', $error)) {
86
                $this->error = $error['msg'];
87
            } else {
88
                $this->error = 'UnKnown Error';
89
            }
90
91
        } else {
92
93
            $this->success = true;
94
            $this->data = $request->parseData($this->content);
95
        }
96
    }
97
98
    /**
99
     * 返回原响应实例
100
     * @return ResponseInterface
101
     */
102
    public function getResponse()
103
    {
104
        return $this->response;
105
    }
106
107
    /**
108
     * 是否成功
109
     * @return bool
110
     */
111
    public function success()
112
    {
113
        return $this->success;
114
    }
115
116
    /**
117
     * 是否失败
118
     * @return bool
119
     */
120
    public function fail()
121
    {
122
        return !$this->success();
123
    }
124
125
    /**
126
     * 返回响应内容
127
     * @return array
128
     */
129
    public function getContent()
130
    {
131
        return $this->content;
132
    }
133
134
    /**
135
     * 返回原始响应内容
136
     * @return string
137
     */
138
    public function getOriginal()
139
    {
140
        return $this->original;
141
    }
142
143
    /**
144
     * 返回响应数据
145
     * @return array|string|bool|mixed
146
     */
147
    public function getData()
148
    {
149
        return $this->data;
150
    }
151
152
    /**
153
     * 返回错误消息
154
     * @return null|string
155
     */
156
    public function getError()
157
    {
158
        return $this->error;
159
    }
160
161
    /**
162
     * 返回错误代码
163
     * @return null|string
164
     */
165
    public function getErrorCode()
166
    {
167
168
        return $this->errorCode;
169
    }
170
171
    /**
172
     * 返回请求实例
173
     * @return AbstractRequest
174
     */
175
    public function getRequest()
176
    {
177
        return $this->request;
178
    }
179
}
180