1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jenky\Hermes; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Response as GuzzleResponse; |
6
|
|
|
use Illuminate\Support\Traits\Macroable; |
7
|
|
|
use Jenky\Hermes\Concerns\InteractsWithMessage; |
8
|
|
|
use Jenky\Hermes\Contracts\HttpResponseHandler; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
class Response extends GuzzleResponse implements HttpResponseHandler |
12
|
|
|
{ |
13
|
|
|
use InteractsWithMessage, Macroable; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Create new response handler instance. |
17
|
|
|
* |
18
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
19
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
20
|
|
|
*/ |
21
|
|
|
public static function create(ResponseInterface $response): ResponseInterface |
22
|
|
|
{ |
23
|
|
|
$handler = new static( |
24
|
|
|
$response->getStatusCode(), |
25
|
|
|
$response->getHeaders(), |
26
|
|
|
$response->getBody(), |
27
|
|
|
$response->getProtocolVersion(), |
28
|
|
|
$response->getReasonPhrase() |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
if ($handler instanceof Parsable) { |
32
|
|
|
$handler->parse(); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $handler; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Determine that request is successful. |
40
|
|
|
* |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
public function isSuccessful(): bool |
44
|
|
|
{ |
45
|
|
|
$statusCode = $this->getStatusCode(); |
46
|
|
|
|
47
|
|
|
return ($statusCode >= 200 && $statusCode < 300) || $statusCode == 304; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Determine that request is error. |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function isError(): bool |
56
|
|
|
{ |
57
|
|
|
return ! $this->isSuccessful(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Checks if HTTP Status code is Information (1xx). |
62
|
|
|
* |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function isInformational(): bool |
66
|
|
|
{ |
67
|
|
|
return $this->getStatusCode() < 200; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Checks if HTTP Status code is a Redirect (3xx). |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function isRedirect(): bool |
76
|
|
|
{ |
77
|
|
|
$statusCode = $this->getStatusCode(); |
78
|
|
|
|
79
|
|
|
return $statusCode >= 300 && $statusCode < 400; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Checks if HTTP Status code is a Client Error (4xx). |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function isClientError(): bool |
88
|
|
|
{ |
89
|
|
|
$statusCode = $this->getStatusCode(); |
90
|
|
|
|
91
|
|
|
return $statusCode >= 400 && $statusCode < 500; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Checks if HTTP Status code is a Server Error (5xx). |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function isServerError(): bool |
100
|
|
|
{ |
101
|
|
|
return $this->getStatusCode() >= 500; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|