Completed
Push — master ( 8afa5e...d79df7 )
by Tim
10s
created

HttpServletRequestWrapper::getRequestedSessionId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * \AppserverIo\Psr\Servlet\Http\HttpServletRequestWrapper
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io-psr/servlet
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Psr\Servlet\Http;
22
23
use AppserverIo\Psr\HttpMessage\PartInterface;
24
use AppserverIo\Psr\HttpMessage\CookieInterface;
25
use AppserverIo\Psr\HttpMessage\RequestInterface;
26
use AppserverIo\Psr\Servlet\ServletRequestWrapper;
27
28
/**
29
 * A servlet request implementation.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2015 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/appserver-io-psr/servlet
35
 * @link      http://www.appserver.io
36
 */
37
class HttpServletRequestWrapper extends ServletRequestWrapper implements HttpServletRequestInterface
38
{
39
40
    /**
41
     * Injects the passed request instance into this servlet request.
42
     *
43
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $request The request instance used for initialization
44
     *
45
     * @return void
46
     */
47 1
    public function injectHttpRequest(HttpServletRequestInterface $request)
48
    {
49 1
        $this->injectRequest($request);
50 1
    }
51
52
    /**
53
     * Returns the base modifier which allows for base path generation within rewritten URL environments
54
     *
55
     * @return string
56
     */
57
    public function getBaseModifier()
58
    {
59
        return $this->getRequest()->getBaseModifier();
60
    }
61
62
    /**
63
     * Returns the application context name (application name prefixed with a slash) for the actual request.
64
     *
65
     * @return string The application context name
66
     */
67
    public function getContextPath()
68
    {
69
        return $this->getRequest()->getContextPath();
70
    }
71
72
    /**
73
     * @return \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface
74
     */
75 1
    public function getRequest()
76
    {
77 1
        return $this->request;
78
    }
79
80
    /**
81
     * Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
82
     *
83
     * @return string
84
     */
85
    public function getRequestUri()
86
    {
87
        return $this->getRequest()->getRequestUri();
88
    }
89
90
    /**
91
     * Returns an part instance
92
     *
93
     * @return \AppserverIo\Psr\HttpMessage\PartInterface
94
     */
95
    public function getHttpPartInstance()
96
    {
97
        return $this->getRequest()->getHttpPartInstance();
98
    }
99
100
    /**
101
     * Returns the URL the client used to make the request.
102
     *
103
     * @return string
104
     */
105
    public function getRequestUrl()
106
    {
107
        return $this->getRequest()->getRequestUrl();
108
    }
109
110
    /**
111
     * Returns the path to the servlet used to handle this request.
112
     *
113
     * @return string The relative path to the servlet
114
     */
115
    public function getServletPath()
116
    {
117
        return $this->getRequest()->getServletPath();
118
    }
119
120
    /**
121
     * Returns the session for this request.
122
     *
123
     * @param boolean $create TRUE to create a new session, else FALSE
124
     *
125
     * @return \AppserverIo\Psr\Servlet\Http\HttpSessionInterface The session instance
126
     */
127
    public function getSession($create = false)
128
    {
129
        return $this->getRequest()->getSession($create);
130
    }
131
132
    /**
133
     * Returns the absolute path info started from the context path.
134
     *
135
     * @return string the absolute path info
136
     */
137
    public function getPathInfo()
138
    {
139
        return $this->getRequest()->getPathInfo();
140
    }
141
142
    /**
143
     * Returns the server name.
144
     *
145
     * @return string The server name
146
     */
147
    public function getServerName()
148
    {
149
        return $this->getRequest()->getServerName();
150
    }
151
152
    /**
153
     * Returns query string of the actual request.
154
     *
155
     * @return string|null The query string of the actual request
156
     */
157 1
    public function getQueryString()
158
    {
159 1
        return $this->getRequest()->getQueryString();
160
    }
161
162
    /**
163
     * Returns header info by given key
164
     *
165
     * @param string $key The header key to get
166
     *
167
     * @return string|null
168
     */
169
    public function getHeader($key)
170
    {
171
        return $this->getRequest()->getHeader($key);
172
    }
173
174
    /**
175
     * Returns headers data.
176
     *
177
     * @return array
178
     */
179
    public function getHeaders()
180
    {
181
        return $this->getRequest()->getHeaders();
182
    }
183
184
    /**
185
     * Returns request method
186
     *
187
     * @return string
188
     */
189
    public function getMethod()
190
    {
191
        return $this->getRequest()->getMethod();
192
    }
193
194
    /**
195
     * Sets the method to be performed on the resource identified by the
196
     * Request-URI.
197
     *
198
     * While HTTP method names are typically all uppercase characters, HTTP
199
     * method names are case-sensitive and thus implementations SHOULD NOT
200
     * modify the given string.
201
     *
202
     * @param string $method Case-insensitive method
203
     *
204
     * @return void
205
     */
206
    public function setMethod($method)
207
    {
208
        $this->getRequest()->setMethod($method);
209
    }
210
211
    /**
212
     * Returns true if the request has a cookie header with the passed
213
     * name, else false.
214
     *
215
     * @param string $cookieName Name of the cookie header to be checked
216
     *
217
     * @return boolean true if the request has the cookie, else false
218
     */
219
    public function hasCookie($cookieName)
220
    {
221
        return $this->getRequest()->hasCookie($cookieName);
222
    }
223
224
    /**
225
     * Returns the value of the cookie with the passed name.
226
     *
227
     * @param string $cookieName The name of the cookie to return
228
     *
229
     * @return \AppserverIo\Psr\HttpMessage\CookieInterface The cookie instance
230
     */
231
    public function getCookie($cookieName)
232
    {
233
        return $this->getRequest()->getCookie($cookieName);
234
    }
235
236
    /**
237
     * Adds the passed cookie to this request.
238
     *
239
     * @param \AppserverIo\Psr\HttpMessage\CookieInterface $cookie The cookie to add
240
     *
241
     * @return void
242
     */
243
    public function addCookie(CookieInterface $cookie)
244
    {
245
        $this->getRequest()->addCookie($cookie);
246
    }
247
248
    /**
249
     * Adds a header information got from connection.
250
     *
251
     * @param string $name  The header name
252
     * @param string $value The headers value
253
     *
254
     * @return void
255
     */
256
    public function addHeader($name, $value)
257
    {
258
        $this->getRequest()->addHeader($name, $value);
259
    }
260
261
    /**
262
     * Adds a part to the parts collection.
263
     *
264
     * @param \AppserverIo\Appserver\ServletEngine\Http\Part $part A form part object
265
     * @param string                                         $name A manually defined name
266
     *
267
     * @return void
268
     */
269
    public function addPart(PartInterface $part, $name = null)
270
    {
271
        $this->getRequest()->addPart($part, $name);
272
    }
273
274
    /**
275
     * Returns the body content as string.
276
     *
277
     * @return string The body content
278
     */
279
    public function getBodyContent()
280
    {
281
        return $this->getRequest()->getBodyContent();
282
    }
283
284
    /**
285
     * Returns the body stream as a resource.
286
     *
287
     * @return resource The body stream
288
     */
289
    public function getBodyStream()
290
    {
291
        return $this->getRequest()->getBodyStream();
292
    }
293
294
    /**
295
     * Returns the Http request instance.
296
     *
297
     * @return \AppserverIo\Psr\HttpMessage\RequestInterface The Http request instance
298
     */
299
    public function getHttpRequest()
300
    {
301
        return $this->getRequest()->getHttpRequest();
302
    }
303
304
    /**
305
     * Returns an array with all request parameters.
306
     *
307
     * @return array The array with the request parameters
308
     */
309
    public function getParameterMap()
310
    {
311
        return $this->getRequest()->getParameterMap();
312
    }
313
314
    /**
315
     * Returns the parameter with the passed name if available or null
316
     * if the parameter not exists.
317
     *
318
     * @param string  $name   The name of the parameter to return
319
     * @param integer $filter The filter to use
320
     *
321
     * @return string|null
322
     */
323
    public function getParameter($name, $filter = FILTER_SANITIZE_STRING)
324
    {
325
        return $this->getRequest()->getParameter($name, $filter);
326
    }
327
328
    /**
329
     * Returns a part object by given name
330
     *
331
     * @param string $name The name of the form part
332
     *
333
     * @return \AppserverIo\Http\HttpPart
334
     */
335
    public function getPart($name)
336
    {
337
        return $this->getRequest()->getPart($name);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getRequest()->getPart($name); (AppserverIo\Http\HttpPart) is incompatible with the return type declared by the interface AppserverIo\Psr\Servlet\...questInterface::getPart of type AppserverIo\Psr\HttpMessage\PartInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
338
    }
339
340
    /**
341
     * Returns the parts collection as array
342
     *
343
     * @return array A collection of HttpPart objects
344
     */
345
    public function getParts()
346
    {
347
        return $this->getRequest()->getParts();
348
    }
349
350
    /**
351
     * Return the session identifier included in this request, if any.
352
     *
353
     * @return string The session identifier included in this request
354
     */
355
    public function getRequestedSessionId()
356
    {
357
        return $this->getRequest()->getRequestedSessionId();
358
    }
359
360
    /**
361
     * Return the session name included in this request, if any.
362
     *
363
     * @return string The session name included in this request
364
     */
365
    public function getRequestedSessionName()
366
    {
367
        return $this->getRequest()->getRequestedSessionName();
368
    }
369
370
    /**
371
     * Returns the servlet response bound to this request.
372
     *
373
     * @return \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface The response instance
374
     */
375
    public function getResponse()
376
    {
377
        return $this->getRequest()->getResponse();
378
    }
379
380
    /**
381
     * Returns the server variable with the requested name.
382
     *
383
     * @param string $name The name of the server variable to be returned
384
     *
385
     * @return mixed The requested server variable
386
     */
387
    public function getServerVar($name)
388
    {
389
        return $this->getRequest()->getServerVar($name);
390
    }
391
392
    /**
393
     * Returns the array with the server variables.
394
     *
395
     * @return array The array with the server variables
396
     */
397
    public function getServerVars()
398
    {
399
        return $this->getRequest()->getServerVars();
400
    }
401
402
    /**
403
     * Returns the request URI.
404
     *
405
     * @return string The request URI
406
     */
407
    public function getUri()
408
    {
409
        return $this->getRequest()->getUri();
410
    }
411
412
    /**
413
     * Returns the protocol version.
414
     *
415
     * @return string The protocol version
416
     */
417
    public function getVersion()
418
    {
419
        return $this->getRequest()->getVersion();
420
    }
421
422
    /**
423
     * Checks if header exists by given name.
424
     *
425
     * @param string $name The header name to check
426
     *
427
     * @return boolean
428
     */
429
    public function hasHeader($name)
430
    {
431
        return $this->getRequest()->hasHeader($name);
432
    }
433
434
    /**
435
     * Queries whether the request contains a parameter or not.
436
     *
437
     * @param string $name The name of the param we're query for
438
     *
439
     * @return boolean TRUE if the parameter is available, else FALSE
440
     */
441
    public function hasParameter($name)
442
    {
443
        return $this->getRequest()->hasParameter($name);
444
    }
445
446
    /**
447
     * Sets the flag that shows if the request has already been dispatched.
448
     *
449
     * @return boolean TRUE if the request has already been dispatched, else FALSE
450
     */
451
    public function isDispatched()
452
    {
453
        return $this->getRequest()->isDispatched();
454
    }
455
456
    /**
457
     * Set Base modifier which allows for base path generation within rewritten URL environments.
458
     *
459
     * @param string $baseModifier The base modifier
460
     *
461
     * @return void
462
     */
463
    public function setBaseModifier($baseModifier)
464
    {
465
        $this->getRequest()->setBaseModifier($baseModifier);
466
    }
467
468
    /**
469
     * Resets the stream resource pointing to body content.
470
     *
471
     * @param resource $bodyStream The body content stream resource
472
     *
473
     * @return void
474
     */
475
    public function setBodyStream($bodyStream)
476
    {
477
        $this->getRequest()->setBodyStream($bodyStream);
478
    }
479
480
    /**
481
     * Sets the application context name (application name prefixed with a slash) for the actual request.
482
     *
483
     * @param string $contextPath The application context name
484
     *
485
     * @return void
486
     */
487
    public function setContextPath($contextPath)
488
    {
489
        $this->getRequest()->setContextPath($contextPath);
490
    }
491
492
    /**
493
     * Sets the flag to mark the request dispatched.
494
     *
495
     * @param boolean $dispatched TRUE if the request has already been dispatched, else FALSE
496
     *
497
     * @return void
498
     */
499
    public function setDispatched($dispatched = true)
500
    {
501
        $this->getRequest()->setDispatched($dispatched);
502
    }
503
504
    /**
505
     * Set headers data.
506
     *
507
     * @param array $headers The headers array to set
508
     *
509
     * @return void
510
     */
511
    public function setHeaders(array $headers)
512
    {
513
        $this->getRequest()->setHeaders($headers);
514
    }
515
516
    /**
517
     * Sets an array with all request parameters.
518
     *
519
     * @param array $parameterMap The parameter map
520
     *
521
     * @return void
522
     */
523
    public function setParameterMap(array $parameterMap)
524
    {
525
        $this->getRequest()->setParameterMap($parameterMap);
526
    }
527
528
    /**
529
     * Sets the absolute path info started from the context path.
530
     *
531
     * @param string $pathInfo The path info
532
     *
533
     * @return void
534
     */
535
    public function setPathInfo($pathInfo)
536
    {
537
        $this->getRequest()->setPathInfo($pathInfo);
538
    }
539
540
    /**
541
     * Set the requested session ID for this request. This is normally called by
542
     * the HTTP Connector, when it parses the request headers.
543
     *
544
     * @param string $requestedSessionId The requested session ID
545
     *
546
     * @return void
547
     */
548
    public function setRequestedSessionId($requestedSessionId)
549
    {
550
        $this->getRequest()->setRequestedSessionId($requestedSessionId);
551
    }
552
553
    /**
554
     * Set the requested session name for this request.
555
     *
556
     * @param string $requestedSessionName The requested session name
557
     *
558
     * @return void
559
     */
560
    public function setRequestedSessionName($requestedSessionName)
561
    {
562
        $this->getRequest()->setRequestedSessionName($requestedSessionName);
563
    }
564
565
    /**
566
     * Sets the path to the servlet used to handle this request.
567
     *
568
     * @param string $servletPath The path to the servlet
569
     *
570
     * @return void
571
     */
572
    public function setServletPath($servletPath)
573
    {
574
        $this->getRequest()->setServletPath($servletPath);
575
    }
576
577
    /**
578
     * Sets the URI.
579
     *
580
     * @param string $uri The request URI
581
     *
582
     * @return void
583
     */
584
    public function setUri($uri)
585
    {
586
        $this->getRequest()->setUri($uri);
587
    }
588
589
    /**
590
     * Set the protocol version.
591
     *
592
     * @param string $version The protocol version
593
     *
594
     * @return void
595
     */
596
    public function setVersion($version)
597
    {
598
        $this->getRequest()->setVersion($version);
599
    }
600
}
601