Completed
Push — master ( e0250b...fc23d8 )
by Tomas
02:44
created

JsonApiResponse::getCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Tomaj\NetteApi\Response;
4
5
use Nette\Application\Responses\JsonResponse;
6
use Nette;
7
8
class JsonApiResponse extends JsonResponse
9
{
10
    /**
11
     * @var integer
12
     */
13
    private $code;
14
15
    /**
16
     * @var string
17
     */
18
    private $charset;
19
20
    /**
21
     * Create JsonApiResponse
22
     * This class only wrap JsonResponse from Nette and add possibility
23
     * to setup response code and automaticaly set content type
24
     *
25
     * @param integer $code
26
     * @param mixed $data
27
     * @param string $contentType
28
     * @param string $charset
29
     */
30 18
    public function __construct($code, $data, $contentType = 'application/json', $charset = 'utf-8')
31
    {
32 18
        parent::__construct($data, $contentType);
33 18
        $this->charset = $charset;
34 18
        $this->code = $code;
35 18
    }
36
37
    /**
38
     * Return api response http code
39
     *
40
     * @return integer
41
     */
42 18
    public function getCode()
43
    {
44 18
        return $this->code;
45
    }
46
47
    /**
48
     * Return encoding charset for http response
49
     *
50
     * @return string
51
     */
52 6
    public function getCharset()
53
    {
54 6
        return $this->charset;
55
    }
56
57
    /**
58
     * Sends response to output.
59
     * @return void
60
     */
61 3
    public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
62
    {
63 3
        $httpResponse->setContentType($this->getContentType(), $this->charset);
64 3
        $httpResponse->setExpiration(false);
65 3
        $result = Nette\Utils\Json::encode($this->getPayload());
66 3
        $httpResponse->setHeader('Content-Length', strlen($result));
67 3
        echo $result;
68 3
    }
69
}
70