ResponseExtensionTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 9
c 1
b 0
f 0
dl 0
loc 29
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setContentTypeHeaderIfNotExists() 0 5 2
A createBody() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HttpSoft\Response;
6
7
use HttpSoft\Message\ResponseTrait;
8
use HttpSoft\Message\Stream;
9
10
/**
11
 * Trait extends the standard functionality defined in `Psr\Http\Message\ResponseInterface`.
12
 *
13
 * @see https://github.com/php-fig/http-message/tree/master/src/ResponseInterface.php
14
 */
15
trait ResponseExtensionTrait
16
{
17
    use ResponseTrait;
18
19
    /**
20
     * Sets the provided Content-Type, if none is already present.
21
     *
22
     * @param string $contentType
23
     */
24 130
    private function setContentTypeHeaderIfNotExists(string $contentType): void
25
    {
26 130
        if (!$this->hasHeader('content-type')) {
27 130
            $this->headerNames['content-type'] = 'Content-Type';
28 130
            $this->headers[$this->headerNames['content-type']] = [$contentType];
29
        }
30
    }
31
32
    /**
33
     * Create the message body.
34
     *
35
     * @param string $content
36
     * @return Stream
37
     */
38 130
    private function createBody(string $content = ''): Stream
39
    {
40 130
        $stream = new Stream();
41 130
        $stream->write($content);
42 130
        $stream->rewind();
43 130
        return $stream;
44
    }
45
}
46