ServerRequest::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 11
dl 0
loc 20
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cakasim\Payone\Sdk\Http\Message;
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
 * The HTTP server request message implementation.
14
 *
15
 * @author Fabian Böttcher <[email protected]>
16
 * @since 0.1.0
17
 */
18
class ServerRequest extends Request implements ServerRequestInterface
19
{
20
    /**
21
     * @var array The server params of this server request.
22
     */
23
    protected $serverParams;
24
25
    /**
26
     * @var array The cookie params of this server request.
27
     */
28
    protected $cookieParams;
29
30
    /**
31
     * @var array The query params of this server request.
32
     */
33
    protected $queryParams;
34
35
    /**
36
     * @var UploadedFileInterface[] The uploaded files of this server request.
37
     */
38
    protected $uploadedFiles = [];
39
40
    /**
41
     * @var array|object|null The parsed body data.
42
     */
43
    protected $parsedBody = null;
44
45
    /**
46
     * @var array The server request attributes.
47
     */
48
    protected $attributes = [];
49
50
    /**
51
     * Constructs the server request.
52
     *
53
     * @param string $method The request method.
54
     * @param UriInterface $uri The request URI.
55
     * @param string $protocolVersion The HTTP protocol version.
56
     * @param StreamInterface $body The request body.
57
     * @param array $headers The request headers.
58
     * @param array $serverParams The server params.
59
     * @param array $cookieParams The cookie params.
60
     * @param array $queryParams  The query params.
61
     * @param array $uploadedFiles The uploaded files.
62
     * @param array|object|null $parsedBody The parsed request body.
63
     * @param array $attributes The request attributes.
64
     */
65
    public function __construct(
66
        string $method,
67
        UriInterface $uri,
68
        string $protocolVersion,
69
        StreamInterface $body,
70
        array $headers,
71
        array $serverParams,
72
        array $cookieParams,
73
        array $queryParams,
74
        array $uploadedFiles,
75
        $parsedBody,
76
        array $attributes
77
    ) {
78
        parent::__construct($method, $uri, $protocolVersion, $body, $headers);
79
        $this->serverParams = $serverParams;
80
        $this->cookieParams = $cookieParams;
81
        $this->queryParams = $queryParams;
82
        $this->uploadedFiles = $uploadedFiles;
83
        $this->parsedBody = $parsedBody;
84
        $this->attributes = $attributes;
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    public function getServerParams()
91
    {
92
        return $this->serverParams;
93
    }
94
95
    /**
96
     * @inheritDoc
97
     */
98
    public function getCookieParams()
99
    {
100
        return $this->cookieParams;
101
    }
102
103
    /**
104
     * Sets the cookie params.
105
     *
106
     * @param array $cookieParams The cookie params.
107
     * @return $this
108
     */
109
    protected function setCookieParams(array $cookieParams): self
110
    {
111
        $this->cookieParams = $cookieParams;
112
        return $this;
113
    }
114
115
    /**
116
     * @inheritDoc
117
     */
118
    public function withCookieParams(array $cookies)
119
    {
120
        return (clone $this)->setCookieParams($cookies);
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126
    public function getQueryParams()
127
    {
128
        return $this->queryParams;
129
    }
130
131
    /**
132
     * Sets the query params.
133
     *
134
     * @param array $queryParams The query params.
135
     * @return $this
136
     */
137
    protected function setQueryParams(array $queryParams): self
138
    {
139
        $this->queryParams = $queryParams;
140
        return $this;
141
    }
142
143
    /**
144
     * @inheritDoc
145
     */
146
    public function withQueryParams(array $query)
147
    {
148
        return (clone $this)->setQueryParams($query);
149
    }
150
151
    /**
152
     * @inheritDoc
153
     */
154
    public function getUploadedFiles()
155
    {
156
        return $this->uploadedFiles;
157
    }
158
159
    /**
160
     * Sets the uploaded files.
161
     *
162
     * @param UploadedFileInterface[] $uploadedFiles The uploaded files.
163
     * @return $this
164
     */
165
    protected function setUploadedFiles(array $uploadedFiles): self
166
    {
167
        $this->uploadedFiles = $uploadedFiles;
168
        return $this;
169
    }
170
171
    /**
172
     * @inheritDoc
173
     */
174
    public function withUploadedFiles(array $uploadedFiles)
175
    {
176
        return (clone $this)->setUploadedFiles($uploadedFiles);
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182
    public function getParsedBody()
183
    {
184
        return $this->parsedBody;
185
    }
186
187
    /**
188
     * Sets the parsed body data.
189
     *
190
     * @param array|object|null $parsedBody The parsed body data.
191
     * @return $this
192
     */
193
    protected function setParsedBody($parsedBody): self
194
    {
195
        $this->parsedBody = $parsedBody;
196
        return $this;
197
    }
198
199
    /**
200
     * @inheritDoc
201
     */
202
    public function withParsedBody($data): self
203
    {
204
        return (clone $this)->setParsedBody($data);
205
    }
206
207
    /**
208
     * @inheritDoc
209
     */
210
    public function getAttributes(): array
211
    {
212
        return $this->attributes;
213
    }
214
215
    /**
216
     * @inheritDoc
217
     */
218
    public function getAttribute($name, $default = null)
219
    {
220
        return $this->attributes[$name] ?? $default;
221
    }
222
223
    /**
224
     * Sets an attribute of this server request.
225
     *
226
     * @param string $name The attribute name.
227
     * @param mixed $value The attribute value.
228
     * @return $this
229
     */
230
    protected function setAttribute($name, $value): self
231
    {
232
        $this->attributes[$name] = $value;
233
        return $this;
234
    }
235
236
    /**
237
     * Removes the specified attribute from this server request.
238
     *
239
     * @param string $name The attribute name.
240
     * @return $this
241
     */
242
    protected function removeAttribute($name): self
243
    {
244
        unset($this->attributes[$name]);
245
        return $this;
246
    }
247
248
    /**
249
     * @inheritDoc
250
     */
251
    public function withAttribute($name, $value): self
252
    {
253
        return (clone $this)->setAttribute($name, $value);
254
    }
255
256
    /**
257
     * @inheritDoc
258
     */
259
    public function withoutAttribute($name): self
260
    {
261
        return (clone $this)->removeAttribute($name);
262
    }
263
}
264