Message   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 35
c 2
b 0
f 0
dl 0
loc 199
ccs 48
cts 48
cp 1
rs 10
wmc 17

12 Methods

Rating   Name   Duplication   Size   Complexity  
A hasHeader() 0 3 1
A withoutHeader() 0 14 2
A getHeaderLine() 0 3 2
A withAddedHeader() 0 9 1
A withBody() 0 9 1
A getHeaders() 0 3 1
A getHeader() 0 3 2
A withProtocolVersion() 0 7 1
A __construct() 0 16 3
A withHeader() 0 9 1
A getBody() 0 3 1
A getProtocolVersion() 0 3 1
1
<?php
2
3
namespace Zapheus\Bridge\Psr;
4
5
use Psr\Http\Message\MessageInterface;
6
use Psr\Http\Message\StreamInterface;
7
8
/**
9
 * Message
10
 *
11
 * @package Zapheus
12
 * @author  Rougin Gutib <[email protected]>
13
 */
14
class Message implements MessageInterface
15
{
16
    /**
17
     * @var \Psr\Http\Message\StreamInterface
18
     */
19
    protected $body;
20
21
    /**
22
     * @var array
23
     */
24
    protected $headers = array();
25
26
    /**
27
     * @var string
28
     */
29
    protected $version = '1.1';
30
31
    /**
32
     * Initializes the message instance.
33
     *
34
     * @param \Psr\Http\Message\StreamInterface|null $body
35
     * @param array                                  $headers
36
     * @param string                                 $version
37
     */
38 93
    public function __construct(StreamInterface $body = null, array $headers = array(), $version = '1.1')
39
    {
40 93
        if ($body === null)
41 31
        {
42 87
            $resource = fopen('php://temp', 'r+');
43
44 87
            $resource = $resource === false ? null : $resource;
45
46 87
            $body = new Stream($resource);
47 29
        }
48
49 93
        $this->body = $body;
50
51 93
        $this->headers = $headers;
52
53 93
        $this->version = $version;
54 93
    }
55
56
    /**
57
     * Returns the body of the message.
58
     *
59
     * @return \Psr\Http\Message\StreamInterface
60
     */
61 36
    public function getBody()
62
    {
63 36
        return $this->body;
64
    }
65
66
    /**
67
     * Retrieves a message header value by the given case-insensitive name.
68
     *
69
     * @param  string $name
70
     * @return string[]
71
     */
72 6
    public function getHeader($name)
73
    {
74 6
        return $this->hasHeader($name) ? $this->headers[$name] : array();
75
    }
76
77
    /**
78
     * Retrieves a comma-separated string of the values for a single header.
79
     *
80
     * @param  string $name
81
     * @return string
82
     */
83 3
    public function getHeaderLine($name)
84
    {
85 3
        return $this->hasHeader($name) ? implode(',', $this->headers[$name]) : '';
86
    }
87
88
    /**
89
     * Retrieves all message header values.
90
     *
91
     * @return string[][]
92
     */
93 36
    public function getHeaders()
94
    {
95 36
        return $this->headers;
96
    }
97
98
    /**
99
     * Retrieves the HTTP protocol version as a string.
100
     *
101
     * @return string
102
     */
103 36
    public function getProtocolVersion()
104
    {
105 36
        return $this->version;
106
    }
107
108
    /**
109
     * Retrieves a message header value by the given case-insensitive name.
110
     *
111
     * @param  string $name
112
     * @return string[]
113
     */
114 12
    public function hasHeader($name)
115
    {
116 12
        return isset($this->headers[$name]);
117
    }
118
119
    /**
120
     * Returns an instance with the specified header appended with the given value.
121
     *
122
     * @param  string          $name
123
     * @param  string|string[] $value
124
     * @return static
125
     *
126
     * @throws \InvalidArgumentException
127
     */
128 6
    public function withAddedHeader($name, $value)
129
    {
130
        // TODO: Add \InvalidArgumentException
131
132 6
        $static = clone $this;
133
134 6
        $static->headers[$name][] = $value;
135
136 6
        return $static;
137
    }
138
139
    /**
140
     * Returns an instance with the specified message body.
141
     *
142
     * @param  \Psr\Http\Message\StreamInterface $body
143
     * @return static
144
     *
145
     * @throws \InvalidArgumentException
146
     */
147 3
    public function withBody(StreamInterface $body)
148
    {
149
        // TODO: Add \InvalidArgumentException
150
151 3
        $static = clone $this;
152
153 3
        $static->body = $body;
154
155 3
        return $static;
156
    }
157
158
    /**
159
     * Returns an instance with the provided value replacing the specified header.
160
     *
161
     * @param  string          $name
162
     * @param  string|string[] $value
163
     * @return static
164
     *
165
     * @throws \InvalidArgumentException
166
     */
167 15
    public function withHeader($name, $value)
168
    {
169
        // TODO: Add \InvalidArgumentException
170
171 15
        $static = clone $this;
172
173 15
        $static->headers[$name] = (array) $value;
174
175 15
        return $static;
176
    }
177
178
    /**
179
     * Returns an instance with the specified HTTP protocol version.
180
     *
181
     * @param  string $version
182
     * @return static
183
     */
184 3
    public function withProtocolVersion($version)
185
    {
186 3
        $static = clone $this;
187
188 3
        $static->version = $version;
189
190 3
        return $static;
191
    }
192
193
    /**
194
     * Returns an instance without the specified header.
195
     *
196
     * @param  string $name
197
     * @return static
198
     */
199 3
    public function withoutHeader($name)
200
    {
201 3
        $instance = clone $this;
202
203 3
        if ($this->hasHeader($name))
204 1
        {
205 3
            $static = clone $this;
206
207 3
            unset($static->headers[$name]);
208
209 3
            $instance = $static;
210 1
        }
211
212 3
        return $instance;
213
    }
214
}
215