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
|
6 |
|
public function getRequest() |
38
|
|
|
{ |
39
|
6 |
|
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
|
19 |
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response) |
60
|
|
|
{ |
61
|
19 |
|
$this->request = $request; |
62
|
19 |
|
$this->response = $response; |
63
|
|
|
|
64
|
19 |
|
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
|
|
|
* Check if request is GET request |
125
|
|
|
* |
126
|
|
|
* @return boolean |
127
|
|
|
*/ |
128
|
5 |
|
public function isGetRequest() |
129
|
|
|
{ |
130
|
5 |
|
$method = $this->getRequestMethod(); |
131
|
|
|
|
132
|
5 |
|
return !$method || $method === 'GET'; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Check if request is POST request |
137
|
|
|
* |
138
|
|
|
* @return boolean |
139
|
|
|
*/ |
140
|
5 |
|
public function isPostRequest() |
141
|
|
|
{ |
142
|
5 |
|
return $this->getRequestMethod() === 'POST'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Check if request is PUT request |
147
|
|
|
* |
148
|
|
|
* @return boolean |
149
|
|
|
*/ |
150
|
5 |
|
public function isPutRequest() |
151
|
|
|
{ |
152
|
5 |
|
return $this->getRequestMethod() === 'PUT'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Check if request is DELETE request |
157
|
|
|
* |
158
|
|
|
* @return boolean |
159
|
|
|
*/ |
160
|
5 |
|
public function isDeleteRequest() |
161
|
|
|
{ |
162
|
5 |
|
return $this->getRequestMethod() === 'DELETE'; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Check if request is HEAD request |
167
|
|
|
* |
168
|
|
|
* @return boolean |
169
|
|
|
*/ |
170
|
5 |
|
public function isHeadRequest() |
171
|
|
|
{ |
172
|
5 |
|
return $this->getRequestMethod() === 'HEAD'; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get status code of response |
177
|
|
|
* |
178
|
|
|
* @return int |
179
|
|
|
*/ |
180
|
14 |
|
protected function getResponseStatusCode() |
181
|
|
|
{ |
182
|
14 |
|
$response = $this->getResponse(); |
183
|
|
|
|
184
|
14 |
|
return $response ? $response->getStatusCode() : 0; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get method of request |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
5 |
|
protected function getRequestMethod() |
193
|
|
|
{ |
194
|
5 |
|
$request = $this->getRequest(); |
195
|
|
|
|
196
|
5 |
|
return $request ? $request->getMethod() : ''; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|