Passed
Push — master ( b65012...5ddc87 )
by Zlatin
01:29
created

ServerRequest::getCookieParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace DevOp\Core\Http;
3
4
use Psr\Http\Message\UriInterface;
5
use Psr\Http\Message\StreamInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
class ServerRequest extends Request implements ServerRequestInterface
9
{
10
11
    use MessageTrait;
12
13
    /**
14
     * @var array
15
     */
16
    private $attributes = [];
17
18
    /**
19
     * @var array
20
     */
21
    private $cookieParams = [];
22
23
    /**
24
     * @var array
25
     */
26
    private $parsedBody = [];
27
28
    /**
29
     * @var array
30
     */
31
    private $queryParams = [];
32
33
    /**
34
     * @var array
35
     */
36
    private $serverParams = [];
37
38
    /**
39
     * @var array
40
     */
41
    private $uploadedFiles = [];
42
43
    /**
44
     * @param type $method
0 ignored issues
show
Bug introduced by
The type DevOp\Core\Http\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
     * @param UriInterface $uri
46
     * @param array $headers
47
     * @param type $body
48
     * @param type $version
49
     * @param array $serverParams
50
     */
51 48
    public function __construct($method, UriInterface $uri, array $headers = [], $body = 'php://temp', $version = '1.1', array $serverParams = [])
52
    {
53 48
        $this->serverParams = $serverParams;
54 48
        parent::__construct($method, $uri, $headers, $body, $version);
0 ignored issues
show
Bug introduced by
It seems like $body can also be of type string; however, parameter $body of DevOp\Core\Http\Request::__construct() does only seem to accept Psr\Http\Message\StreamInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        parent::__construct($method, $uri, $headers, /** @scrutinizer ignore-type */ $body, $version);
Loading history...
55 48
    }
56
57 14
    public static function getUriFromGlobals(array $server = [])
58
    {
59 14
        $uri = new Uri('');
60 14
        $uri = $uri->withScheme(!empty($server['HTTPS']) && $server['HTTPS'] !== 'off' ? 'https' : 'http');
61 14
        $hasPort = false;
62 14
        if (isset($server['HTTP_HOST'])) {
63 14
            $hostHeaderParts = explode(':', $server['HTTP_HOST']);
64 14
            $uri = $uri->withHost($hostHeaderParts[0]);
65 14
            if (isset($hostHeaderParts[1])) {
66
                $hasPort = true;
67 14
                $uri = $uri->withPort($hostHeaderParts[1]);
0 ignored issues
show
Bug introduced by
$hostHeaderParts[1] of type string is incompatible with the type integer expected by parameter $port of DevOp\Core\Http\Uri::withPort(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
                $uri = $uri->withPort(/** @scrutinizer ignore-type */ $hostHeaderParts[1]);
Loading history...
68
            }
69
        } elseif (isset($server['SERVER_NAME'])) {
70
            $uri = $uri->withHost($server['SERVER_NAME']);
71
        } elseif (isset($server['SERVER_ADDR'])) {
72
            $uri = $uri->withHost($server['SERVER_ADDR']);
73
        }
74 14
        if (!$hasPort && isset($server['SERVER_PORT'])) {
75
            $uri = $uri->withPort($server['SERVER_PORT']);
76
        }
77 14
        $hasQuery = false;
78 14
        if (isset($server['REQUEST_URI'])) {
79 14
            $requestUriParts = explode('?', $server['REQUEST_URI'], 2);
80 14
            $uri = $uri->withPath($requestUriParts[0]);
81 14
            if (isset($requestUriParts[1])) {
82
                $hasQuery = true;
83
                $uri = $uri->withQuery($requestUriParts[1]);
84
            }
85
        }
86 14
        if (!$hasQuery && isset($server['QUERY_STRING'])) {
87 14
            $uri = $uri->withQuery($server['QUERY_STRING']);
88
        }
89 14
        return $uri;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getAttribute($name, $default = null)
96
    {
97
        if (array_key_exists($name, $this->attributes)) {
98
            return $this->attributes[$name];
99
        }
100
        return $default;
101
    }
102
103
    /**
104
     * @return array
105
     */
106 2
    public function getAttributes()
107
    {
108 2
        return $this->attributes;
109
    }
110
111
    /**
112
     * @return array
113
     */
114 2
    public function getCookieParams()
115
    {
116 2
        return $this->cookieParams;
117
    }
118
119
    /**
120
     * @return mixed
121
     */
122 2
    public function getParsedBody()
123
    {
124 2
        return $this->parsedBody;
125
    }
126
127
    /**
128
     * @return array
129
     */
130 2
    public function getQueryParams()
131
    {
132 2
        return $this->queryParams;
133
    }
134
135
    /**
136
     * @return array
137
     */
138 2
    public function getServerParams()
139
    {
140 2
        return $this->serverParams;
141
    }
142
143
    /**
144
     * @return array
145
     */
146 2
    public function getUploadedFiles()
147
    {
148 2
        return $this->uploadedFiles;
149
    }
150
151
    /**
152
     * @param string $name
153
     * @param string $value
154
     * @return \DevOp\Core\Http\ServerRequest
155
     */
156
    public function withAttribute($name, $value)
157
    {
158
        $clone = clone $this;
159
        $clone->attributes[$name] = $value;
160
161
        return $clone;
162
    }
163
164
    /**
165
     * @param array $cookies
166
     * @return \DevOp\Core\Http\ServerRequest
167
     */
168
    public function withCookieParams(array $cookies)
169
    {
170
        $clone = clone $this;
171
        $clone->cookieParams = $cookies;
172
173
        return $clone;
174
    }
175
176
    /**
177
     * @param array $data
178
     * @return \DevOp\Core\Http\ServerRequest
179
     */
180
    public function withParsedBody($data)
181
    {
182
        $clone = clone $this;
183
        $clone->parsedBody = $data;
184
185
        return $clone;
186
    }
187
188
    /**
189
     * @param array $query
190
     * @return \DevOp\Core\Http\ServerRequest
191
     */
192
    public function withQueryParams(array $query)
193
    {
194
        $clone = clone $this;
195
        $clone->queryParams = $query;
196
197
        return $clone;
198
    }
199
200
    /**
201
     * @param array $uploadedFiles
202
     * @return \DevOp\Core\Http\ServerRequest
203
     */
204
    public function withUploadedFiles(array $uploadedFiles)
205
    {
206
        $clone = clone $this;
207
        $clone->uploadedFiles = $uploadedFiles;
208
209
        return $clone;
210
    }
211
212
    /**
213
     * @param string $name
214
     * @return self
215
     */
216
    public function withoutAttribute($name)
217
    {
218
        if (!array_key_exists($name, $this->attributes)) {
219
            return $this;
220
        }
221
222
        $clone = clone $this;
223
        unset($clone->attributes[$name]);
224
225
        return $clone;
226
    }
227
}
228