Completed
Branch master (8ae5a6)
by Neomerx
01:35
created

Sapi::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 12
cts 12
cp 1
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 8
crap 1

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 namespace Limoncello\Core\Application;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Contracts\Core\SapiInterface;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\StreamInterface;
22
use Psr\Http\Message\UriInterface;
23
use Zend\Diactoros\Response\EmitterInterface;
24
use Zend\Diactoros\ServerRequestFactory;
25
26
/**
27
 * @package Limoncello\Core
28
 */
29
class Sapi implements SapiInterface
30
{
31
    /**
32
     * @var EmitterInterface
33
     */
34
    private $sapiEmitter;
35
36
    /**
37
     * @var array
38
     */
39
    private $server;
40
41
    /**
42
     * @var array
43
     */
44
    private $files;
45
46
    /**
47
     * @var array
48
     */
49
    private $headers;
50
51
    /**
52
     * @var UriInterface
53
     */
54
    private $uri;
55
56
    /**
57
     * @var string
58
     */
59
    private $method;
60
61
    /**
62
     * @var array
63
     */
64
    private $cookies;
65
66
    /**
67
     * @var array
68
     */
69
    private $queryParams;
70
71
    /**
72
     * @var array|object
73
     */
74
    private $parsedBody;
75
76
    /**
77
     * @var string|resource|StreamInterface
78
     */
79
    private $messageBody;
80
81
    /**
82
     * @var string
83
     */
84
    private $protocolVersion;
85
86
    /**
87
     * Sapi constructor.
88
     *
89
     * @param EmitterInterface                $sapiEmitter
90
     * @param array|null                      $server
91
     * @param array|null                      $queryParams
92
     * @param array|object|null               $parsedBody
93
     * @param array|null                      $cookies
94
     * @param array|null                      $files
95
     * @param string|resource|StreamInterface $messageBody
96
     * @param string                          $protocolVersion
97
     *
98
     * @SuppressWarnings(PHPMD.StaticAccess)
99
     * @SuppressWarnings(PHPMD.Superglobals)
100
     */
101 7
    public function __construct(
102
        EmitterInterface $sapiEmitter,
103
        array $server = null,
104
        array $queryParams = null,
105
        array $parsedBody = null,
106
        array $cookies = null,
107
        array $files = null,
108
        $messageBody = 'php://input',
109
        string $protocolVersion = '1.1'
110
    ) {
111 7
        $this->sapiEmitter = $sapiEmitter;
112
113
        // Code below based on ServerRequestFactory::fromGlobals
114 7
        $this->server          = ServerRequestFactory::normalizeServer($server ?? $_SERVER);
115 7
        $this->files           = ServerRequestFactory::normalizeFiles($files ?? $_FILES);
116 7
        $this->headers         = ServerRequestFactory::marshalHeaders($this->server);
117 7
        $this->uri             = ServerRequestFactory::marshalUriFromServer($this->server, $this->headers);
118 7
        $this->method          = ServerRequestFactory::get('REQUEST_METHOD', $this->server, 'GET');
119 7
        $this->cookies         = $cookies ?? $_COOKIE;
120 7
        $this->queryParams     = $queryParams ?? $_GET;
121 7
        $this->parsedBody      = $parsedBody ?? $_POST;
122 7
        $this->messageBody     = $messageBody;
123 7
        $this->protocolVersion = $protocolVersion;
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129 5
    public function getServer(): array
130
    {
131 5
        return $this->server;
132
    }
133
134
    /**
135
     * @inheritdoc
136
     */
137 5
    public function getFiles(): array
138
    {
139 5
        return $this->files;
140
    }
141
142
    /**
143
     * @inheritdoc
144
     */
145 5
    public function getHeaders(): array
146
    {
147 5
        return $this->headers;
148
    }
149
150
    /**
151
     * @inheritdoc
152
     */
153 7
    public function getUri(): UriInterface
154
    {
155 7
        return $this->uri;
156
    }
157
158
    /**
159
     * @inheritdoc
160
     */
161 7
    public function getMethod(): string
162
    {
163 7
        return $this->method;
164
    }
165
166
    /**
167
     * @inheritdoc
168
     */
169 5
    public function getCookies(): array
170
    {
171 5
        return $this->cookies;
172
    }
173
174
    /**
175
     * @inheritdoc
176
     */
177 5
    public function getQueryParams(): array
178
    {
179 5
        return $this->queryParams;
180
    }
181
182
    /**
183
     * @inheritdoc
184
     */
185 5
    public function getParsedBody()
186
    {
187 5
        return $this->parsedBody;
188
    }
189
190
    /**
191
     * @inheritdoc
192
     */
193 5
    public function getRequestBody()
194
    {
195 5
        return $this->messageBody;
196
    }
197
198
    /**
199
     * @inheritdoc
200
     */
201 5
    public function getProtocolVersion(): string
202
    {
203 5
        return $this->protocolVersion;
204
    }
205
206
    /**
207
     * @inheritdoc
208
     */
209 1
    public function handleResponse(ResponseInterface $response): void
210
    {
211 1
        $this->sapiEmitter->emit($response);
212
    }
213
}
214