Passed
Push — master ( 9b6318...46ddd5 )
by Evgeniy
01:25
created

SapiNormalizer::normalizeUri()   B

Complexity

Conditions 9
Paths 72

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 9.1317

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 72
nop 1
dl 0
loc 29
ccs 15
cts 17
cp 0.8824
crap 9.1317
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HttpSoft\ServerRequest;
6
7
use HttpSoft\Message\Uri;
8
use Psr\Http\Message\UriInterface;
9
10
use function array_key_exists;
11
use function explode;
12
use function in_array;
13
use function is_string;
14
use function preg_replace;
15
use function strpos;
16
use function str_replace;
17
use function strtolower;
18
use function substr;
19
use function ucwords;
20
21
final class SapiNormalizer implements ServerNormalizerInterface
22
{
23
    /**
24
     * {@inheritDoc}
25
     */
26 6
    public function normalizeMethod(array $server): string
27
    {
28 6
        if (empty($server['REQUEST_METHOD'])) {
29 5
            return 'GET';
30
        }
31
32 1
        return (string) $server['REQUEST_METHOD'];
33
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38 6
    public function normalizeProtocolVersion(array $server): string
39
    {
40 6
        if (empty($server['SERVER_PROTOCOL'])) {
41 5
            return '1.1';
42
        }
43
44 1
        return str_replace('HTTP/', '', (string) $server['SERVER_PROTOCOL']);
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 6
    public function normalizeUri(array $server): UriInterface
51
    {
52 6
        $uri = new Uri();
53
54 6
        if (isset($server['HTTPS']) && in_array(strtolower((string) $server['HTTPS']), ['on', '1'])) {
55 1
            $uri = $uri->withScheme('https');
56 5
        } elseif ($scheme = $server['HTTP_X_FORWARDED_PROTO'] ?? $server['REQUEST_SCHEME'] ?? '') {
57
            $uri = $uri->withScheme((string) $scheme);
58
        }
59
60 6
        if ($host = $server['HTTP_X_FORWARDED_HOST'] ?? $server['HTTP_HOST'] ?? '') {
61 2
            $uri = $uri->withHost((string) $host);
62 4
        } elseif ($host = $server['SERVER_NAME'] ?? $server['SERVER_ADDR'] ?? '') {
63
            $uri = $uri->withHost((string) $host);
64
        }
65
66 6
        if (isset($server['SERVER_PORT'])) {
67 1
            $uri = $uri->withPort((int) $server['SERVER_PORT']);
68
        }
69
70 6
        if ($path = $server['REQUEST_URI'] ?? $server['ORIG_PATH_INFO'] ?? '') {
71 2
            $uri = $uri->withPath(explode('?', preg_replace('/^[^\/:]+:\/\/[^\/]+/', '', (string) $path), 2)[0]);
72
        }
73
74 6
        if (isset($server['QUERY_STRING'])) {
75 2
            $uri = $uri->withQuery((string) $server['QUERY_STRING']);
76
        }
77
78 6
        return $uri;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84 6
    public function normalizeHeaders(array $server): array
85
    {
86 6
        $headers = [];
87
88 6
        foreach ($server as $name => $value) {
89 4
            if (!is_string($name) ||  $value === '') {
90 2
                continue;
91
            }
92
93 4
            if (strpos($name, 'REDIRECT_') === 0) {
94
                if (array_key_exists($name = substr($name, 9), $server)) {
95
                    continue;
96
                }
97
            }
98
99 4
            if (strpos($name, 'HTTP_') === 0) {
100 2
                $headers[$this->normalizeHeaderName(substr($name, 5))] = $value;
101
            }
102
103 4
            if (strpos($name, 'CONTENT_') === 0) {
104 2
                $headers[$this->normalizeHeaderName($name)] = $value;
105
            }
106
        }
107
108 6
        return $headers;
109
    }
110
111
    /**
112
     * @param string $name
113
     * @return string
114
     */
115 2
    private function normalizeHeaderName(string $name): string
116
    {
117 2
        return str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $name))));
118
    }
119
}
120