Completed
Push — master ( 1ba115...baab82 )
by Nico
02:35
created

AbstractHeaderProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 88
ccs 32
cts 40
cp 0.8
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
getRawHeaders() 0 1 ?
A getHeadersFromUrl() 0 10 2
A getHeadersFromString() 0 17 2
A headerArrayToBag() 0 12 3
A getRawHeadersFromRedirectingUrl() 0 16 3
A getNameAndValue() 0 8 1
A createHeader() 0 4 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * @license  http://opensource.org/licenses/mit-license.php MIT
5
 * @link     https://github.com/nicoSWD
6
 * @author   Nicolas Oelgart <[email protected]>
7
 */
8
namespace nicoSWD\SecHeaderCheck\Domain\Header;
9
10
use nicoSWD\SecHeaderCheck\Domain\Header\Exception\TooManyRedirectsException;
11
use nicoSWD\SecHeaderCheck\Domain\URL\URL;
12
13
abstract class AbstractHeaderProvider implements HeaderProviderInterface
14
{
15
    /** @var int */
16
    private $maxRedirects;
17
    /** @var int */
18
    protected $connectionTimeout;
19
20
    public function __construct(int $maxRedirects, int $connectionTimeout)
21
    {
22
        $this->maxRedirects = $maxRedirects;
23
        $this->connectionTimeout = $connectionTimeout;
24
    }
25
26
    abstract public function getRawHeaders(URL $url): string;
27
28 2
    public function getHeadersFromUrl(URL $url, bool $followRedirects = true): HttpHeaderBag
29
    {
30 2
        $headers = $this->getHeadersFromString($this->getRawHeaders($url));
31
32 2
        if ($followRedirects) {
33 2
            $headers = $this->getRawHeadersFromRedirectingUrl($url, $headers);
34
        }
35
36 2
        return $headers;
37
    }
38
39 2
    public function getHeadersFromString(string $headers): HttpHeaderBag
40
    {
41 2
        $headersLines = preg_split('~\r?\n~', $headers, -1, PREG_SPLIT_NO_EMPTY);
42 2
        $parsedHeaders = [];
43
44 2
        $firstLine = array_shift($headersLines);
45
46 2
        [$headerName, $headerValue] = explode(' ', $firstLine, 2);
0 ignored issues
show
Bug introduced by
The variable $headerName does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $headerValue does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
47 2
        $parsedHeaders[$headerName][] = $headerValue;
48
49 2
        foreach ($headersLines as $line) {
50 2
            [$headerName, $headerValue] = $this->getNameAndValue($line);
51 2
            $parsedHeaders[$headerName][] = $headerValue;
52
        }
53
54 2
        return $this->headerArrayToBag($parsedHeaders);
55
    }
56
57 2
    private function headerArrayToBag(array $headers): HttpHeaderBag
58
    {
59 2
        $headerBag = new HttpHeaderBag();
60
61 2
        foreach ($headers as $headerName => $values) {
62 2
            foreach ($values as $value) {
63 2
                $headerBag->add($this->createHeader($headerName, $value));
64
            }
65
        }
66
67 2
        return $headerBag;
68
    }
69
70 2
    private function getRawHeadersFromRedirectingUrl(URL $url, HttpHeaderBag $headers): HttpHeaderBag
71
    {
72 2
        $numRedirects = 0;
73
74 2
        while ($headers->has('location')) {
75
            if (++$numRedirects > $this->maxRedirects) {
76
                throw new TooManyRedirectsException();
77
            }
78
79
            $headers = $this->getHeadersFromString(
80
                $this->getRawHeaders($url->redirectTo($headers->getFirst('location')))
81
            );
82
        }
83
84 2
        return $headers;
85
    }
86
87 2
    protected function getNameAndValue(string $line): array
88
    {
89 2
        $parts = explode(':', $line, 2);
90 2
        $headerName = trim(strtolower($parts[0]));
91 2
        $headerValue = trim($parts[1] ?? '');
92
93 2
        return [$headerName, $headerValue];
94
    }
95
96 2
    private function createHeader(string $headerName, string $value): HttpHeader
97
    {
98 2
        return new HttpHeader(strtolower(trim($headerName)), $value);
99
    }
100
}
101