BaseApiResponse::send()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
c 1
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Kelemen\ApiNette\Response;
4
5
use Nette\Http\IRequest;
6
use Nette\Http\IResponse;
7
use DateTimeInterface;
8
9
abstract class BaseApiResponse implements ApiResponse
10
{
11
    /** @var int */
12
    protected $code;
13
14
    /** @var mixed */
15
    protected $data;
16
17
    /** @var string */
18
    protected $contentType;
19
20
    /** @var string */
21
    protected $charset;
22
23
    /** @var string|int|DateTimeInterface */
24
    protected $expiration = 0;
25
26
    /**
27
     * @param int $code
28
     * @param mixed $data
29
     * @param string $contentType
30
     * @param string $charset
31
     */
32 22
    public function __construct($code, $data, $contentType, $charset)
33
    {
34 22
        $this->code = $code;
35 22
        $this->data = $data;
36 22
        $this->contentType = $contentType;
37 22
        $this->charset = $charset;
38 22
    }
39
40
    /**
41
     * @param string|int|DateTimeInterface $expiration
42
     * @return BaseApiResponse
43
     */
44
    public function setExpiration($expiration)
45
    {
46
        $this->expiration = $expiration;
47
        return $this;
48
    }
49
50
    /**
51
     * Return api response http code
52
     * @return integer
53
     */
54 22
    public function getCode()
55
    {
56 22
        return $this->code;
57
    }
58
59
    /**
60
     * @return mixed
61
     */
62 22
    public function getData()
63
    {
64 22
        return $this->data;
65
    }
66
67
    /**
68
     * Returns the MIME content type of a downloaded file.
69
     * @return string
70
     */
71 12
    public function getContentType()
72
    {
73 12
        return $this->contentType;
74
    }
75
76
    /**
77
     * Return encoding charset for http response
78
     * @return string
79
     */
80 12
    public function getCharset()
81
    {
82 12
        return $this->charset;
83
    }
84
85
    /**
86
     * @param IRequest $httpRequest
87
     * @param IResponse $httpResponse
88
     */
89
    public function send(IRequest $httpRequest, IResponse $httpResponse)
90
    {
91
        $httpResponse->setCode($this->getCode());
92
        $httpResponse->setContentType($this->getContentType(), $this->charset);
93
        $httpResponse->setExpiration($this->expiration);
94
        $result = $this->getEncodedData();
95
        $httpResponse->setHeader('Content-Length', strlen($result));
96
        echo $result;
97
    }
98
}
99