SuperglobalRequestAdapter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 52
ccs 15
cts 15
cp 1
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A serverToHeaders() 0 10 3
A getRemoteAddr() 0 3 1
A __construct() 0 3 1
A getHeaders() 0 3 1
1
<?php
2
3
/*
4
The MIT License (MIT)
5
6
Copyright (c) 2015 Vectorface, Inc.
7
8
Permission is hereby granted, free of charge, to any person obtaining a copy
9
of this software and associated documentation files (the "Software"), to deal
10
in the Software without restriction, including without limitation the rights
11
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
copies of the Software, and to permit persons to whom the Software is
13
furnished to do so, subject to the following conditions:
14
15
The above copyright notice and this permission notice shall be included in
16
all copies or substantial portions of the Software.
17
18
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
THE SOFTWARE.
25
*/
26
27
namespace Vectorface\Whip\Request;
28
29
/**
30
 * Provide IP address data from the $_SERVER superglobal.
31
 */
32
class SuperglobalRequestAdapter implements RequestAdapter
33
{
34
    /**
35
     * The $_SERVER-style array that serves as the source of data.
36
     *
37
     * @var string[]
38
     */
39
    private array $server;
40
41
    /**
42
     * A formatted version of the HTTP headers: ["header" => "value", ...]
43
     *
44
     * @var string[]
45
     */
46
    private array $headers;
47
48
    /**
49
     * Create a new adapter for a superglobal $_SERVER-style array.
50
     *
51
     * @param string[] $server An array in a format like PHP's $_SERVER var.
52
     */
53 21
    public function __construct(array $server)
54
    {
55 21
        $this->server = $server;
56 21
    }
57
58 21
    public function getRemoteAddr() : ?string
59
    {
60 21
        return $this->server['REMOTE_ADDR'] ?? null;
61
    }
62
63 21
    public function getHeaders() : array
64
    {
65 21
        return $this->headers ??= $this->serverToHeaders($this->server);
66 21
    }
67
68 21
    /**
69
     * Convert from $_SERVER-style format to normal header names.
70
     *
71
     * @param string[] $server The $_SERVER-style array.
72
     * @return string[] Array of headers with lowercased keys.
73
     */
74
    private static function serverToHeaders(array $server) : array
75
    {
76
        $headers = [];
77 21
        foreach ($server as $key => $value) {
78
            if (str_starts_with($key, 'HTTP_')) {
79 21
                $key = strtolower(str_replace("_", '-', substr($key, 5)));
80 21
                $headers[$key] = $value;
81 20
            }
82 12
        }
83 20
        return $headers;
84
    }
85
}
86