Passed
Push — master ( b65012...5ddc87 )
by Zlatin
01:29
created

MessageTrait::withoutHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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