1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jasny\Controller; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Methods to check the response |
9
|
|
|
*/ |
10
|
|
|
trait CheckResponse |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Get response. set for controller |
14
|
|
|
* |
15
|
|
|
* @return ResponseInterface |
16
|
|
|
*/ |
17
|
|
|
abstract public function getResponse(); |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Check if response is a 1xx informational |
22
|
|
|
* |
23
|
|
|
* @return boolean |
24
|
|
|
*/ |
25
|
15 |
|
public function isInformational() |
26
|
|
|
{ |
27
|
15 |
|
$code = $this->getResponse()->getStatusCode() ?: 200; |
28
|
15 |
|
return $code >= 100 && $code < 200; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Check if response is 2xx succesful, or empty |
33
|
|
|
* |
34
|
|
|
* @return boolean |
35
|
|
|
*/ |
36
|
15 |
|
public function isSuccessful() |
37
|
|
|
{ |
38
|
15 |
|
$code = $this->getResponse()->getStatusCode() ?: 200; |
39
|
15 |
|
return $code >= 200 && $code < 300; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Check if response is a 3xx redirect |
44
|
|
|
* |
45
|
|
|
* @return boolean |
46
|
|
|
*/ |
47
|
15 |
|
public function isRedirection() |
48
|
|
|
{ |
49
|
15 |
|
$code = $this->getResponse()->getStatusCode() ?: 200; |
50
|
15 |
|
return $code >= 300 && $code < 400; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Check if response is a 4xx client error |
55
|
|
|
* |
56
|
|
|
* @return boolean |
57
|
|
|
*/ |
58
|
15 |
|
public function isClientError() |
59
|
|
|
{ |
60
|
15 |
|
$code = $this->getResponse()->getStatusCode() ?: 200; |
61
|
15 |
|
return $code >= 400 && $code < 500; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check if response is a 5xx redirect |
66
|
|
|
* |
67
|
|
|
* @return boolean |
68
|
|
|
*/ |
69
|
15 |
|
public function isServerError() |
70
|
|
|
{ |
71
|
15 |
|
$code = $this->getResponse()->getStatusCode() ?: 200; |
72
|
15 |
|
return $code >= 500 && $code < 600; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check if response is 4xx or 5xx error |
77
|
|
|
* |
78
|
|
|
* @return boolean |
79
|
|
|
*/ |
80
|
15 |
|
public function isError() |
81
|
|
|
{ |
82
|
15 |
|
return $this->isClientError() || $this->isServerError(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|