HeadersFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 57
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
B resolveHeaders() 0 21 6
1
<?php
2
namespace Kambo\Http\Message\Factories\Environment\Superglobal;
3
4
// \Http\Message
5
use Kambo\Http\Message\Headers;
6
use Kambo\Http\Message\Environment\Environment;
7
use Kambo\Http\Message\Factories\Environment\Interfaces\FactoryInterface;
8
9
/**
10
 * Create instance of Headers object from instance of Environment object
11
 *
12
 * @package Kambo\Http\Message\Factories\Environment\Superglobal
13
 * @author  Bohuslav Simek <[email protected]>
14
 * @license MIT
15
 */
16
class HeadersFactory implements FactoryInterface
17
{
18
    /**
19
     * Special HTTP headers without "HTTP_" prefix for resolving headers
20
     *
21
     * @var array
22
     */
23
    private $specialHeaders = [
24
        'CONTENT_TYPE' => true,
25
        'CONTENT_LENGTH' => true,
26
        'PHP_AUTH_USER' => true,
27
        'PHP_AUTH_PW' => true,
28
        'PHP_AUTH_DIGEST' => true,
29
        'AUTH_TYPE' => true,
30
    ];
31
32
    /**
33
     * Create instance of Headers object from instance of Environment object
34
     *
35
     * @param Environment $environment environment data
36
     *
37
     * @return Headers Instance of Headers object from environment
38
     */
39 6
    public function create(Environment $environment)
40
    {
41 6
        return new Headers($this->resolveHeaders($environment->getServer()));
42
    }
43
44
    /**
45
     * Resolve headers from provided array
46
     *
47
     * @param array $headersForResolve array compatible with $_SERVER superglobal variable
48
     *
49
     * @return Headers Instance of Headers object from environment
50
     */
51 6
    private function resolveHeaders($headersForResolve)
52
    {
53 6
        $headers = [];
54
55 6
        foreach ($headersForResolve as $name => $value) {
56 5
            if (strpos($name, 'REDIRECT_') === 0) {
57 3
                $name = substr($name, 9);
58
59
                // Do not replace existing variables
60 3
                if (array_key_exists($name, $headersForResolve)) {
61 2
                    continue;
62
                }
63 3
            }
64
65 5
            if (substr($name, 0, 5) == 'HTTP_' || isset($this->specialHeaders[$name])) {
66 5
                $headers[$name] = $value;
67 5
            }
68 6
        }
69
70 6
        return $headers;
71
    }
72
}
73