Passed
Push — master ( 7082b8...22c70c )
by Evgeniy
02:23
created

SapiNormalizer::normalizeUri()   B

Complexity

Conditions 9
Paths 72

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 72
nop 1
dl 0
loc 29
ccs 17
cts 17
cp 1
crap 9
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
     * @psalm-suppress MixedAssignment
51
     */
52 6
    public function normalizeUri(array $server): UriInterface
53
    {
54 6
        $uri = new Uri();
55
56 6
        if (isset($server['HTTPS']) && in_array(strtolower((string) $server['HTTPS']), ['on', '1'])) {
57 1
            $uri = $uri->withScheme('https');
58 6
        } elseif ($scheme = $server['HTTP_X_FORWARDED_PROTO'] ?? $server['REQUEST_SCHEME'] ?? '') {
59 1
            $uri = $uri->withScheme((string) $scheme);
60
        }
61
62 6
        if ($host = $server['HTTP_X_FORWARDED_HOST'] ?? $server['HTTP_HOST'] ?? '') {
63 2
            $uri = $uri->withHost((string) $host);
64 5
        } elseif ($host = $server['SERVER_NAME'] ?? $server['SERVER_ADDR'] ?? '') {
65 1
            $uri = $uri->withHost((string) $host);
66
        }
67
68 6
        if (isset($server['SERVER_PORT'])) {
69 1
            $uri = $uri->withPort((int) $server['SERVER_PORT']);
70
        }
71
72 6
        if ($path = $server['REQUEST_URI'] ?? $server['ORIG_PATH_INFO'] ?? '') {
73 2
            $uri = $uri->withPath(explode('?', preg_replace('/^[^\/:]+:\/\/[^\/]+/', '', (string) $path), 2)[0]);
74
        }
75
76 6
        if (isset($server['QUERY_STRING'])) {
77 2
            $uri = $uri->withQuery((string) $server['QUERY_STRING']);
78
        }
79
80 6
        return $uri;
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     *
86
     * @psalm-suppress MixedAssignment
87
     */
88 6
    public function normalizeHeaders(array $server): array
89
    {
90 6
        $headers = [];
91
92 6
        foreach ($server as $name => $value) {
93 4
            if (!is_string($name) ||  $value === '') {
94 2
                continue;
95
            }
96
97 4
            if (strpos($name, 'REDIRECT_') === 0) {
98 1
                if (array_key_exists($name = substr($name, 9), $server)) {
99 1
                    continue;
100
                }
101
            }
102
103 4
            if (strpos($name, 'HTTP_') === 0) {
104 2
                $headers[$this->normalizeHeaderName(substr($name, 5))] = $value;
105
            }
106
107 4
            if (strpos($name, 'CONTENT_') === 0) {
108 2
                $headers[$this->normalizeHeaderName($name)] = $value;
109
            }
110
        }
111
112 6
        return $headers;
113
    }
114
115
    /**
116
     * @param string $name
117
     * @return string
118
     */
119 2
    private function normalizeHeaderName(string $name): string
120
    {
121 2
        return str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', $name))));
122
    }
123
}
124