Passed
Push — master ( d190a9...d6bddc )
by Stefan
02:02
created

Request::redirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Nono;
4
5
/**
6
 * Simple request class, which is passed as the first argument to
7
 * any callable / controller method associated with a route.
8
 *
9
 * @method string|mixed get(string $name = null, mixed $default = null)
10
 * @method string|mixed post(string $name = null, mixed $default = null)
11
 * @method string|mixed request(string $name = null, mixed $default = null)
12
 * @method string|mixed server(string $name = null, mixed $default = null)
13
 * @method string|mixed session(string $name = null, mixed $default = null)
14
 * @method string|mixed cookie(string $name = null, mixed $default = null)
15
 * @method array|mixed  files(string $name = null, mixed $default = null)
16
 */
17
class Request
18
{
19
    /**
20
     * Just triggering the super global.
21
     */
22 18
    public function __construct()
0 ignored issues
show
Coding Style introduced by
__construct 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...
23
    {
24 18
        $_SERVER;
25 18
    }
26
27
    /**
28
     * Return URI with query string.
29
     *
30
     * @return string
31
     */
32 5
    public function uri()
33
    {
34 5
        return urldecode($this->plainUri());
35
    }
36
37
    /**
38
     * Return URI without query string.
39
     *
40
     * @return string
41
     */
42 5
    public function plainUri()
43
    {
44 5
        return explode('?', $this->server('REQUEST_URI'))[0];
45
    }
46
47
    /**
48
     * Return the http verb.
49
     *
50
     * @return string
51
     */
52 5
    public function method()
53
    {
54 5
        return $this->server('REQUEST_METHOD');
55
    }
56
57
    /**
58
     * Return whether the request URL is HTTPS URL or not.
59
     * Questionable reliability -> depends upon server config.
60
     *
61
     * @return string
62
     */
63 1
    public function isHttps()
64
    {
65 1
        return $this->server('SERVER_PORT') == 443
66 1
            || strtoupper($this->server('HTTPS')) == 'ON';
67
    }
68
69
    /**
70
     * Return the hostname.
71
     *
72
     * @return string
73
     */
74 1
    public function host()
75
    {
76 1
        return $this->server('HTTP_HOST');
77
    }
78
79
    /**
80
     * Return the time the request was initiated.
81
     *
82
     * @return float
83
     */
84 1
    public function requestTimeFloat()
85
    {
86 1
        return $this->server('REQUEST_TIME_FLOAT');
87
    }
88
89
    /**
90
     * Return the elapsed time since the request was initiated.
91
     *
92
     * @return float
93
     */
94 1
    public function elapsedRequestTimeFloat()
95
    {
96 1
        return microtime(1) - $this->server('REQUEST_TIME_FLOAT');
97
    }
98
99
    /**
100
     * Allow redirects when headers have already been sent
101
     * due to sessions or other output.
102
     *
103
     * @param string $url
104
     */
105 1
    public function redirect($url)
106
    {
107 1
        echo "<script>location.replace('$url');</script>";
108 1
    }
109
110
    /**
111
     * Magic method to access super globals with optional default argument.
112
     * Calling without arguments e.g. $request->server() will simply
113
     * return the entire _SERVER global.
114
     *
115
     * @param string $name
116
     * @param array  $args
117
     * @return mixed
118
     */
119 16
    public function __call($name, $args)
0 ignored issues
show
Coding Style introduced by
__call uses the super-global variable $GLOBALS 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...
120
    {
121 16
        $global = $GLOBALS['_' . strtoupper($name)];
122 16
        $default = isset($args[1]) ? $args[1] : null;
123
124 16
        if (!count($args)) {
125 1
            return $global;
126 15
        } elseif (isset($global[$args[0]])) {
127 13
            return $global[$args[0]];
128
        } else {
129 2
            return $default;
130
        }
131
    }
132
}
133
134