|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Controller |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class Controller |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Server request |
|
15
|
|
|
* @var ServerRequestInterface |
|
16
|
|
|
**/ |
|
17
|
|
|
protected $request = null; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Response |
|
21
|
|
|
* @var ResponseInterface |
|
22
|
|
|
**/ |
|
23
|
|
|
protected $response = null; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Run the controller |
|
27
|
|
|
* |
|
28
|
|
|
* @return ResponseInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract public function run(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get request, set for controller |
|
34
|
|
|
* |
|
35
|
|
|
* @return ServerRequestInterface |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function getRequest() |
|
38
|
|
|
{ |
|
39
|
1 |
|
return $this->request; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get response. set for controller |
|
44
|
|
|
* |
|
45
|
|
|
* @return ResponseInterface |
|
46
|
|
|
*/ |
|
47
|
15 |
|
public function getResponse() |
|
48
|
|
|
{ |
|
49
|
15 |
|
return $this->response; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Run the controller as function |
|
54
|
|
|
* |
|
55
|
|
|
* @param ServerRequestInterface $request |
|
56
|
|
|
* @param ResponseInterface $response |
|
57
|
|
|
* @return ResponseInterface |
|
58
|
|
|
*/ |
|
59
|
14 |
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response) |
|
60
|
|
|
{ |
|
61
|
14 |
|
$this->request = $request; |
|
62
|
14 |
|
$this->response = $response; |
|
63
|
|
|
|
|
64
|
14 |
|
return $this->run(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Check if response is 2xx succesful, or empty |
|
69
|
|
|
* |
|
70
|
|
|
* @return boolean |
|
71
|
|
|
*/ |
|
72
|
14 |
|
public function isSuccessful() |
|
73
|
|
|
{ |
|
74
|
14 |
|
$code = $this->getResponseStatusCode(); |
|
75
|
|
|
|
|
76
|
14 |
|
return !$code || ($code >= 200 && $code < 300); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Check if response is a 3xx redirect |
|
81
|
|
|
* |
|
82
|
|
|
* @return boolean |
|
83
|
|
|
*/ |
|
84
|
14 |
|
public function isRedirection() |
|
85
|
|
|
{ |
|
86
|
14 |
|
$code = $this->getResponseStatusCode(); |
|
87
|
|
|
|
|
88
|
14 |
|
return $code >= 300 && $code < 400; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Check if response is a 4xx client error |
|
93
|
|
|
* |
|
94
|
|
|
* @return boolean |
|
95
|
|
|
*/ |
|
96
|
14 |
|
public function isClientError() |
|
97
|
|
|
{ |
|
98
|
14 |
|
$code = $this->getResponseStatusCode(); |
|
99
|
|
|
|
|
100
|
14 |
|
return $code >= 400 && $code < 500; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Check if response is a 5xx redirect |
|
105
|
|
|
* |
|
106
|
|
|
* @return boolean |
|
107
|
|
|
*/ |
|
108
|
14 |
|
public function isServerError() |
|
109
|
|
|
{ |
|
110
|
14 |
|
return $this->getResponseStatusCode() >= 500; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Check if response is 4xx or 5xx error |
|
115
|
|
|
* |
|
116
|
|
|
* @return boolean |
|
117
|
|
|
*/ |
|
118
|
13 |
|
public function isError() |
|
119
|
|
|
{ |
|
120
|
13 |
|
return $this->isClientError() || $this->isServerError(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get status code of response |
|
125
|
|
|
* |
|
126
|
|
|
* @return int |
|
127
|
|
|
*/ |
|
128
|
14 |
|
protected function getResponseStatusCode() |
|
129
|
|
|
{ |
|
130
|
14 |
|
$response = $this->getResponse(); |
|
131
|
|
|
|
|
132
|
14 |
|
return $response ? $response->getStatusCode() : 0; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|