Total Complexity | 56 |
Total Lines | 336 |
Duplicated Lines | 0 % |
Coverage | 72.62% |
Changes | 0 |
Complex classes like Request often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Request |
||
11 | { |
||
12 | use SingletonTrait; |
||
13 | |||
14 | const VERB_GET = 'GET'; |
||
15 | const VERB_POST = 'POST'; |
||
16 | const VERB_PUT = 'PUT'; |
||
17 | const VERB_DELETE = 'DELETE'; |
||
18 | const VERB_OPTIONS = 'OPTIONS'; |
||
19 | const VERB_HEAD = 'HEAD'; |
||
20 | const VERB_PATCH = 'PATCH'; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $server; |
||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $cookies; |
||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $upload; |
||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $header; |
||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $data; |
||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $raw = []; |
||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $query; |
||
50 | /** |
||
51 | * @var bool |
||
52 | */ |
||
53 | private $isLoaded = false; |
||
54 | |||
55 | 2 | public function init() |
|
56 | { |
||
57 | 2 | $this->server = $_SERVER or []; |
|
58 | 2 | $this->cookies = $_COOKIE or []; |
|
59 | 2 | $this->upload = $_FILES or []; |
|
60 | 2 | $this->header = $this->parseHeaders(); |
|
61 | 2 | $this->data = $_REQUEST or []; |
|
62 | 2 | $this->query = $_GET or []; |
|
63 | 2 | $this->raw = json_decode(file_get_contents('php://input'), true) ?: []; |
|
64 | 2 | $this->isLoaded = true; |
|
65 | 2 | } |
|
66 | |||
67 | /** |
||
68 | * @return bool |
||
69 | */ |
||
70 | 1 | public function isLoaded() { |
|
71 | 1 | return $this->isLoaded; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * Método que devuelve las cabeceras de la petición |
||
76 | * @return array |
||
77 | */ |
||
78 | 2 | private function parseHeaders() |
|
79 | { |
||
80 | 2 | return getallheaders(); |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Método que verifica si existe una cabecera concreta |
||
85 | * @param $header |
||
86 | * |
||
87 | * @return boolean |
||
88 | */ |
||
89 | 6 | public function hasHeader($header) |
|
90 | { |
||
91 | 6 | return array_key_exists($header, $this->header); |
|
92 | } |
||
93 | |||
94 | |||
95 | /** |
||
96 | * Método que indica si una petición tiene cookies |
||
97 | * @return boolean |
||
98 | */ |
||
99 | 1 | public function hasCookies() |
|
100 | { |
||
101 | 1 | return (null !== $this->cookies && 0 !== count($this->cookies)); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Método que indica si una petición tiene cookies |
||
106 | * @return boolean |
||
107 | */ |
||
108 | 1 | public function hasUpload() |
|
109 | { |
||
110 | 1 | return (null !== $this->upload && 0 !== count($this->upload)); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Método que devuelve el TimeStamp de la petición |
||
115 | * |
||
116 | * @param boolean $formatted |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | public static function ts($formatted = false) |
||
121 | { |
||
122 | return self::getInstance()->getTs($formatted); |
||
123 | } |
||
124 | |||
125 | 1 | public function getTs($formatted = false) |
|
126 | { |
||
127 | 1 | return $formatted ? date('Y-m-d H:i:s', $this->server['REQUEST_TIME_FLOAT']) : $this->server['REQUEST_TIME_FLOAT']; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * Método que devuelve el Método HTTP utilizado |
||
132 | * @return string |
||
133 | */ |
||
134 | 4 | public function getMethod() |
|
135 | { |
||
136 | 4 | return array_key_exists('REQUEST_METHOD', $this->server) ? strtoupper($this->server['REQUEST_METHOD']) : 'GET'; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Método que devuelve una cabecera de la petición si existe |
||
141 | * @param string $name |
||
142 | * @param string $default |
||
143 | * |
||
144 | * @return string|null |
||
145 | */ |
||
146 | 5 | public static function header($name, $default = null) |
|
147 | { |
||
148 | 5 | return self::getInstance()->getHeader($name, $default); |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param string $name |
||
153 | * @param string $default |
||
154 | * @return string|null |
||
155 | */ |
||
156 | 5 | public function getHeader($name, $default = null) |
|
157 | { |
||
158 | 5 | $header = null; |
|
159 | 5 | if ($this->hasHeader($name)) { |
|
160 | $header = $this->header[$name]; |
||
161 | 5 | } else if(array_key_exists('h_' . strtolower($name), $this->query)) { |
|
162 | $header = $this->query['h_' . strtolower($name)]; |
||
163 | 5 | } else if(array_key_exists('HTTP_' . strtoupper(str_replace('-', '_', $name)), $this->server)) { |
|
164 | $header = $this->server['HTTP_' . strtoupper(str_replace('-', '_', $name))]; |
||
165 | } |
||
166 | 5 | return $header ?: $default; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * Método que devuelve la url solicitada |
||
171 | * @return string|null |
||
172 | */ |
||
173 | 1 | public static function requestUri() |
|
174 | { |
||
175 | 1 | return self::getInstance()->getRequestUri(); |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * @return string |
||
180 | */ |
||
181 | 7 | public function getRequestUri() |
|
182 | { |
||
183 | 7 | return array_key_exists('REQUEST_URI', $this->server) ? $this->server['REQUEST_URI'] : ''; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * Método que devuelve el idioma de la petición |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getLanguage() |
||
191 | { |
||
192 | return array_key_exists('HTTP_ACCEPT_LANGUAGE', $this->server) ? $this->server['HTTP_ACCEPT_LANGUAGE'] : 'es_ES'; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Método que determina si se ha solicitado un fichero |
||
197 | * @return boolean |
||
198 | */ |
||
199 | 4 | public function isFile() |
|
200 | { |
||
201 | 4 | return preg_match('/\.[a-z0-9]{2,4}$/', $this->getRequestUri()) !== 0; |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * Get query params |
||
206 | * |
||
207 | * @param string $queryParams |
||
208 | * |
||
209 | * @return mixed |
||
210 | */ |
||
211 | public function getQuery($queryParams) |
||
212 | { |
||
213 | return array_key_exists($queryParams, $this->query) ? $this->query[$queryParams] : null; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Get all query params |
||
218 | * |
||
219 | * @return mixed |
||
220 | */ |
||
221 | 1 | public function getQueryParams() |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * Método que devuelve un parámetro de la solicitud |
||
228 | * @param string $param |
||
229 | * |
||
230 | * @return string|null |
||
231 | */ |
||
232 | public function get($param) |
||
233 | { |
||
234 | return array_key_exists($param, $this->data) ? $this->data[$param] : null; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Método que devuelve todos los datos del Request |
||
239 | * @return array |
||
240 | */ |
||
241 | 1 | public function getData() |
|
242 | { |
||
243 | 1 | return array_merge($this->data, $this->raw); |
|
244 | } |
||
245 | |||
246 | /** |
||
247 | * @return array |
||
248 | */ |
||
249 | public function getRawData() { |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Método que realiza una redirección a la url dada |
||
255 | * @param string $url |
||
256 | */ |
||
257 | public function redirect($url = null) |
||
258 | { |
||
259 | if (null === $url) { |
||
260 | $url = $this->getServer('HTTP_ORIGIN'); |
||
261 | } |
||
262 | ob_start(); |
||
263 | header('Location: ' . $url); |
||
264 | ob_end_clean(); |
||
265 | Security::getInstance()->updateSession(); |
||
266 | exit(t('Redireccionando...')); |
||
1 ignored issue
–
show
|
|||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Devuelve un parámetro de $_SERVER |
||
271 | * @param string $param |
||
272 | * @param $default |
||
273 | * @return string|null |
||
274 | */ |
||
275 | 5 | public function getServer($param, $default = null) |
|
276 | { |
||
277 | 5 | return array_key_exists($param, $this->server) ? $this->server[$param] : $default; |
|
278 | } |
||
279 | |||
280 | /** |
||
281 | * Devuelve el nombre del servidor |
||
282 | * @return string|null |
||
283 | */ |
||
284 | 2 | public function getServerName() |
|
285 | { |
||
286 | 2 | return $this->getServer('SERVER_NAME'); |
|
287 | } |
||
288 | |||
289 | /** |
||
290 | * Devuelve el protocolo de la conexión |
||
291 | * @return string |
||
292 | */ |
||
293 | 2 | public function getProtocol() |
|
294 | { |
||
295 | 2 | return ($this->getServer('HTTPS') || $this->getServer('https')) ? 'https://' : 'http://'; |
|
296 | } |
||
297 | |||
298 | /** |
||
299 | * Devuelve la url completa de base |
||
300 | * @param boolean $hasProtocol |
||
301 | * @return string |
||
302 | */ |
||
303 | 2 | public function getRootUrl($hasProtocol = true) |
|
304 | { |
||
305 | 2 | $url = $this->getServerName(); |
|
306 | 2 | $protocol = $hasProtocol ? $this->getProtocol() : ''; |
|
307 | 2 | if (!empty($protocol)) { |
|
308 | 2 | $url = $protocol . $url; |
|
309 | } |
||
310 | 2 | if (!in_array($this->getServer('SERVER_PORT'), [80, 443], true)) { |
|
311 | 2 | $url .= ':' . $this->getServer('SERVER_PORT'); |
|
312 | } |
||
313 | 2 | return $url; |
|
314 | } |
||
315 | |||
316 | /** |
||
317 | * Método que devuelve el valor de una cookie en caso de que exista |
||
318 | * @param string $name |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | 1 | public function getCookie($name) |
|
323 | { |
||
324 | 1 | return array_key_exists($name, $this->cookies) ? $this->cookies[$name] : null; |
|
325 | } |
||
326 | |||
327 | /** |
||
328 | * Método que devuelve los files subidos por POST |
||
329 | * @param $name |
||
330 | * |
||
331 | * @return array |
||
332 | */ |
||
333 | public function getFile($name) |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Método que devuelve si la petición es ajax o no |
||
340 | * @return boolean |
||
341 | */ |
||
342 | 1 | public function isAjax() |
|
346 | } |
||
347 | |||
348 | } |
||
349 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.