Completed
Push — master ( eda34b...67914c )
by Matthias
02:34
created

WebRequest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/*
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2015 zepi
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 *
25
 */
26
27
/**
28
 * The WebRequest representates a web framework request.
29
 * 
30
 * @package Zepi\Turbo\Request
31
 * @author Matthias Zobrist <[email protected]>
32
 * @copyright Copyright (c) 2015 zepi
33
 */
34
35
namespace Zepi\Turbo\Request;
36
37
use \Zepi\Turbo\FrameworkInterface\SessionInterface;
38
39
/**
40
 * The WebRequest representates a web framework request.
41
 * 
42
 * @author Matthias Zobrist <[email protected]>
43
 * @copyright Copyright (c) 2015 zepi
44
 */
45
class WebRequest extends RequestAbstract
46
{
47
    /**
48
     * @access protected
49
     * @var string
50
     */
51
    protected $_requestedUrl;
52
    
53
    /**
54
     * @access protected
55
     * @var array
56
     */
57
    protected $_headers;
58
    
59
    /**
60
     * @access protected
61
     * @var string
62
     */
63
    protected $_protocol;
64
    
65
    /**
66
     * @access protected
67
     * @var boolean
68
     */
69
    protected $_isSsl = false;
70
    
71
    /**
72
     * @access protected
73
     * @var \Zepi\Turbo\FrameworkInterface\SessionInterface
74
     */
75
    protected $_session = null;
76
    
77
    /**
78
     * Constructs the object
79
     * 
80
     * @access public
81
     * @param string $requestedUrl
82
     * @param string $route
83
     * @param array params
84
     * @param string $base
85
     * @param string $locale
86
     * @param boolean $isSsl
87
     * @param array $headers
88
     * @param string $protocol
89
     * @param array $data
90
     */
91
    public function __construct($requestedUrl, $route, $params, $base, $locale, $isSsl, $headers, $protocol, $data = array())
92
    {
93
        parent::__construct($route, $params, $base, $locale, $data);
94
        
95
        $this->_requestedUrl = $requestedUrl;
96
        $this->_isSsl = $isSsl;
97
        $this->_headers = $headers;
98
        $this->_protocol = $protocol;
99
    }
100
    
101
    /**
102
     * Returns the requested url
103
     * 
104
     * @access public
105
     * @return string
106
     */
107
    public function getRequestedUrl()
108
    {
109
        return $this->_requestedUrl;
110
    }
111
    
112
    /**
113
     * Returns the delimitier, which is used to split the route
114
     * into parts.
115
     * The delimiter for the html request is the slash (/).
116
     * 
117
     * @access public
118
     * @return string
119
     */
120
    public function getRouteDelimiter()
121
    {
122
        return '/';
123
    }
124
    
125
    /**
126
     * Saves the given value for the given key in the session data
127
     * 
128
     * @access public
129
     * @param string $key
130
     * @param mixed $value
131
     */
132
    public function setSessionData($key, $value)
1 ignored issue
show
Coding Style introduced by
setSessionData uses the super-global variable $_SESSION 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...
133
    {
134
        $_SESSION[$key] = $value;
135
    }
136
    
137
    /**
138
     * Returns the session value of the given key.
139
     * 
140
     * @access public
141
     * @param string $key
142
     * @return mixed
143
     */
144
    public function getSessionData($key = '')
1 ignored issue
show
Coding Style introduced by
getSessionData uses the super-global variable $_SESSION 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...
145
    {
146
        if (!isset($_SESSION[$key])) {
147
            return false;
148
        }
149
        
150
        return $_SESSION[$key];
151
    }
152
    
153
    /**
154
     * Deletes the value for the given key
155
     * 
156
     * @access public
157
     * @param string $key
158
     * @return boolean
159
     */
160
    public function deleteSessionData($key)
1 ignored issue
show
Coding Style introduced by
deleteSessionData uses the super-global variable $_SESSION 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...
161
    {
162
        if (!isset($_SESSION[$key])) {
163
            return false;
164
        }
165
        
166
        unset($_SESSION[$key]);
167
        return true;
168
    }
169
    
170
    /**
171
     * Removes all session data
172
     * 
173
     * @access public
174
     */
175
    public function clearSessionData()
1 ignored issue
show
Coding Style introduced by
clearSessionData uses the super-global variable $_SESSION 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...
176
    {
177
        foreach ($_SESSION as $key => $value) {
178
            unset($_SESSION[$key]);
179
        }
180
    }
181
    
182
    /**
183
     * Returns the cookie value of the given key.
184
     * 
185
     * @access public
186
     * @param string $key
187
     * @return mixed
188
     */
189
    public function getCookieData($key = '')
1 ignored issue
show
Coding Style introduced by
getCookieData 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...
190
    {
191
        if (!isset($_COOKIE[$key])) {
192
            return false;
193
        }
194
        
195
        return $_COOKIE[$key];
196
    }
197
    
198
    /**
199
     * Returns true if this request was made over a 
200
     * secure connection trough ssl.
201
     * 
202
     * @access public
203
     * @return boolean
204
     */
205
    public function isSsl()
206
    {
207
        return ($this->_isSsl);
208
    }
209
    
210
    /**
211
     * Adds a session object to the request
212
     * 
213
     * @access public
214
     * @param \Zepi\Turbo\FrameworkInterface\SessionInterface $session
215
     * @return boolean
216
     */
217
    public function setSession(SessionInterface $session)
218
    {
219
        if (!is_object($session) || $this->_session !== null) {
220
            return false;
221
        }
222
        
223
        $this->_session = $session;
224
        
225
        return true;
226
    }
227
    
228
    /**
229
     * Returns true if a session for the given name
230
     * exists. Otherwise returns false.
231
     * 
232
     * @access public
233
     * @return boolean
234
     */
235
    public function hasSession()
236
    {
237
        if ($this->_session === null) {
238
            return false;
239
        }
240
        
241
        return true;
242
    }
243
    
244
    /**
245
     * Returns the session
246
     * 
247
     * @access public
248
     * @return false|\Zepi\Turbo\FrameworkInterface\SessionInterface
249
     */
250
    public function getSession()
251
    {
252
        if ($this->_session === null) {
253
            return false;
254
        }
255
        
256
        return $this->_session;
257
    }
258
    
259
    /**
260
     * Removes the session
261
     * 
262
     * @access public
263
     */
264
    public function removeSession()
265
    {
266
        $this->_session = null;
267
        $this->clearSessionData();
268
    }
269
    
270
    /**
271
     * Returns the value for the given header key
272
     * 
273
     * @access public
274
     * @param string $key
275
     * @return false|mixed
276
     */
277
    public function getHeader($key)
278
    {
279
        if (!isset($this->_headers[$key])) {
280
            return false;
281
        }
282
        
283
        return $this->_headers[$key];
284
    }
285
    
286
    /**
287
     * Returns an array with all headers
288
     * 
289
     * @access public
290
     * @return array
291
     */
292
    public function getHeaders()
293
    {
294
        return $this->_headers;
295
    }
296
    
297
    /**
298
     * Returns the protocol of the request
299
     * 
300
     * @access public
301
     * @return string
302
     */
303
    public function getProtocol()
304
    {
305
        return $this->_protocol;
306
    }
307
}
308