WebRequest::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 11
crap 1

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
    const METHOD_GET = 'GET';
48
    const METHOD_POST = 'POST';
49
    const METHOD_PUT = 'PUT';
50
    const METHOD_DELETE = 'DELETE';
51
    
52
    /**
53
     * @access protected
54
     * @var string
55
     */
56
    protected $method;
57
    
58
    /**
59
     * @access protected
60
     * @var string
61
     */
62
    protected $requestedUrl;
63
    
64
    /**
65
     * @access protected
66
     * @var array
67
     */
68
    protected $headers;
69
    
70
    /**
71
     * @access protected
72
     * @var string
73
     */
74
    protected $protocol;
75
    
76
    /**
77
     * @access protected
78
     * @var boolean
79
     */
80
    protected $isSsl = false;
81
    
82
    /**
83
     * @access protected
84
     * @var \Zepi\Turbo\FrameworkInterface\SessionInterface
85
     */
86
    protected $session = null;
87
    
88
    /**
89
     * Constructs the object
90
     * 
91
     * @access public
92
     * @param string $method
93
     * @param string $requestedUrl
94
     * @param string $route
95
     * @param array params
96
     * @param string $base
97
     * @param string $locale
98
     * @param string $operatingSystem
99
     * @param boolean $isSsl
100
     * @param array $headers
101
     * @param string $protocol
102
     * @param array $data
103
     */
104 21
    public function __construct($method, $requestedUrl, $route, $params, $base, $locale, $operatingSystem, $isSsl, $headers, $protocol, $data = array())
105
    {
106 21
        parent::__construct($route, $params, $base, $locale, $operatingSystem, $data);
107
        
108 21
        $this->method = $method;
109 21
        $this->requestedUrl = $requestedUrl;
110 21
        $this->isSsl = $isSsl;
111 21
        $this->headers = $headers;
112 21
        $this->protocol = $protocol;
113 21
    }
114
    
115
    /**
116
     * Returns the method of the request
117
     * 
118
     * @access public
119
     * @return string
120
     */
121
    public function getMethod()
122
    {
123
        return $this->method;
124
    }
125
    
126
    /**
127
     * Returns the requested url
128
     * 
129
     * @access public
130
     * @return string
131
     */
132
    public function getRequestedUrl()
133
    {
134
        return $this->requestedUrl;
135
    }
136
    
137
    /**
138
     * Returns the delimitier, which is used to split the route
139
     * into parts.
140
     * The delimiter for the html request is the slash (/).
141
     * 
142
     * @access public
143
     * @return string
144
     */
145 3
    public function getRouteDelimiter()
146
    {
147 3
        return '/';
148
    }
149
    
150
    /**
151
     * Saves the given value for the given key in the session data
152
     * 
153
     * @access public
154
     * @param string $key
155
     * @param mixed $value
156
     */
157 1
    public function setSessionData($key, $value)
158
    {
159 1
        $_SESSION[$key] = $value;
160 1
    }
161
    
162
    /**
163
     * Returns the session value of the given key.
164
     * 
165
     * @access public
166
     * @param string $key
167
     * @return mixed
168
     */
169 3
    public function getSessionData($key = '')
170
    {
171 3
        if (!isset($_SESSION[$key])) {
172 1
            return false;
173
        }
174
        
175 2
        return $_SESSION[$key];
176
    }
177
    
178
    /**
179
     * Deletes the value for the given key
180
     * 
181
     * @access public
182
     * @param string $key
183
     * @return boolean
184
     */
185 2
    public function deleteSessionData($key)
186
    {
187 2
        if (!isset($_SESSION[$key])) {
188 1
            return false;
189
        }
190
        
191 1
        unset($_SESSION[$key]);
192 1
        return true;
193
    }
194
    
195
    /**
196
     * Removes all session data
197
     * 
198
     * @access public
199
     */
200 1
    public function clearSessionData()
201
    {
202 1
        foreach ($_SESSION as $key => $value) {
203
            unset($_SESSION[$key]);
204
        }
205 1
    }
206
    
207
    /**
208
     * Returns the cookie value of the given key.
209
     * 
210
     * @access public
211
     * @param string $key
212
     * @return mixed
213
     */
214 2
    public function getCookieData($key = '')
215
    {
216 2
        if (!isset($_COOKIE[$key])) {
217 1
            return false;
218
        }
219
        
220 1
        return $_COOKIE[$key];
221
    }
222
    
223
    /**
224
     * Returns true if this request was made over a 
225
     * secure connection trough ssl.
226
     * 
227
     * @access public
228
     * @return boolean
229
     */
230 1
    public function isSsl()
231
    {
232 1
        return ($this->isSsl);
233
    }
234
    
235
    /**
236
     * Adds a session object to the request
237
     * 
238
     * @access public
239
     * @param \Zepi\Turbo\FrameworkInterface\SessionInterface $session
240
     * @return boolean
241
     */
242 3
    public function setSession(SessionInterface $session)
243
    {
244 3
        if (!is_object($session) || $this->session !== null) {
245 1
            return false;
246
        }
247
        
248 3
        $this->session = $session;
249
        
250 3
        return true;
251
    }
252
    
253
    /**
254
     * Returns true if a session for the given name
255
     * exists. Otherwise returns false.
256
     * 
257
     * @access public
258
     * @return boolean
259
     */
260 2
    public function hasSession()
261
    {
262 2
        if ($this->session === null) {
263 2
            return false;
264
        }
265
        
266 1
        return true;
267
    }
268
    
269
    /**
270
     * Returns the session
271
     * 
272
     * @access public
273
     * @return false|\Zepi\Turbo\FrameworkInterface\SessionInterface
274
     */
275 3
    public function getSession()
276
    {
277 3
        if ($this->session === null) {
278 1
            return false;
279
        }
280
        
281 3
        return $this->session;
282
    }
283
    
284
    /**
285
     * Removes the session
286
     * 
287
     * @access public
288
     */
289 1
    public function removeSession()
290
    {
291 1
        $this->session = null;
292 1
        $this->clearSessionData();
293 1
    }
294
    
295
    /**
296
     * Returns the value for the given header key
297
     * 
298
     * @access public
299
     * @param string $key
300
     * @return false|mixed
301
     */
302
    public function getHeader($key)
303
    {
304
        if (!isset($this->headers[$key])) {
305
            return false;
306
        }
307
        
308
        return $this->headers[$key];
309
    }
310
    
311
    /**
312
     * Returns an array with all headers
313
     * 
314
     * @access public
315
     * @return array
316
     */
317
    public function getHeaders()
318
    {
319
        return $this->headers;
320
    }
321
    
322
    /**
323
     * Returns the protocol of the request
324
     * 
325
     * @access public
326
     * @return string
327
     */
328
    public function getProtocol()
329
    {
330
        return $this->protocol;
331
    }
332
}
333