Passed
Push — master ( 9f2f7c...75ce57 )
by Zlatin
01:24
created

Request::__construct()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.6563

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 18
nop 5
dl 0
loc 25
ccs 14
cts 19
cp 0.7368
crap 6.6563
rs 8.439
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\RequestInterface;
7
8
class Request implements RequestInterface
9
{
10
11
    use Traits\MessageTrait;
12
    use Traits\RequestTrait;
13
14
    /**
15
     * 
16
     * @param string $method
17
     * @param UriInterface $uri
18
     * @param array $headers
19
     * @param string|resource|StreamInterface $body
20
     * @param string $protocolVersion
21
     */
22 22
    public function __construct($method, UriInterface $uri, array $headers = [], $body = 'php://temp', $protocolVersion = '1.1')
23
    {
24 22
        $this->method = $method;
25 22
        $this->uri = new Uri($uri);
26
27 22
        if ($body instanceof StreamInterface) {
28 2
            $this->body = $body;
29 21
        } else if (is_resource($body)) {
30
            $this->body = (new Factory\StreamFactory())->createStreamFromResource($body);
31
        } else {
32 20
            $this->body = (new Factory\StreamFactory())->createStreamFromFile($body, "wb+");
33
        }
34
35 22
        foreach ($headers AS $header => $value) {
36
            $normalize = strtolower($value);
37
            $this->headersName[$header] = $normalize;
38
            $this->headers[$normalize] = $value;
39 11
        }
40
        
41 22
        if (!$this->hasHeader('host')) {
42 22
            $this->headersName['host'] = 'Host';
43 22
            $this->headers['Host'] = [!empty($this->uri->getHost()) ? $this->uri->getHost() : 'localhost'];
44 11
        }
45
46 22
        $this->protocolVersion = $protocolVersion;
47 22
    }
48
}
49