Completed
Push — master ( bc72c5...1b234b )
by richard
02:39
created

ServerRequest::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Almendra\Http\Psr\Messages;
4
5
use Psr\Http\Message\ServerRequestInterface as RequestInterface;
6
use Almendra\Http\Psr\Messages\Response;
7
8
use Almendra\Http\Helpers\URI as URIHelper;
9
use Almendra\Http\Server;
10
11
class ServerRequest extends Response implements RequestInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: getAttribute, getAttributes, getMethod, getParsedBody, getRequestTarget, getUploadedFiles, getUri, withAttribute, withMethod, withParsedBody, withQueryParams, withRequestTarget, withUploadedFiles, withUri, withoutAttribute
Loading history...
12
{
13
    protected $_cookies = [];
14
    protected $_serverParams = [];
15
16
    // protocol ver
17
    // headers
18
    // body
19
20
21
    public function __construct($cookies = null, $serverParams = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $serverParams is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
        if (isset($cookies) && null !== $cookies) {
23
            $this -> setCookies($cookies);
24
        } else {
25
            $this -> setCookies($this -> getCookieParams());
26
        }
27
        
28
    }
29
    
30
    /**
31
     * Retrieve server parameters.
32
     *
33
     * Retrieves data related to the incoming request environment,
34
     * typically derived from PHP's $_SERVER superglobal. The data IS NOT
35
     * REQUIRED to originate from $_SERVER.
36
     *
37
     * @return array
38
     */
39
    public function getServerParams() {
0 ignored issues
show
Coding Style introduced by
getServerParams uses the super-global variable $_SERVER 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...
40
        if (isset($_SERVER) && null !== $_SERVER) {
41
            $this -> _serverParams = $_SERVER;
42
        }
43
44
        return $this -> _serverParams; // attempt to retrieve them
45
    }
46
47
48
    /**
49
     * Retrieve cookies.
50
     *
51
     * Retrieves cookies sent by the client to the server.
52
     *
53
     * The data MUST be compatible with the structure of the $_COOKIE
54
     * superglobal.
55
     *
56
     * @return array
57
     */
58
    public function getCookieParams()
59
    {
60
        return $this -> _cookies;
61
    }
62
63
    public function withCookieParams(array $cookies)
64
    {
65
        $clone = clone $this;
66
        $clone -> setCookies($cookies);
67
68
        return $clone;
69
    }
70
71
    public function setCookies(array $cookies)
72
    {
73
        foreach ($cookies as $name => $value) {
74
            $this -> setCookie($name, $value);
75
        }
76
    }
77
78
    public function setCookie($name, $value)
79
    {
80
        $this -> _cookies[$name] = $value;
81
    }
82
83
    /**
84
     * Retrieve query string arguments.
85
     *
86
     * Retrieves the deserialized query string arguments, if any.
87
     *
88
     * Note: the query params might not be in sync with the URI or server
89
     * params. If you need to ensure you are only getting the original
90
     * values, you may need to parse the query string from `getUri()->getQuery()`
91
     * or from the `QUERY_STRING` server param.
92
     *
93
     * @return array
94
     */
95
    public function getQueryParams()
96
    {
97
        return URIHelper::getQueryParams($this -> getUri(), false);
0 ignored issues
show
Bug Compatibility introduced by
The expression \Almendra\Http\Helpers\U...this->getUri(), false); of type array|string adds the type string to the return on line 97 which is incompatible with the return type declared by the interface Psr\Http\Message\ServerR...terface::getQueryParams of type array.
Loading history...
98
    }
99
}
100