Completed
Push — master ( 6b7617...800138 )
by Matthias
03:06
created

WebRequest::deleteSessionData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
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 boolean $isSsl
99
     * @param array $headers
100
     * @param string $protocol
101
     * @param array $data
102
     */
103
    public function __construct($method, $requestedUrl, $route, $params, $base, $locale, $isSsl, $headers, $protocol, $data = array())
104
    {
105
        parent::__construct($route, $params, $base, $locale, $data);
106
        
107
        $this->_method = $method;
108
        $this->_requestedUrl = $requestedUrl;
109
        $this->_isSsl = $isSsl;
110
        $this->_headers = $headers;
111
        $this->_protocol = $protocol;
112
    }
113
    
114
    /**
115
     * Returns the method of the request
116
     * 
117
     * @access public
118
     * @return string
119
     */
120
    public function getMethod()
121
    {
122
        return $this->_method;
123
    }
124
    
125
    /**
126
     * Returns the requested url
127
     * 
128
     * @access public
129
     * @return string
130
     */
131
    public function getRequestedUrl()
132
    {
133
        return $this->_requestedUrl;
134
    }
135
    
136
    /**
137
     * Returns the delimitier, which is used to split the route
138
     * into parts.
139
     * The delimiter for the html request is the slash (/).
140
     * 
141
     * @access public
142
     * @return string
143
     */
144
    public function getRouteDelimiter()
145
    {
146
        return '/';
147
    }
148
    
149
    /**
150
     * Saves the given value for the given key in the session data
151
     * 
152
     * @access public
153
     * @param string $key
154
     * @param mixed $value
155
     */
156
    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...
157
    {
158
        $_SESSION[$key] = $value;
159
    }
160
    
161
    /**
162
     * Returns the session value of the given key.
163
     * 
164
     * @access public
165
     * @param string $key
166
     * @return mixed
167
     */
168
    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...
169
    {
170
        if (!isset($_SESSION[$key])) {
171
            return false;
172
        }
173
        
174
        return $_SESSION[$key];
175
    }
176
    
177
    /**
178
     * Deletes the value for the given key
179
     * 
180
     * @access public
181
     * @param string $key
182
     * @return boolean
183
     */
184
    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...
185
    {
186
        if (!isset($_SESSION[$key])) {
187
            return false;
188
        }
189
        
190
        unset($_SESSION[$key]);
191
        return true;
192
    }
193
    
194
    /**
195
     * Removes all session data
196
     * 
197
     * @access public
198
     */
199
    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...
200
    {
201
        foreach ($_SESSION as $key => $value) {
202
            unset($_SESSION[$key]);
203
        }
204
    }
205
    
206
    /**
207
     * Returns the cookie value of the given key.
208
     * 
209
     * @access public
210
     * @param string $key
211
     * @return mixed
212
     */
213
    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...
214
    {
215
        if (!isset($_COOKIE[$key])) {
216
            return false;
217
        }
218
        
219
        return $_COOKIE[$key];
220
    }
221
    
222
    /**
223
     * Returns true if this request was made over a 
224
     * secure connection trough ssl.
225
     * 
226
     * @access public
227
     * @return boolean
228
     */
229
    public function isSsl()
230
    {
231
        return ($this->_isSsl);
232
    }
233
    
234
    /**
235
     * Adds a session object to the request
236
     * 
237
     * @access public
238
     * @param \Zepi\Turbo\FrameworkInterface\SessionInterface $session
239
     * @return boolean
240
     */
241
    public function setSession(SessionInterface $session)
242
    {
243
        if (!is_object($session) || $this->_session !== null) {
244
            return false;
245
        }
246
        
247
        $this->_session = $session;
248
        
249
        return true;
250
    }
251
    
252
    /**
253
     * Returns true if a session for the given name
254
     * exists. Otherwise returns false.
255
     * 
256
     * @access public
257
     * @return boolean
258
     */
259
    public function hasSession()
260
    {
261
        if ($this->_session === null) {
262
            return false;
263
        }
264
        
265
        return true;
266
    }
267
    
268
    /**
269
     * Returns the session
270
     * 
271
     * @access public
272
     * @return false|\Zepi\Turbo\FrameworkInterface\SessionInterface
273
     */
274
    public function getSession()
275
    {
276
        if ($this->_session === null) {
277
            return false;
278
        }
279
        
280
        return $this->_session;
281
    }
282
    
283
    /**
284
     * Removes the session
285
     * 
286
     * @access public
287
     */
288
    public function removeSession()
289
    {
290
        $this->_session = null;
291
        $this->clearSessionData();
292
    }
293
    
294
    /**
295
     * Returns the value for the given header key
296
     * 
297
     * @access public
298
     * @param string $key
299
     * @return false|mixed
300
     */
301
    public function getHeader($key)
302
    {
303
        if (!isset($this->_headers[$key])) {
304
            return false;
305
        }
306
        
307
        return $this->_headers[$key];
308
    }
309
    
310
    /**
311
     * Returns an array with all headers
312
     * 
313
     * @access public
314
     * @return array
315
     */
316
    public function getHeaders()
317
    {
318
        return $this->_headers;
319
    }
320
    
321
    /**
322
     * Returns the protocol of the request
323
     * 
324
     * @access public
325
     * @return string
326
     */
327
    public function getProtocol()
328
    {
329
        return $this->_protocol;
330
    }
331
}
332