Passed
Push — master ( 92dbda...a34afa )
by Evgeniy
01:17
created

setContentTypeHeaderIfNotExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
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 130
    }
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