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
|
16 |
|
public function getResponse() |
48
|
|
|
{ |
49
|
16 |
|
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
|
|
|
* Encode data to send to client |
125
|
|
|
* |
126
|
|
|
* @param mixed $data |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
13 |
|
public function encodeData($data) |
130
|
|
|
{ |
131
|
13 |
|
$response = $this->getResponse(); |
132
|
13 |
|
$invalid = !$response || |
133
|
12 |
|
!($type = $response->getHeaderLine('Content-Type')) || |
134
|
13 |
|
!in_array($type, ['application/json', 'text/xml', 'application/xml'], true); |
135
|
|
|
|
136
|
13 |
|
if ($invalid) { |
137
|
6 |
|
throw new \RuntimeException("Valid content type is not set in response for encoding data"); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
switch ($type) { |
|
|
|
|
141
|
7 |
|
case 'application/json': |
142
|
5 |
|
return $this->encodeDataAsJson($data); |
143
|
2 |
|
case 'text/xml': |
144
|
2 |
|
case 'application/xml': |
145
|
2 |
|
return $this->encodeDataAsXml($data); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Encode data as xml |
151
|
|
|
* |
152
|
|
|
* @param \SimpleXMLElement $data |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
2 |
|
protected function encodeDataAsXml(\SimpleXMLElement $data) |
156
|
|
|
{ |
157
|
2 |
|
return $data->asXML(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Encode data as json |
162
|
|
|
* |
163
|
|
|
* @param mixed |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
5 |
|
protected function encodeDataAsJson($data) |
167
|
|
|
{ |
168
|
5 |
|
$data = json_encode($data); |
169
|
|
|
|
170
|
5 |
|
return $this->isJsonp() ? |
171
|
5 |
|
$this->getRequest()->getQueryParams()['callback'] . '(' . $data . ')' : |
172
|
5 |
|
$data; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Check if we should respond with jsonp |
177
|
|
|
* |
178
|
|
|
* @return boolean |
179
|
|
|
*/ |
180
|
5 |
|
protected function isJsonp() |
181
|
|
|
{ |
182
|
5 |
|
$request = $this->getRequest(); |
183
|
|
|
|
184
|
5 |
|
return $request && !empty($request->getQueryParams()['callback']); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get status code of response |
189
|
|
|
* |
190
|
|
|
* @return int |
191
|
|
|
*/ |
192
|
14 |
|
protected function getResponseStatusCode() |
193
|
|
|
{ |
194
|
14 |
|
$response = $this->getResponse(); |
195
|
|
|
|
196
|
14 |
|
return $response ? $response->getStatusCode() : 0; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: