TextResponse   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 5
eloc 7
dl 0
loc 35
ccs 6
cts 9
cp 0.6667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getData() 0 5 2
A send() 0 4 2
1
<?php
2
3
namespace kalanis\Restful\Application\Responses;
4
5
6
use kalanis\Restful\Mapping\IMapper;
7
use kalanis\Restful\Resource\Media;
8
use Nette\Http;
9
use stdClass;
10
11
12
/**
13
 * TextResponse
14
 * @package kalanis\Restful\Application\Responses
15
 */
16 1
class TextResponse extends BaseResponse
17
{
18
19
    /**
20
     * @param Media|stdClass|string|iterable<string|int, mixed> $media
21
     * @param IMapper $mapper
22
     * @param string|null $contentType
23
     */
24 1
    public function __construct(
25
        protected readonly Media|iterable|stdClass|string $media,
26
        IMapper                  $mapper,
27
        ?string                  $contentType = null,
28
    )
29
    {
30 1
        parent::__construct($mapper, $contentType);
31 1
    }
32
33
    /**
34
     * Get response data
35
     * @inheritDoc
36
     */
37
    public function getData(): iterable|stdClass|string
38
    {
39
        return ($this->media instanceof Media)
40
            ? $this->media->getContent()
41
            : $this->media;
42
    }
43
44
    /**
45
     * Sends response to output
46
     */
47
    public function send(Http\IRequest $httpRequest, Http\IResponse $httpResponse): void
48
    {
49 1
        $httpResponse->setContentType($this->contentType ?: 'text/plain', 'UTF-8');
50 1
        echo $this->mapper->stringify($this->media, $this->isPrettyPrint());
51 1
    }
52
}
53