Message::getHeaders()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace CodeJet\Http;
4
5
use Doctrine\Instantiator\Exception\InvalidArgumentException;
6
use Psr\Http\Message\MessageInterface;
7
use Psr\Http\Message\StreamInterface;
8
9
abstract class Message implements MessageInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $protocolVersion = '1.1';
15
16
    /**
17
     * @var array[string]
18
     */
19
    private $validProtocolVersions = ['1.0', '1.1'];
20
21
    /**
22
     * @var array[string]
23
     */
24
    protected $headers = [];
25
26
    /**
27
     * @var StreamInterface
28
     */
29
    protected $body;
30
31
    /**
32
     * @var array[string]
33
     */
34
    protected $normalizedHeaderIndex = [];
35
36
    /**
37
     * @inheritDoc
38
     */
39 3
    public function getProtocolVersion()
40
    {
41 3
        return $this->protocolVersion;
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47 6
    public function withProtocolVersion($protocolVersion)
48
    {
49 6
        if (!in_array($protocolVersion, $this->validProtocolVersions)) {
50 3
            throw new InvalidArgumentException('Invalid Protocol Version.');
51
        }
52
53 3
        $clone = clone $this;
54 3
        $clone->protocolVersion = $protocolVersion;
55
56 3
        return $clone;
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62 9
    public function getHeaders()
63
    {
64 9
        return $this->headers;
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70 60
    public function hasHeader($name)
71
    {
72 60
        if (isset($this->normalizedHeaderIndex[strtolower($name)])) {
73 27
            return true;
74
        }
75
76 60
        return false;
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82 30
    public function getHeader($name)
83
    {
84 30
        if (!$this->hasHeader($name)) {
85 9
            return [];
86
        }
87
88 24
        return $this->headers[$this->normalizedHeaderIndex[strtolower($name)]];
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94 6
    public function getHeaderLine($name)
95
    {
96 6
        if (!$this->hasHeader($name)) {
97 3
            return '';
98
        }
99
100 3
        return implode(", ", $this->getHeader($name));
101
    }
102
103
    /**
104
     * @inheritDoc
105
     */
106 51
    public function withHeader($name, $value)
107
    {
108 51
        $clone = clone $this;
109
110 51
        if ($this->hasHeader($name)) {
111 6
            unset($clone->headers[$this->normalizedHeaderIndex[strtolower($name)]]);
112
        }
113
114 51
        $clone->normalizedHeaderIndex[strtolower($name)] = $name;
115 51
        $clone->headers[$name] = $value;
116
117 51
        return $clone;
118
    }
119
120
    /**
121
     * @inheritDoc
122
     */
123 6
    public function withAddedHeader($name, $value)
124
    {
125 6
        if (!$this->hasHeader($name)) {
126
            return $this->withHeader($name, $value);
127
        }
128
129 6
        $headerIndex = $this->normalizedHeaderIndex[strtolower($name)];
130
131 6
        $clone = clone $this;
132
133 6
        if (!is_array($clone->headers[$headerIndex])) {
134 6
            $clone->headers[$headerIndex] = [
135 6
                $clone->headers[$headerIndex]
136
            ];
137
        }
138
139 6
        array_push($clone->headers[$headerIndex], $value);
140
141 6
        return $clone;
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147 3
    public function withoutHeader($name)
148
    {
149 3
        if (!$this->hasHeader($name)) {
150
            return $this;
151
        }
152
153 3
        $clone = clone $this;
154 3
        $normalizedHeaderIndex = $this->normalizedHeaderIndex[strtolower($name)];
155 3
        unset($clone->headers[$normalizedHeaderIndex]);
156 3
        unset($clone->normalizedHeaderIndex[strtolower($name)]);
157
158 3
        return $clone;
159
    }
160
161
    /**
162
     * @inheritDoc
163
     */
164 3
    public function getBody()
165
    {
166 3
        return $this->body;
167
    }
168
169
    /**
170
     * @inheritDoc
171
     */
172 3
    public function withBody(StreamInterface $body)
173
    {
174 3
        $clone = clone $this;
175 3
        $clone->body = $body;
176
177 3
        return $clone;
178
    }
179
}
180