Completed
Push — next ( bac8c4...aa6b76 )
by Jonathan
02:49
created

SuperglobalRequestAdapter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 50
wmc 8
lcom 1
cbo 0
ccs 19
cts 19
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRemoteAddr() 0 4 2
A getHeaders() 0 7 2
A serverToHeaders() 0 11 3
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 $server;
40
41
    /**
42
     * Create a new adapter for a superglobal $_SERVER-style array.
43
     *
44
     * @param string[] $server An array in a format like PHP's $_SERVER var.
45
     */
46 18
    public function __construct(array $server)
47
    {
48 18
        $this->server = $server;
49 18
    }
50
51 18
    public function getRemoteAddr()
52
    {
53 18
        return isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : null;
54
    }
55
56 18
    public function getHeaders()
57
    {
58 18
        if (!isset($this->headers)) {
59 18
            $this->headers = $this->serverToHeaders($this->server);
0 ignored issues
show
Bug introduced by
The property headers does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60 18
        }
61 18
        return $this->headers;
62
    }
63
64
    /**
65
     * Convert from $_SERVER-style format to normal header names.
66
     *
67
     * @param string[] $server The $_SERVER-style array.
68
     * @return string[] Array of headers with lowercased keys.
69
     */
70 18
    private static function serverToHeaders(array $server)
71
    {
72 18
        $headers = [];
73 18
        foreach ($server as $key => $value) {
74 17
            if (strpos($key, 'HTTP_') === 0) {
75 11
                $key = strtolower(str_replace("_", '-', substr($key, 5)));
76 11
                $headers[$key] = $value;
77 11
            }
78 18
        }
79 18
        return $headers;
80
    }
81
}
82