Passed
Push — master ( 5fb311...42b16f )
by Zlatin
02:02
created

ServerRequest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Test Coverage

Coverage 9.84%

Importance

Changes 0
Metric Value
dl 0
loc 216
ccs 6
cts 61
cp 0.0984
rs 10
c 0
b 0
f 0
wmc 20

15 Methods

Rating   Name   Duplication   Size   Complexity  
A withAttribute() 0 6 1
A getAttribute() 0 6 2
A getParsedBody() 0 3 1
A getQueryParams() 0 3 1
A __construct() 0 4 1
A getAttributes() 0 3 1
A withoutAttribute() 0 10 2
A getServerParams() 0 3 1
B createFromGlobals() 0 26 4
A getUploadedFiles() 0 3 1
A withQueryParams() 0 6 1
A withUploadedFiles() 0 6 1
A withCookieParams() 0 6 1
A withParsedBody() 0 6 1
A getCookieParams() 0 3 1
1
<?php
2
namespace DevOp\Core\Http;
3
4
use DevOp\Core\Http\Request;
5
use Psr\Http\Message\UriInterface;
6
use Psr\Http\Message\StreamInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
class ServerRequest extends Request implements ServerRequestInterface
10
{
11
12
    use MessageTrait;
13
    use RequestTrait;
14
15
    /**
16
     * @var array
17
     */
18
    private $attributes = [];
19
20
    /**
21
     * @var array
22
     */
23
    private $cookieParams = [];
24
25
    /**
26
     * @var mixed
27
     */
28
    private $parsedBody;
29
30
    /**
31
     * @var array
32
     */
33
    private $queryParams = [];
34
35
    /**
36
     * @var array
37
     */
38
    private $serverParams = [];
39
40
    /**
41
     * @var array
42
     */
43
    private $uploadedFiles = [];
44
45
    /**
46
     * @return self
47
     */
48
    public static function createFromGlobals()
49
    {
50
51
        $uri = Uri::createFromGlobals();
52
53
        $method = $_SERVER['REQUEST_METHOD'] ?: 'GET';
54
55
        $headers = [];
56
        if (function_exists('getallheaders')) {
57
            $headers = getallheaders();
58
        }
59
60
        $body = 'php://memory';
61
62
        $protocol = '1.1';
63
        if (isset($_SERVER['SERVER_PROTOCOL'])) {
64
            $protocol = str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']);
65
        }
66
67
        $serverRequest = new ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER);
0 ignored issues
show
Bug introduced by
It seems like $headers can also be of type false; however, parameter $headers of DevOp\Core\Http\ServerRequest::__construct() does only seem to accept array, 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

67
        $serverRequest = new ServerRequest($method, $uri, /** @scrutinizer ignore-type */ $headers, $body, $protocol, $_SERVER);
Loading history...
68
69
        return $serverRequest
70
                ->withParsedBody($_POST)
71
                ->withQueryParams($_GET)
72
                ->withCookieParams($_COOKIE)
73
                ->withUploadedFiles(UploadedFile::createFromGlobal());
74
    }
75
76
    /**
77
     * 
78
     * @param string $method
79
     * @param UriInterface|string $uri
80
     * @param array $headers
81
     * @param string|null|resource|StreamInterface $body
82
     * @param string|null $version
83
     * @param array $serverParams
84
     */
85 2
    public function __construct($method, $uri, array $headers = [], $body = 'php://memory', $version = '1.1', array $serverParams = [])
86
    {
87 2
        parent::__construct($method, $uri, $headers, $body, $version);
0 ignored issues
show
Bug introduced by
It seems like $body can also be of type resource; however, parameter $body of DevOp\Core\Http\Request::__construct() does only seem to accept string|DevOp\Core\Http\Stream, 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

87
        parent::__construct($method, $uri, $headers, /** @scrutinizer ignore-type */ $body, $version);
Loading history...
88 2
        $this->serverParams = $serverParams;
89 2
    }
90
    
91
    /**
92
     * @return string
93
     */
94
    public function getAttribute($name, $default = null)
95
    {
96
        if (array_key_exists($name, $this->attributes)) {
97
            return $this->attributes[$name];
98
        }
99
        return $default;
100
    }
101
102
    /**
103
     * @return array
104
     */
105 2
    public function getAttributes()
106
    {
107 2
        return $this->attributes;
108
    }
109
110
    /**
111
     * @return array
112
     */
113
    public function getCookieParams()
114
    {
115
        return $this->cookieParams;
116
    }
117
118
    /**
119
     * @return mixed
120
     */
121
    public function getParsedBody()
122
    {
123
        return $this->parsedBody;
124
    }
125
126
    /**
127
     * @return array
128
     */
129
    public function getQueryParams()
130
    {
131
        return $this->queryParams;
132
    }
133
134
    /**
135
     * @return array
136
     */
137
    public function getServerParams()
138
    {
139
        return $this->serverParams;
140
    }
141
142
    /**
143
     * @return array
144
     */
145
    public function getUploadedFiles()
146
    {
147
        return $this->uploadedFiles;
148
    }
149
150
    /**
151
     * @param string $name
152
     * @param string $value
153
     * @return \DevOp\Core\Http\ServerRequest
154
     */
155
    public function withAttribute($name, $value)
156
    {
157
        $clone = clone $this;
158
        $clone->attributes[$name] = $value;
159
160
        return $clone;
161
    }
162
163
    /**
164
     * @param array $cookies
165
     * @return \DevOp\Core\Http\ServerRequest
166
     */
167
    public function withCookieParams(array $cookies)
168
    {
169
        $clone = clone $this;
170
        $clone->cookieParams = $cookies;
171
172
        return $clone;
173
    }
174
175
    /**
176
     * @param mixed $data
177
     * @return \DevOp\Core\Http\ServerRequest
178
     */
179
    public function withParsedBody($data)
180
    {
181
        $clone = clone $this;
182
        $clone->parsedBody = $data;
183
184
        return $clone;
185
    }
186
187
    /**
188
     * @param array $query
189
     * @return \DevOp\Core\Http\ServerRequest
190
     */
191
    public function withQueryParams(array $query)
192
    {
193
        $clone = clone $this;
194
        $clone->queryParams = $query;
195
196
        return $clone;
197
    }
198
199
    /**
200
     * @param array $uploadedFiles
201
     * @return \DevOp\Core\Http\ServerRequest
202
     */
203
    public function withUploadedFiles(array $uploadedFiles)
204
    {
205
        $clone = clone $this;
206
        $clone->uploadedFiles = $uploadedFiles;
207
208
        return $clone;
209
    }
210
211
    /**
212
     * @param string $name
213
     * @return \DevOp\Core\Http\ServerRequest|$this
214
     */
215
    public function withoutAttribute($name)
216
    {
217
        if (!array_key_exists($name, $this->attributes)) {
218
            return $this;
219
        }
220
221
        $clone = clone $this;
222
        unset($clone->attributes[$name]);
223
224
        return $clone;
225
    }
226
}
227