SapiNormalizer::normalizeHeaders()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

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