Test Failed
Push — master ( fc5a43...510786 )
by Fran
04:03
created

Request.php ➔ getallheaders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 3
1
<?php
2
namespace PSFS\base;
3
4
use PSFS\base\types\traits\SingletonTrait;
5
6
/**
7
 * Class Request
8
 * @package PSFS
9
 */
10 2
class Request
11 2
{
12 2
    use SingletonTrait;
13 2
    protected $server;
14 2
    protected $cookies;
15
    protected $upload;
16
    protected $header;
17
    protected $data;
18
    protected $query;
19
    private $isLoaded = false;
20
21
    public function init()
0 ignored issues
show
Coding Style introduced by
init uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
init uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
init uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
init uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
init uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
22
    {
23
        $this->server = $_SERVER or [];
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
24
        $this->cookies = $_COOKIE or [];
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
25
        $this->upload = $_FILES or [];
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
26
        $this->header = $this->parseHeaders();
27
        $this->data = $_REQUEST or [];
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
28
        $this->query = $_GET or [];
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
29
        $contentType = $this->getHeader('Content-Type');
30
        if(null === $contentType) {
31
            $contentType = $this->getServer('CONTENT_TYPE') ?: 'text/html';
32
        }
33 2
        if (preg_match('/application\/json/i', $contentType)) {
34
            $this->data += json_decode(file_get_contents("php://input"), true) ?: array();
35 2
        }
36 2
        $this->isLoaded = true;
37 2
    }
38 2
39 2
    /**
40 2
     * @return bool
41 2
     */
42 2
    public function isLoaded() {
43 2
        return $this->isLoaded;
44 2
    }
45 2
46
    /**
47
     * Método que devuelve las cabeceras de la petición
48 2
     * @return array
49 2
     */
50
    private function parseHeaders()
51
    {
52
        return getallheaders();
53
    }
54 1
55 1
    /**
56
     * Método que verifica si existe una cabecera concreta
57
     * @param $header
58
     *
59
     * @return boolean
60
     */
61
    public function hasHeader($header)
62 2
    {
63
        return array_key_exists($header, $this->header);
64 2
    }
65
66
67
    /**
68
     * Método que indica si una petición tiene cookies
69
     * @return boolean
70
     */
71
    public function hasCookies()
72
    {
73 3
        return (null !== $this->cookies && 0 !== count($this->cookies));
74
    }
75 3
76
    /**
77
     * Método que indica si una petición tiene cookies
78
     * @return boolean
79
     */
80
    public function hasUpload()
81
    {
82
        return (null !== $this->upload && 0 !== count($this->upload));
83 1
    }
84
85 1
    /**
86
     * Método que devuelve el TimeStamp de la petición
87
     *
88
     * @param boolean $formatted
89
     *
90
     * @return string
91
     */
92 1
    public static function ts($formatted = false)
93
    {
94 1
        return self::getInstance()->getTs($formatted);
95
    }
96
97
    public function getTs($formatted = false)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
98
    {
99
        return ($formatted) ? date('Y-m-d H:i:s', $this->server['REQUEST_TIME_FLOAT']) : $this->server['REQUEST_TIME_FLOAT'];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
100
    }
101
102
    /**
103
     * Método que devuelve el Método HTTP utilizado
104
     * @return string
105
     */
106
    public function getMethod()
107
    {
108
        return (array_key_exists('REQUEST_METHOD', $this->server)) ? strtoupper($this->server['REQUEST_METHOD']) : 'GET';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
109 1
    }
110
111 1
    /**
112
     * Método que devuelve una cabecera de la petición si existe
113
     * @param $name
114
     *
115
     * @return string|null
116
     */
117
    public static function header($name)
118 3
    {
119
        return self::getInstance()->getHeader($name);
120 3
    }
121
122
    public function getHeader($name)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
123
    {
124
        $header = null;
125
        if ($this->hasHeader($name)) {
126
            $header = $this->header[$name];
127
        }
128
        return $header;
129
    }
130
131
    /**
132
     * Método que devuelve la url solicitada
133
     * @return string|null
134 2
     */
135
    public static function requestUri()
136 2
    {
137 2
        return self::getInstance()->getRequestUri();
138
    }
139
140 2
    /**
141
     * @return string
142
     */
143
    public function getRequestUri()
144
    {
145
        return array_key_exists('REQUEST_URI', $this->server) ? $this->server['REQUEST_URI'] : '';
146
    }
147
148
    /**
149
     * Método que devuelve el idioma de la petición
150
     * @return string
151
     */
152
    public function getLanguage()
153
    {
154
        return array_key_exists('HTTP_ACCEPT_LANGUAGE', $this->server) ? $this->server['HTTP_ACCEPT_LANGUAGE'] : 'es_ES';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
155 6
    }
156
157 6
    /**
158
     * Método que determina si se ha solicitado un fichero
159
     * @return boolean
160
     */
161
    public function isFile()
162
    {
163
        $file = (preg_match('/\.[a-z0-9]{2,4}$/', $this->getRequestUri()) !== 0);
164
        return $file;
165
    }
166
167
    /**
168
     * Get query params
169
     *
170
     * @param string $queryParams
171
     *
172
     * @return mixed
173 4
     */
174
    public function getQuery($queryParams)
175 4
    {
176 4
        return (array_key_exists($queryParams, $this->query)) ? $this->query[$queryParams] : null;
177
    }
178
179
    /**
180
     * Get all query params
181
     *
182
     * @return mixed
183
     */
184
    public function getQueryParams()
185
    {
186
        return $this->query;
187
    }
188
189
    /**
190
     * Método que devuelve un parámetro de la solicitud
191
     * @param string $param
192
     *
193
     * @return string|null
194
     */
195
    public function get($param)
196
    {
197
        return (array_key_exists($param, $this->data)) ? $this->data[$param] : null;
198
    }
199
200
    /**
201
     * Método que devuelve todos los datos del Request
202
     * @return array
203
     */
204
    public function getData()
205
    {
206
        return $this->data;
207
    }
208
209
    /**
210
     * Método que realiza una redirección a la url dada
211
     * @param string $url
212
     */
213
    public function redirect($url = null)
214
    {
215
        if (null === $url) $url = $this->getServer('HTTP_ORIGIN');
216
        ob_start();
217
        header('Location: ' . $url);
218
        ob_end_clean();
219
        Security::getInstance()->updateSession();
220
        exit(_("Redireccionando..."));
221
    }
222
223
    /**
224
     * Devuelve un parámetro de $_SERVER
225
     * @param string $param
226
     *
227
     * @return string|null
228
     */
229
    public function getServer($param)
230
    {
231
        return array_key_exists($param, $this->server) ? $this->server[$param] : null;
232
    }
233
234
    /**
235
     * Devuelve el nombre del servidor
236
     * @return string|null
237
     */
238
    public function getServerName()
239
    {
240
        return $this->getServer("SERVER_NAME");
241 3
    }
242
243 3
    /**
244
     * Devuelve el protocolo de la conexión
245
     * @return string
246
     */
247
    public function getProtocol()
248
    {
249
        return ($this->getServer("HTTPS") || $this->getServer("https")) ? 'https://' : 'http://';
250
    }
251
252
    /**
253
     * Devuelve la url completa de base
254
     * @param boolean $protocol
255
     * @return string
256
     */
257
    public function getRootUrl($protocol = true)
258
    {
259
        $host = $this->getServerName();
260
        $protocol = $protocol ? $this->getProtocol() : '';
261
        $url = '';
262
        if (!empty($host) && !empty($protocol)) $url = $protocol . $host;
263
        if (!in_array($this->getServer('SERVER_PORT'), [80, 443])) {
264
            $url .= ':' . $this->getServer('SERVER_PORT');
265
        }
266
        return $url;
267
    }
268
269
    /**
270
     * Método que devuelve el valor de una cookie en caso de que exista
271
     * @param string $name
272
     *
273
     * @return string
274
     */
275
    public function getCookie($name)
276
    {
277
        return array_key_exists($name, $this->cookies) ? $this->cookies[$name] : null;
278
    }
279
280
    /**
281
     * Método que devuelve los files subidos por POST
282
     * @param $name
283
     *
284
     * @return array
285
     */
286
    public function getFile($name)
287 1
    {
288
        return array_key_exists($name, $this->upload) ? $this->upload[$name] : array();
289 1
    }
290
291
    /**
292
     * Método que devuelve si la petición es ajax o no
293
     * @return boolean
294
     */
295
    public function isAjax()
296
    {
297
        $requested = $this->getServer("HTTP_X_REQUESTED_WITH");
298
        return (null !== $requested && strtolower($requested) == 'xmlhttprequest');
299
    }
300
301
}
302