Completed
Push — master ( 104e68...b4839f )
by Maxime
12s
created

AbstractHttpMessage   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 87
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setHttpVersion() 0 6 1
A addHeader() 0 9 2
A getHeader() 0 4 2
A getHttpVersion() 0 4 1
A getHeaders() 0 4 2
A initHeaders() 0 7 2
A createNotHttpException() 0 6 1
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
/**
17
 * Class AbstractHttpMessage
18
 *
19
 * @internal
20
 */
21
abstract class AbstractHttpMessage
22
{
23
    /**
24
     * @var HttpHeadersBag
25
     */
26
    private $headers;
27
28
    /**
29
     * @var string for example "HTTP/1.1"
30
     */
31
    private $httpVersion;
32
33
    /**
34
     * @param string $httpVersion
35
     * @return self
36
     */
37 19
    protected function setHttpVersion($httpVersion)
38
    {
39 19
        $this->httpVersion = $httpVersion;
40
41 19
        return $this;
42
    }
43
44
    /**
45
     * @param string $name
46
     * @param string $value
47
     * @return self
48
     */
49 17
    public function addHeader(string $name, string $value)
50
    {
51 17
        if (null === $this->headers) {
52 17
            $this->headers = new HttpHeadersBag();
53
        }
54 17
        $this->headers->add($name, $value);
55
56 17
        return $this;
57
    }
58
59
    /**
60
     * @param string $header
61
     * @param mixed  $default
62
     * @return string
63
     */
64 10
    public function getHeader(string $header, $default = null)
65
    {
66 10
        return $this->headers[$header] ?: $default;
67
    }
68
69
    /**
70
     * @return string
71
     */
72 8
    public function getHttpVersion()
73
    {
74 8
        return $this->httpVersion;
75
    }
76
77
    /**
78
     * @return array|HttpHeadersBag
79
     */
80 12
    public function getHeaders()
81
    {
82 12
        return $this->headers ?: new HttpHeadersBag();
83
    }
84
85
    /**
86
     * @param string[]              $headers
87
     * @param AbstractHttpMessage   $request
88
     */
89 12
    protected static function initHeaders(array $headers, AbstractHttpMessage $request)
90
    {
91 12
        foreach ($headers as $header) {
92 12
            $cuttedHeader = \explode(':', $header);
93 12
            $request->addHeader(\trim($cuttedHeader[0]), trim(str_replace($cuttedHeader[0] . ':', '', $header)));
94
        }
95 12
    }
96
97
    /**
98
     * @param string $line
99
     * @return HttpException
100
     */
101 2
    protected static function createNotHttpException($line)
102
    {
103 2
        return new HttpException(
104 2
            \sprintf('The message is not an http request. "%s" received.', $line)
105
        );
106
    }
107
}
108