Completed
Pull Request — master (#100)
by Maxime
02:32
created

AbstractHttpMessage::initHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * This file is a part of a nekland library
4
 *
5
 * (c) Nekland <[email protected]>
6
 *
7
 * For the full license, take a look to the LICENSE file
8
 * on the root directory of this project
9
 */
10
11
namespace Nekland\Woketo\Http;
12
13
14
use Nekland\Woketo\Exception\Http\HttpException;
15
16
abstract class AbstractHttpMessage
17
{
18
    /**
19
     * @var HttpHeadersBag
20
     */
21
    private $headers;
22
23
    /**
24
     * @var string for example "HTTP/1.1"
25
     */
26
    private $httpVersion;
27
28
    /**
29
     * @param string $httpVersion
30
     * @return self
31
     */
32 17
    protected function setHttpVersion($httpVersion)
33
    {
34 17
        $this->httpVersion = $httpVersion;
35
36 17
        return $this;
37
    }
38
39
    /**
40
     * @param string $name
41
     * @param string $value
42
     * @return self
43
     */
44 15
    public function addHeader(string $name, string $value)
45
    {
46 15
        if (null === $this->headers) {
47 15
            $this->headers = new HttpHeadersBag();
48
        }
49 15
        $this->headers->add($name, $value);
50
51 15
        return $this;
52
    }
53
54
    /**
55
     * @param string $header
56
     * @return string
57
     */
58 8
    public function getHeader($header)
59
    {
60 8
        return $this->headers[$header];
61
    }
62
63
    /**
64
     * @return string
65
     */
66 8
    public function getHttpVersion()
67
    {
68 8
        return $this->httpVersion;
69
    }
70
71
    /**
72
     * @return array|HttpHeadersBag
73
     */
74 10
    public function getHeaders()
75
    {
76 10
        return $this->headers;
77
    }
78
79
    /**
80
     * @param string[]              $headers
81
     * @param AbstractHttpMessage   $request
82
     */
83 12
    protected static function initHeaders(array $headers, AbstractHttpMessage $request)
84
    {
85 12
        foreach ($headers as $header) {
86 12
            $cuttedHeader = \explode(':', $header);
87 12
            $request->addHeader(\trim($cuttedHeader[0]), trim(str_replace($cuttedHeader[0] . ':', '', $header)));
88
        }
89 12
    }
90
91 2
    protected static function createNotHttpException($line)
92
    {
93 2
        return new HttpException(
94 2
            \sprintf('The message is not an http request. "%s" received.', $line)
95
        );
96
    }
97
}
98