Passed
Branch issue_11 (3b88d6)
by Jay
13:12 queued 23s
created

Http::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace FMUP\Request;
3
4
use FMUP\Request;
5
6
class Http extends Request
7
{
8
    const HTTP_X_REQUESTED_WITH = 'HTTP_X_REQUESTED_WITH';
9
    const HTTP_X_REQUESTED_WITH_AJAX = 'XMLHttpRequest';
10
11
    const REQUEST_URI = 'REQUEST_URI';
12
    const REQUEST_SCHEME = 'REQUEST_SCHEME';
13
    const QUERY_STRING = 'QUERY_STRING';
14
15
    const REQUEST_METHOD = 'REQUEST_METHOD';
16
    const REQUEST_METHOD_GET = 'GET';
17
    const REQUEST_METHOD_POST = 'POST';
18
19
    const SERVER_NAME = 'SERVER_NAME';
20
    const HTTP_REFERER = 'HTTP_REFERER';
21
    const HTTP_HOST = 'HTTP_HOST';
22
23
    protected $get = array();
24
    protected $post = array();
25
    protected $server = array();
26
    protected $files = array();
27
28
    /**
29
     * mapped to HTTP values
30
     */
31 25
    public function __construct()
0 ignored issues
show
Coding Style introduced by
__construct 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...
Coding Style introduced by
__construct uses the super-global variable $_POST 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
__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...
Coding Style introduced by
__construct 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...
32
    {
33 25
        $this->get = $_GET;
34 25
        $this->post = $_POST;
35 25
        $this->server = $_SERVER;
36 25
        $this->files = $_FILES;
37 25
    }
38
39
    /**
40
     * Get a value in the request depending HTTP method
41
     * @param string $name Name to retrieve
42
     * @param mixed $defaultValue Value returned if name is not defined in query
43
     * @return mixed
44
     */
45 1
    public function get($name, $defaultValue = null)
46
    {
47 1
        return ($this->getMethod() == self::REQUEST_METHOD_POST)
48 1
            ? $this->getPost($name, $defaultValue)
49 1
            : $this->getGet($name, $defaultValue);
50
    }
51
52
    /**
53
     * Check if param exists
54
     * @param string $name
55
     * @return bool
56
     */
57 1
    public function has($name)
58
    {
59 1
        return ($this->getMethod() == self::REQUEST_METHOD_POST)
60 1
            ? $this->hasPost($name)
61 1
            : $this->hasGet($name);
62
    }
63
64
    /**
65
     * Return requested value in GET method
66
     * @param string $name Name to retrieve
67
     * @param mixed $defaultValue Value returned if name is not defined in query
68
     * @return mixed
69
     */
70 2
    public function getGet($name, $defaultValue = null)
71
    {
72 2
        return $this->hasGet($name) ? $this->get[$name] : $defaultValue;
73
    }
74
75
    /**
76
     * Return requested value in POST method
77
     * @param string $name Name to retrieve
78
     * @param mixed $defaultValue Value returned if name is not defined in query
79
     * @return mixed
80
     */
81 2
    public function getPost($name, $defaultValue = null)
82
    {
83 2
        return $this->hasPost($name) ? $this->post[$name] : $defaultValue;
84
    }
85
86
    /**
87
     * Retrieve a value defined on server
88
     * @param string $name Name to retrieve
89
     * @param mixed $defaultValue Value returned if name is not defined in query
90
     * @return mixed
91
     */
92 7
    public function getServer($name, $defaultValue = null)
93
    {
94 7
        return $this->hasServer($name) ? $this->server[$name] : $defaultValue;
95
    }
96
97
    /**
98
     * Retrieve a value defined on files
99
     * @param int $name
100
     * @param mixed $defaultValue Value returned if name is not defined in query
101
     * @return mixed
102
     */
103 1
    public function getFiles($name, $defaultValue = null)
104
    {
105 1
        return $this->hasFiles($name) ? $this->files[$name] : $defaultValue;
106
    }
107
108
    /**
109
     * Check whether request is an Ajax request
110
     * @return bool
111
     */
112 1
    public function isAjax()
113
    {
114 1
        $requestClient = $this->getServer(self::HTTP_X_REQUESTED_WITH);
115 1
        return (strtolower($requestClient) == strtolower(self::HTTP_X_REQUESTED_WITH_AJAX));
116
    }
117
118
    /**
119
     * Get HTTP method (GET/POST/DELETE...)
120
     * @return string
121
     */
122 3
    public function getMethod()
123
    {
124 3
        return $this->getServer(self::REQUEST_METHOD);
125
    }
126
127
    /**
128
     * Retrieve called URI
129
     * @param bool $withQuerySting must return query string in the request
130
     * @return string
131
     */
132 1
    public function getRequestUri($withQuerySting = false)
133
    {
134 1
        $requestUri = $this->getServer(self::REQUEST_URI);
135
        return (string)(
136 1
            $withQuerySting
137 1
            ? $requestUri
138 1
            : str_replace('?' . $this->getServer(self::QUERY_STRING), '', $requestUri)
139
        );
140
    }
141
142
    /**
143
     * Define a POST value
144
     * @param string $name Name to define
145
     * @param mixed $value Value to define
146
     * @return $this
147
     */
148 1 View Code Duplication
    public function setPostValue($name, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
setPostValue uses the super-global variable $_POST 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
setPostValue 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...
149
    {
150 1
        $this->post[$name] = $value;
151
        //for compliance purpose only - do not use
152 1
        $_POST[$name] = $value;
153 1
        $_REQUEST[$name] = $value;
154 1
        return $this;
155
    }
156
157
    /**
158
     * Define a GET value
159
     * @param string $name Name to define
160
     * @param mixed $value Value to define
161
     * @return $this
162
     */
163 1 View Code Duplication
    public function setGetValue($name, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
setGetValue 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...
Coding Style introduced by
setGetValue 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...
164
    {
165 1
        $this->get[$name] = $value;
166
        //for compliance purpose only - do not use
167 1
        $_GET[$name] = $value;
168 1
        $_REQUEST[$name] = $value;
169 1
        return $this;
170
    }
171
172
    /**
173
     * Check whether selected post exists
174
     * @param string $name
175
     * @return bool
176
     */
177 3
    public function hasPost($name)
178
    {
179 3
        return array_key_exists($name, $this->post);
180
    }
181
182
    /**
183
     * Check whether selected get exists
184
     * @param string $name
185
     * @return bool
186
     */
187 3
    public function hasGet($name)
188
    {
189 3
        return array_key_exists($name, $this->get);
190
    }
191
192
    /**
193
     * Check whether selected server value exists
194
     * @param string $name
195
     * @return bool
196
     */
197 7
    public function hasServer($name)
198
    {
199 7
        return array_key_exists($name, $this->server);
200
    }
201
202
    /**
203
     * Check whether selected files value exists
204
     * @param string $name
205
     * @return bool
206
     */
207 1
    public function hasFiles($name)
208
    {
209 1
        return array_key_exists($name, $this->files);
210
    }
211
212
    /**
213
     * Return request referer
214
     * @return string
215
     */
216 1
    public function getReferer()
217
    {
218 1
        return $this->getServer(self::HTTP_REFERER);
219
    }
220
221
    /**
222
     * Return request headers
223
     * @codeCoverageIgnore
224
     * @return array
225
     */
226
    public function getHeaders()
227
    {
228
        return (array) (function_exists('getallheaders') ? getallheaders() : false);
229
    }
230
}
231