PluginProbe ʕ •ᴥ•ʔ
Brevo – Email, SMS, Web Push, Chat, and more. / 3.2.0
Brevo – Email, SMS, Web Push, Chat, and more. v3.2.0
2.9.13 2.9.14 2.9.15 2.9.16 2.9.17 2.9.18 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.2 3.1.20 3.1.21 3.1.22 3.1.23 3.1.24 3.1.25 3.1.26 3.1.27 3.1.28 3.1.29 3.1.3 3.1.30 3.1.31 3.1.32 3.1.33 3.1.34 3.1.35 3.1.36 3.1.37 3.1.38 3.1.39 3.1.4 3.1.40 3.1.41 3.1.42 3.1.43 3.1.44 3.1.45 3.1.46 3.1.47 3.1.48 3.1.49 3.1.5 3.1.50 3.1.51 3.1.52 3.1.53 3.1.54 3.1.55 3.1.56 3.1.57 3.1.58 3.1.59 3.1.6 3.1.60 3.1.61 3.1.62 3.1.63 3.1.64 3.1.65 3.1.66 3.1.67 3.1.68 3.1.69 3.1.7 3.1.70 3.1.71 3.1.72 3.1.73 3.1.74 3.1.75 3.1.76 3.1.77 3.1.78 3.1.79 3.1.8 3.1.80 3.1.81 3.1.82 3.1.83 3.1.84 3.1.85 3.1.86 3.1.87 3.1.88 3.1.89 3.1.9 3.1.90 3.1.91 3.1.92 3.1.93 3.1.94 3.1.95 3.1.96 3.1.97 3.1.98 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 trunk 1.0 1.5 2.0.8 2.9.10 2.9.11 2.9.12
mailin / wonderpush-php-lib / lib / Net / Response.php
mailin / wonderpush-php-lib / lib / Net Last commit date
CurlHttpClient.php 1 year ago HttpClientInterface.php 1 year ago Request.php 1 year ago Response.php 1 year ago
Response.php
279 lines
1 <?php
2
3 namespace WonderPush\Net;
4
5 if (count(get_included_files()) === 1) { http_response_code(403); exit(); } // Prevent direct access
6
7 /**
8 * Represents an HTTP response, with JSON parsing facility.
9 */
10 class Response extends \WonderPush\Obj\BaseObject {
11
12 /**
13 * The request
14 * @var Request
15 */
16 private $request;
17 /**
18 * HTTP Status code.
19 * @var integer
20 */
21 private $statusCode;
22
23 /**
24 * HTTP headers.
25 * @var string[]
26 */
27 private $headers;
28
29 /**
30 * Raw HTTP body.
31 * @var string
32 */
33 private $rawBody;
34
35 /**
36 * Whether parsing has already taken place.
37 * @var boolean
38 */
39 private $isParsed;
40
41 /**
42 * JSON parsed body, or `null` on parsing errors.
43 *
44 * Uses objects instead of associative arrays to preserve `{}`/`[]` distinctions.
45 *
46 * @var object|null
47 */
48 private $parsedBody;
49
50 /**
51 * @var \WonderPush\Errors\Base
52 */
53 private $parseError;
54
55 /**
56 * @return Request
57 */
58 public function getRequest() {
59 return $this->request;
60 }
61
62 /**
63 * @param Request $request
64 * @return Response
65 */
66 public function setRequest($request) {
67 $this->request = $request;
68 return $this;
69 }
70
71 /**
72 * The HTTP Status code.
73 * @return integer
74 */
75 public function getStatusCode() {
76 return $this->statusCode;
77 }
78
79 /**
80 * Set the HTTP Status code.
81 * @param integer $statusCode
82 * @return $this
83 */
84 public function setStatusCode($statusCode) {
85 $this->statusCode = $statusCode;
86 return $this;
87 }
88
89 /**
90 * The HTTP headers.
91 * @return string[]
92 */
93 public function getHeaders() {
94 return $this->headers;
95 }
96
97 /**
98 * Set the HTTP headers.
99 * @param string[] $headers
100 * @return $this
101 */
102 public function setHeaders($headers) {
103 $this->headers = $headers;
104 return $this;
105 }
106
107 /**
108 * The raw HTTP body.
109 * @return string
110 */
111 public function getRawBody() {
112 return $this->rawBody;
113 }
114
115 /**
116 * Set the raw HTTP body.
117 * @param string $rawBody
118 * @return $this
119 */
120 public function setRawBody($rawBody) {
121 $this->rawBody = $rawBody;
122
123 // Reset parsing info
124 $this->isParsed = null;
125 $this->parsedBody = null;
126 $this->parseError = null;
127
128 return $this;
129 }
130
131 /**
132 * Parses the raw HTML body into JSON, and notes any parsing error.
133 */
134 private function parseBody() {
135 if ($this->isParsed) {
136 return;
137 }
138
139 // false body means no answer from server
140 if ($this->rawBody === false) {
141 $this->parsedBody = null;
142 $this->parseError = new \WonderPush\Errors\Network($this->request, $this);
143 $this->isParsed = true;
144 return;
145 }
146
147 // Try json_decode
148 $this->parsedBody = json_decode($this->rawBody, false);
149
150 // Manage json parsing errors
151 $jsonParseError = json_last_error();
152 if (function_exists('json_last_error_msg')) {
153 // @codingStandardsIgnoreLine
154 $jsonParseErrorMsg = json_last_error_msg();
155 } else {
156 switch ($this->parseError) {
157 case JSON_ERROR_NONE:
158 // https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/json/tests/007.phpt
159 $jsonParseErrorMsg = 'No error';
160 break;
161 case JSON_ERROR_DEPTH:
162 // https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/json/tests/007.phpt
163 $jsonParseErrorMsg = 'Maximum stack depth exceeded';
164 break;
165 case JSON_ERROR_STATE_MISMATCH:
166 // https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/json/tests/007.phpt
167 $jsonParseErrorMsg = 'State mismatch (invalid or malformed JSON)';
168 break;
169 case JSON_ERROR_CTRL_CHAR:
170 // https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/json/tests/007.phpt
171 $jsonParseErrorMsg = 'Control character error, possibly incorrectly encoded';
172 break;
173 case JSON_ERROR_SYNTAX:
174 // https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/json/tests/007.phpt
175 $jsonParseErrorMsg = 'Syntax error';
176 break;
177 // @codingStandardsIgnoreLine
178 case JSON_ERROR_UTF8:
179 // https://github.com/php/php-src/blob/6053987bc27e8dede37f437193a5cad448f99bce/ext/json/tests/bug54058.phpt
180 $jsonParseErrorMsg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
181 break;
182 default:
183 // On PHP 5.5.0 there were other codes, but fortunately, the json_last_error_msg() function exists
184 // so we won't end up here.
185 $jsonParseErrorMsg = 'Unknown error';
186 break;
187 }
188 }
189 if ($jsonParseError !== JSON_ERROR_NONE) {
190 $this->parseError = new \WonderPush\Errors\Parsing($jsonParseError, $jsonParseErrorMsg . ' body:' . ($this->rawBody ?: '') . ' status:' . $this->statusCode . ' headers:' . ($this->headers ? json_encode($this->headers) : ''));
191 }
192 $this->isParsed = true;
193 }
194
195 /**
196 * The error code encountered while parsing the body, if any.
197 * @return \WonderPush\Errors\Base
198 */
199 public function parseError() {
200 $this->parseBody();
201 return $this->parseError;
202 }
203
204 /**
205 * The parsed body, or a {@link \WonderPush\Errors\Parsing}.
206 * @return mixed
207 */
208 public function parsedBody() {
209 $this->parseBody();
210 return $this->parsedBody;
211 }
212
213 /**
214 * Returns an exception when there was an error making the call, or if the server returned an error response.
215 * @return null|\WonderPush\Errors\Base
216 */
217 public function exception() {
218 $this->parseBody();
219
220 if ($this->parseError) {
221 return $this->parseError;
222 }
223
224 $statusCode = $this->getStatusCode();
225 $body = $this->parsedBody();
226
227 $error = false; // false if no exception, true to return one, or an actual \Exception for chaining
228 $errorMessage = null;
229 $errorCode = null;
230
231 if (isset($body->error) && is_object($body->error)) {
232
233 $error = true;
234 if (isset($body->error->message)) {
235 $errorMessage = $body->error->message;
236 }
237 if (isset($body->error->code )) {
238 $errorCode = $body->error->code;
239 }
240 //if (isset($body->error->status )) {
241 // $statusCode = $body->error->status;
242 //}
243
244 }
245
246 if ($statusCode < 200 || $statusCode >= 300) {
247 $error = true;
248 if ($errorMessage === null) {
249 $errorMessage = 'Non 2xx status code';
250 }
251 }
252
253 if ($error !== false) {
254 if ($error instanceof \Exception) {
255 return new \WonderPush\Errors\Server($this->request, $this, $errorMessage, $errorCode, $error);
256 }
257 return new \WonderPush\Errors\Server($this->request, $this, $errorMessage, $errorCode);
258 }
259 return null;
260 }
261
262 /**
263 * Returns the result instantiated with provided $cls or throws when request had error.
264 * @param $cls
265 * @return mixed
266 * @throws \WonderPush\Errors\Base
267 */
268 public function checkedResult($cls, $key = null) {
269 $exception = $this->exception();
270 if ($exception) {
271 throw $exception;
272 }
273 $this->parseBody();
274 $body = $this->parsedBody();
275 return new $cls($key ? $body->{$key} : $body);
276 }
277
278 }
279