1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Postpay\Http; |
4
|
|
|
|
5
|
|
|
use Postpay\Exceptions\GraphQLException; |
6
|
|
|
use Postpay\Exceptions\RESTfulException; |
7
|
|
|
|
8
|
|
|
class Response |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \Postpay\Http\Request The original request. |
12
|
|
|
*/ |
13
|
|
|
protected $request; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int The HTTP response status code. |
17
|
|
|
*/ |
18
|
|
|
protected $statusCode; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array The headers returned from API. |
22
|
|
|
*/ |
23
|
|
|
protected $headers; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string The raw body of the response. |
27
|
|
|
*/ |
28
|
|
|
protected $body; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array The decoded body. |
32
|
|
|
*/ |
33
|
|
|
protected $decodedBody = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string The exception type to be thrown. |
37
|
|
|
*/ |
38
|
|
|
protected $exc; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \Postpay\Exceptions\PostpayException The exception thrown. |
42
|
|
|
*/ |
43
|
|
|
protected $thrownException; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Creates a new Response entity. |
47
|
|
|
* |
48
|
|
|
* @param \Postpay\Http\Request $request |
49
|
|
|
* @param int|null $statusCode |
50
|
|
|
* @param array|null $headers |
51
|
|
|
* @param string|null $body |
52
|
|
|
*/ |
53
|
21 |
|
public function __construct( |
54
|
|
|
Request $request, |
55
|
|
|
$statusCode = null, |
56
|
|
|
array $headers = [], |
57
|
|
|
$body = null |
58
|
|
|
) { |
59
|
21 |
|
$this->request = $request; |
60
|
21 |
|
$this->statusCode = $statusCode; |
61
|
21 |
|
$this->headers = $headers; |
62
|
21 |
|
$this->body = $body; |
63
|
|
|
|
64
|
21 |
|
if ($this->request->isGraphQL()) { |
65
|
1 |
|
$this->exc = GraphQLException::class; |
66
|
|
|
} else { |
67
|
20 |
|
$this->exc = RESTfulException::class; |
68
|
|
|
} |
69
|
21 |
|
$this->decodeBody(); |
70
|
21 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return the original request that returned this response. |
74
|
|
|
* |
75
|
|
|
* @return \Postpay\Http\Request |
76
|
|
|
*/ |
77
|
7 |
|
public function getRequest() |
78
|
|
|
{ |
79
|
7 |
|
return $this->request; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Return the HTTP status code. |
84
|
|
|
* |
85
|
|
|
* @return int |
86
|
|
|
*/ |
87
|
5 |
|
public function getStatusCode() |
88
|
|
|
{ |
89
|
5 |
|
return $this->statusCode; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return the HTTP headers. |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
1 |
|
public function getHeaders() |
98
|
|
|
{ |
99
|
1 |
|
return $this->headers; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return the JSON response. |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
7 |
|
public function json() |
108
|
|
|
{ |
109
|
7 |
|
return $this->decodedBody; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns true if API returned an error message. |
114
|
|
|
* |
115
|
|
|
* @return boolean |
116
|
|
|
*/ |
117
|
21 |
|
public function isError() |
118
|
|
|
{ |
119
|
21 |
|
$excClassName = $this->exc; |
120
|
21 |
|
return isset($this->decodedBody[$excClassName::ERROR_KEY]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Convert the raw response into an array if possible. |
125
|
|
|
*/ |
126
|
21 |
|
public function decodeBody() |
127
|
|
|
{ |
128
|
21 |
|
$this->decodedBody = json_decode($this->body, true); |
|
|
|
|
129
|
|
|
|
130
|
21 |
|
if (!is_array($this->decodedBody)) { |
131
|
15 |
|
$this->decodedBody = []; |
132
|
|
|
} |
133
|
|
|
|
134
|
21 |
|
if ($this->isError()) { |
135
|
3 |
|
$this->thrownException = new $this->exc($this); |
136
|
|
|
} |
137
|
21 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns the exception that was thrown. |
141
|
|
|
* |
142
|
|
|
* @return \Postpay\Exceptions\ApiException|null |
143
|
|
|
*/ |
144
|
2 |
|
public function getThrownException() |
145
|
|
|
{ |
146
|
2 |
|
return $this->thrownException; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..