Passed
Push — master ( 478a0c...9f2f7c )
by Zlatin
01:26
created

MessageTrait   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 13.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 169
ccs 7
cts 53
cp 0.1321
rs 10
c 1
b 0
f 0
wmc 20

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaderLine() 0 3 1
A getHeader() 0 6 2
A withBody() 0 10 2
A getHeaders() 0 3 1
A withHeader() 0 15 3
A hasHeader() 0 3 1
A withProtocolVersion() 0 10 2
A getProtocolVersion() 0 3 1
A withoutHeader() 0 12 2
A getBody() 0 3 1
A withAddedHeader() 0 14 4
1
<?php
2
namespace DevOp\Core\Http\Traits;
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
     * @var StreamInterface
31
     */
32
    protected $stream;
33
34
    /**
35
     * @return StreamInterface
36
     */
37
    public function getBody()
38
    {
39
        return $this->body;
40
    }
41
42
    /**
43
     * @param StreamInterface $body
44
     * @return self
45
     */
46
    public function withBody(StreamInterface $body)
47
    {
48
        if ($body === $this->stream) {
49
            return $this;
50
        }
51
52
        $clone = clone $this;
53
        $clone->stream = $body;
54
55
        return $clone;
56
    }
57
58
    /**
59
     * @param string $name
60
     * @return boolean
61
     */
62
    public function hasHeader($name)
63
    {
64
        return isset($this->headersName[strtolower($name)]);
65
    }
66
67
    /**
68
     * @param string $name
69
     * @return mixed
70
     */
71
    public function getHeader($name)
72
    {
73
        if ($this->hasHeader($name)) {
74
            return [$this->headers[$this->headersName[strtolower($name)]]];
75
        }
76
        return [];
77
    }
78
79
    public function getHeaderLine($name)
80
    {
81
        return implode(', ', $this->getHeader($name));
82
    }
83
84
    /**
85
     * @return array)
86
     */
87
    public function getHeaders()
88
    {
89
        return $this->headers;
90
    }
91
92
    /**
93
     * @param string $name
94
     * @param mixed $value
95
     * @return self
96
     */
97
    public function withHeader($name, $value)
98
    {
99
        $clone = new $this;
100
101
        $normalize = strtolower($name);
102
103
        if ($clone->hasHeader($name)) {
104
            unset($clone->headers[$this->headersName[$normalize]]);
105
            unset($clone->headersName[$normalize]);
106
        }
107
108
        $clone->headersName[$normalize] = $name;
109
        $clone->headers[$normalize] = !is_array($value) ? [$value] : $value;
110
111
        return $clone;
112
    }
113
114
    /**
115
     * @param string $name
116
     * @param mixed $value
117
     * @return self
118
     */
119
    public function withAddedHeader($name, $value)
120
    {
121
        $clone = new $this;
122
123
        $normalize = strtolower($name);
124
125
        if ($clone->hasHeader($name)) {
126
            $clone->headers[$normalize] = array_merge($this->headers, (!is_array($value) ? [$value] : $value));
127
        } else {
128
            $clone->headersName[$normalize] = $name;
129
            $clone->headers[$normalize] = !is_array($value) ? [$value] : $value;
130
        }
131
132
        return $clone;
133
    }
134
135
    /**
136
     * @param string $name
137
     * @return self
138
     */
139
    public function withoutHeader($name)
140
    {
141
        $clone = new $this;
142
143
        $normalize = strtolower($name);
144
145
        if ($clone->hasHeader($name)) {
146
            unset($clone->headers[$this->headersName[$normalize]]);
147
            unset($clone->headersName[$normalize]);
148
        }
149
150
        return $clone;
151
    }
152
153
    /**
154
     * @return string
155
     */
156 4
    public function getProtocolVersion()
157
    {
158 4
        return $this->protocolVersion;
159
    }
160
161
    /**
162
     * @param string $version
163
     * @return self
164
     */
165 2
    public function withProtocolVersion($version)
166
    {
167 2
        if ($version === $this->protocolVersion) {
168
            return $this;
169
        }
170
171 2
        $clone = clone $this;
172 2
        $clone->protocolVersion = $version;
173
174 2
        return $clone;
175
    }
176
}
177