Completed
Push — master ( 96fcad...d3856b )
by Tobias
02:21
created

ServerRequest::__construct()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 27

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 0
Metric Value
dl 27
loc 27
ccs 12
cts 13
cp 0.9231
rs 9.1768
c 0
b 0
f 0
cc 5
nc 8
nop 6
crap 5.0113
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Psr7;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\StreamInterface;
9
use Psr\Http\Message\UploadedFileInterface;
10
use Psr\Http\Message\UriInterface;
11
12
/**
13
 * @author Michael Dowling and contributors to guzzlehttp/psr7
14
 * @author Tobias Nyholm <[email protected]>
15
 */
16
final class ServerRequest implements ServerRequestInterface
17
{
18
    use MessageTrait;
19
    use RequestTrait;
20
21
    /** @var array */
22
    private $attributes = [];
23
24
    /** @var array */
25
    private $cookieParams = [];
26
27
    /** @var null|array|object */
28
    private $parsedBody;
29
30
    /** @var array */
31
    private $queryParams = [];
32
33
    /** @var array */
34
    private $serverParams;
35
36
    /** @var UploadedFileInterface[] */
37
    private $uploadedFiles = [];
38
39
    /**
40
     * @param string                               $method       HTTP method
41
     * @param string|UriInterface                  $uri          URI
42
     * @param array                                $headers      Request headers
43
     * @param string|null|resource|StreamInterface $body         Request body
44
     * @param string                               $version      Protocol version
45
     * @param array                                $serverParams Typically the $_SERVER superglobal
46
     */
47 53 View Code Duplication
    public function __construct(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
        string $method,
49
        $uri,
50
        array $headers = [],
51
        $body = null,
52
        string $version = '1.1',
53
        array $serverParams = []
54
    ) {
55 53
        $this->serverParams = $serverParams;
56
57 53
        if (!($uri instanceof UriInterface)) {
58 24
            $uri = new Uri($uri);
59
        }
60
61 53
        $this->method = $method;
62 53
        $this->uri = $uri;
63 53
        $this->setHeaders($headers);
64 53
        $this->protocol = $version;
65
66 53
        if (!$this->hasHeader('Host')) {
67 53
            $this->updateHostFromUri();
68
        }
69
70 53
        if ('' !== $body && null !== $body) {
71
            $this->stream = (new StreamFactory())->createStream($body);
72
        }
73 53
    }
74
75 3
    public function getServerParams(): array
76
    {
77 3
        return $this->serverParams;
78
    }
79
80 10
    public function getUploadedFiles(): array
81
    {
82 10
        return $this->uploadedFiles;
83
    }
84
85 9
    public function withUploadedFiles(array $uploadedFiles)
86
    {
87 9
        $new = clone $this;
88 9
        $new->uploadedFiles = $uploadedFiles;
89
90 9
        return $new;
91
    }
92
93 5
    public function getCookieParams(): array
94
    {
95 5
        return $this->cookieParams;
96
    }
97
98 10
    public function withCookieParams(array $cookies)
99
    {
100 10
        $new = clone $this;
101 10
        $new->cookieParams = $cookies;
102
103 10
        return $new;
104
    }
105
106 4
    public function getQueryParams(): array
107
    {
108 4
        return $this->queryParams;
109
    }
110
111 10
    public function withQueryParams(array $query)
112
    {
113 10
        $new = clone $this;
114 10
        $new->queryParams = $query;
115
116 10
        return $new;
117
    }
118
119 6
    public function getParsedBody()
120
    {
121 6
        return $this->parsedBody;
122
    }
123
124 16
    public function withParsedBody($data)
125
    {
126 16
        if (!is_array($data) && !is_object($data) && null !== $data) {
127 4
            throw new \InvalidArgumentException('First parameter to withParsedBody MUST be object, array or null');
128
        }
129
130 12
        $new = clone $this;
131 12
        $new->parsedBody = $data;
132
133 12
        return $new;
134
    }
135
136 3
    public function getAttributes(): array
137
    {
138 3
        return $this->attributes;
139
    }
140
141 4 View Code Duplication
    public function getAttribute($attribute, $default = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143 4
        if (false === array_key_exists($attribute, $this->attributes)) {
144 4
            return $default;
145
        }
146
147 4
        return $this->attributes[$attribute];
148
    }
149
150 5
    public function withAttribute($attribute, $value): self
151
    {
152 5
        $new = clone $this;
153 5
        $new->attributes[$attribute] = $value;
154
155 5
        return $new;
156
    }
157
158 3 View Code Duplication
    public function withoutAttribute($attribute): self
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
    {
160 3
        if (false === array_key_exists($attribute, $this->attributes)) {
161 1
            return $this;
162
        }
163
164 3
        $new = clone $this;
165 3
        unset($new->attributes[$attribute]);
166
167 3
        return $new;
168
    }
169
}
170