Completed
Pull Request — master (#16)
by Tim
05:29
created

HttpServletRequestWrapper::setParameterMap()   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 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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
     * Return the session identifier proposed by the actual configuration and request state.
122
     *
123
     * @return string The session identifier proposed for this request
124
     */
125
    public function getProposedSessionId()
126
    {
127
        return $this->getRequest()->getProposedSessionId();
128
    }
129
130
    /**
131
     * Returns the session for this request.
132
     *
133
     * @param boolean $create TRUE to create a new session, else FALSE
134
     *
135
     * @return \AppserverIo\Psr\Servlet\Http\HttpSessionInterface The session instance
136
     */
137
    public function getSession($create = false)
138
    {
139
        return $this->getRequest()->getSession($create);
140
    }
141
142
    /**
143
     * Returns the absolute path info started from the context path.
144
     *
145
     * @return string the absolute path info
146
     */
147
    public function getPathInfo()
148
    {
149
        return $this->getRequest()->getPathInfo();
150
    }
151
152
    /**
153
     * Returns the server name.
154
     *
155
     * @return string The server name
156
     */
157
    public function getServerName()
158
    {
159
        return $this->getRequest()->getServerName();
160
    }
161
162
    /**
163
     * Returns query string of the actual request.
164
     *
165
     * @return string|null The query string of the actual request
166
     */
167 1
    public function getQueryString()
168
    {
169 1
        return $this->getRequest()->getQueryString();
170
    }
171
172
    /**
173
     * Returns header info by given key
174
     *
175
     * @param string $key The header key to get
176
     *
177
     * @return string|null
178
     */
179
    public function getHeader($key)
180
    {
181
        return $this->getRequest()->getHeader($key);
182
    }
183
184
    /**
185
     * Returns headers data.
186
     *
187
     * @return array
188
     */
189
    public function getHeaders()
190
    {
191
        return $this->getRequest()->getHeaders();
192
    }
193
194
    /**
195
     * Returns request method
196
     *
197
     * @return string
198
     */
199
    public function getMethod()
200
    {
201
        return $this->getRequest()->getMethod();
202
    }
203
204
    /**
205
     * Sets the method to be performed on the resource identified by the
206
     * Request-URI.
207
     *
208
     * While HTTP method names are typically all uppercase characters, HTTP
209
     * method names are case-sensitive and thus implementations SHOULD NOT
210
     * modify the given string.
211
     *
212
     * @param string $method Case-insensitive method
213
     *
214
     * @return void
215
     */
216
    public function setMethod($method)
217
    {
218
        $this->getRequest()->setMethod($method);
219
    }
220
221
    /**
222
     * Returns true if the request has a cookie header with the passed
223
     * name, else false.
224
     *
225
     * @param string $cookieName Name of the cookie header to be checked
226
     *
227
     * @return boolean true if the request has the cookie, else false
228
     */
229
    public function hasCookie($cookieName)
230
    {
231
        return $this->getRequest()->hasCookie($cookieName);
232
    }
233
234
    /**
235
     * Returns the value of the cookie with the passed name.
236
     *
237
     * @param string $cookieName The name of the cookie to return
238
     *
239
     * @return \AppserverIo\Psr\HttpMessage\CookieInterface The cookie instance
240
     */
241
    public function getCookie($cookieName)
242
    {
243
        return $this->getRequest()->getCookie($cookieName);
244
    }
245
246
    /**
247
     * Adds the passed cookie to this request.
248
     *
249
     * @param \AppserverIo\Psr\HttpMessage\CookieInterface $cookie The cookie to add
250
     *
251
     * @return void
252
     */
253
    public function addCookie(CookieInterface $cookie)
254
    {
255
        $this->getRequest()->addCookie($cookie);
256
    }
257
258
    /**
259
     * Adds a header information got from connection.
260
     *
261
     * @param string $name  The header name
262
     * @param string $value The headers value
263
     *
264
     * @return void
265
     */
266
    public function addHeader($name, $value)
267
    {
268
        $this->getRequest()->addHeader($name, $value);
269
    }
270
271
    /**
272
     * Adds a part to the parts collection.
273
     *
274
     * @param \AppserverIo\Appserver\ServletEngine\Http\Part $part A form part object
275
     * @param string                                         $name A manually defined name
276
     *
277
     * @return void
278
     */
279
    public function addPart(PartInterface $part, $name = null)
280
    {
281
        $this->getRequest()->addPart($part, $name);
282
    }
283
284
    /**
285
     * Returns the body content as string.
286
     *
287
     * @return string The body content
288
     */
289
    public function getBodyContent()
290
    {
291
        return $this->getRequest()->getBodyContent();
292
    }
293
294
    /**
295
     * Returns the body stream as a resource.
296
     *
297
     * @return resource The body stream
298
     */
299
    public function getBodyStream()
300
    {
301
        return $this->getRequest()->getBodyStream();
302
    }
303
304
    /**
305
     * Returns the Http request instance.
306
     *
307
     * @return \AppserverIo\Psr\HttpMessage\RequestInterface The Http request instance
308
     */
309
    public function getHttpRequest()
310
    {
311
        return $this->getRequest()->getHttpRequest();
312
    }
313
314
    /**
315
     * Returns an array with all request parameters.
316
     *
317
     * @return array The array with the request parameters
318
     */
319
    public function getParameterMap()
320
    {
321
        return $this->getRequest()->getParameterMap();
322
    }
323
324
    /**
325
     * Returns the parameter with the passed name if available or null
326
     * if the parameter not exists.
327
     *
328
     * @param string  $name   The name of the parameter to return
329
     * @param integer $filter The filter to use
330
     *
331
     * @return string|null
332
     */
333
    public function getParameter($name, $filter = FILTER_SANITIZE_STRING)
334
    {
335
        return $this->getRequest()->getParameter($name, $filter);
336
    }
337
338
    /**
339
     * Returns a part object by given name
340
     *
341
     * @param string $name The name of the form part
342
     *
343
     * @return \AppserverIo\Http\HttpPart
344
     */
345
    public function getPart($name)
346
    {
347
        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...
348
    }
349
350
    /**
351
     * Returns the parts collection as array
352
     *
353
     * @return array A collection of HttpPart objects
354
     */
355
    public function getParts()
356
    {
357
        return $this->getRequest()->getParts();
358
    }
359
360
    /**
361
     * Return the session identifier included in this request, if any.
362
     *
363
     * @return string The session identifier included in this request
364
     */
365
    public function getRequestedSessionId()
366
    {
367
        return $this->getRequest()->getRequestedSessionId();
368
    }
369
370
    /**
371
     * Return the session name included in this request, if any.
372
     *
373
     * @return string The session name included in this request
374
     */
375
    public function getRequestedSessionName()
376
    {
377
        return $this->getRequest()->getRequestedSessionName();
378
    }
379
380
    /**
381
     * Returns the servlet response bound to this request.
382
     *
383
     * @return \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface The response instance
384
     */
385
    public function getResponse()
386
    {
387
        return $this->getRequest()->getResponse();
388
    }
389
390
    /**
391
     * Returns the server variable with the requested name.
392
     *
393
     * @param string $name The name of the server variable to be returned
394
     *
395
     * @return mixed The requested server variable
396
     */
397
    public function getServerVar($name)
398
    {
399
        return $this->getRequest()->getServerVar($name);
400
    }
401
402
    /**
403
     * Returns the array with the server variables.
404
     *
405
     * @return array The array with the server variables
406
     */
407
    public function getServerVars()
408
    {
409
        return $this->getRequest()->getServerVars();
410
    }
411
412
    /**
413
     * Returns the request URI.
414
     *
415
     * @return string The request URI
416
     */
417
    public function getUri()
418
    {
419
        return $this->getRequest()->getUri();
420
    }
421
422
    /**
423
     * Returns the protocol version.
424
     *
425
     * @return string The protocol version
426
     */
427
    public function getVersion()
428
    {
429
        return $this->getRequest()->getVersion();
430
    }
431
432
    /**
433
     * Checks if header exists by given name.
434
     *
435
     * @param string $name The header name to check
436
     *
437
     * @return boolean
438
     */
439
    public function hasHeader($name)
440
    {
441
        return $this->getRequest()->hasHeader($name);
442
    }
443
444
    /**
445
     * Queries whether the request contains a parameter or not.
446
     *
447
     * @param string $name The name of the param we're query for
448
     *
449
     * @return boolean TRUE if the parameter is available, else FALSE
450
     */
451
    public function hasParameter($name)
452
    {
453
        return $this->getRequest()->hasParameter($name);
454
    }
455
456
    /**
457
     * Sets the flag that shows if the request has already been dispatched.
458
     *
459
     * @return boolean TRUE if the request has already been dispatched, else FALSE
460
     */
461
    public function isDispatched()
462
    {
463
        return $this->getRequest()->isDispatched();
464
    }
465
466
    /**
467
     * Set Base modifier which allows for base path generation within rewritten URL environments.
468
     *
469
     * @param string $baseModifier The base modifier
470
     *
471
     * @return void
472
     */
473
    public function setBaseModifier($baseModifier)
474
    {
475
        $this->getRequest()->setBaseModifier($baseModifier);
476
    }
477
478
    /**
479
     * Resets the stream resource pointing to body content.
480
     *
481
     * @param resource $bodyStream The body content stream resource
482
     *
483
     * @return void
484
     */
485
    public function setBodyStream($bodyStream)
486
    {
487
        $this->getRequest()->setBodyStream($bodyStream);
488
    }
489
490
    /**
491
     * Sets the application context name (application name prefixed with a slash) for the actual request.
492
     *
493
     * @param string $contextPath The application context name
494
     *
495
     * @return void
496
     */
497
    public function setContextPath($contextPath)
498
    {
499
        $this->getRequest()->setContextPath($contextPath);
500
    }
501
502
    /**
503
     * Sets the flag to mark the request dispatched.
504
     *
505
     * @param boolean $dispatched TRUE if the request has already been dispatched, else FALSE
506
     *
507
     * @return void
508
     */
509
    public function setDispatched($dispatched = true)
510
    {
511
        $this->getRequest()->setDispatched($dispatched);
512
    }
513
514
    /**
515
     * Set headers data.
516
     *
517
     * @param array $headers The headers array to set
518
     *
519
     * @return void
520
     */
521
    public function setHeaders(array $headers)
522
    {
523
        $this->getRequest()->setHeaders($headers);
524
    }
525
526
    /**
527
     * Sets an array with all request parameters.
528
     *
529
     * @param array $parameterMap The parameter map
530
     *
531
     * @return void
532
     */
533
    public function setParameterMap(array $parameterMap)
534
    {
535
        $this->getRequest()->setParameterMap($parameterMap);
536
    }
537
538
    /**
539
     * Sets the absolute path info started from the context path.
540
     *
541
     * @param string $pathInfo The path info
542
     *
543
     * @return void
544
     */
545
    public function setPathInfo($pathInfo)
546
    {
547
        $this->getRequest()->setPathInfo($pathInfo);
548
    }
549
550
    /**
551
     * Set the requested session ID for this request. This is normally called by
552
     * the HTTP Connector, when it parses the request headers.
553
     *
554
     * @param string $requestedSessionId The requested session ID
555
     *
556
     * @return void
557
     */
558
    public function setRequestedSessionId($requestedSessionId)
559
    {
560
        $this->getRequest()->setRequestedSessionId($requestedSessionId);
561
    }
562
563
    /**
564
     * Set the requested session name for this request.
565
     *
566
     * @param string $requestedSessionName The requested session name
567
     *
568
     * @return void
569
     */
570
    public function setRequestedSessionName($requestedSessionName)
571
    {
572
        $this->getRequest()->setRequestedSessionName($requestedSessionName);
573
    }
574
575
    /**
576
     * Sets the path to the servlet used to handle this request.
577
     *
578
     * @param string $servletPath The path to the servlet
579
     *
580
     * @return void
581
     */
582
    public function setServletPath($servletPath)
583
    {
584
        $this->getRequest()->setServletPath($servletPath);
585
    }
586
587
    /**
588
     * Sets the URI.
589
     *
590
     * @param string $uri The request URI
591
     *
592
     * @return void
593
     */
594
    public function setUri($uri)
595
    {
596
        $this->getRequest()->setUri($uri);
597
    }
598
599
    /**
600
     * Set the protocol version.
601
     *
602
     * @param string $version The protocol version
603
     *
604
     * @return void
605
     */
606
    public function setVersion($version)
607
    {
608
        $this->getRequest()->setVersion($version);
609
    }
610
}
611