Completed
Pull Request — master (#61)
by Michal
08:40
created

JsonApiResponse::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Tomaj\NetteApi\Response;
4
5
use DateTimeInterface;
6
use Nette\Http\IRequest;
7
use Nette\Http\IResponse;
8
use Nette\SmartObject;
9
use Nette\Utils\Json;
10
11
class JsonApiResponse implements ResponseInterface
12
{
13
    use SmartObject;
14
15
    /** @var integer */
16
    private $code;
17
18
    /** @var mixed */
19
    private $payload;
20
21
    /** @var string */
22
    private $contentType;
23
24
    /** @var string */
25
    private $charset;
26
27
    /** @var bool|DateTimeInterface|int|string */
28
    private $expiration;
29
30
    /**
31
     * This class is only copy of JsonResponse from Nette with added possibility
32
     * to setup response code, content type, charset and expiration
33
     *
34
     * @param integer $code
35
     * @param mixed $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
36
     * @param string $contentType
37
     * @param string $charset
38
     * @param bool|DateTimeInterface|int|string $expiration
39
     */
40 21
    public function __construct($code, $payload, $contentType = 'application/json', $charset = 'utf-8', $expiration = false)
41
    {
42 21
        $this->code = $code;
43 21
        $this->payload = $payload;
44 21
        $this->contentType = $contentType ?: 'application/json';
45 21
        $this->charset = $charset;
46 21
        $this->expiration = $expiration;
47 21
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 21
    public function getCode(): int
53
    {
54 21
        return $this->code;
55
    }
56
57
    /**
58
     * @return mixed
59
     */
60 21
    public function getPayload()
61
    {
62 21
        return $this->payload;
63
    }
64
65 9
    public function getContentType(): string
66
    {
67 9
        return $this->contentType;
68
    }
69
70 9
    public function getCharset(): string
71
    {
72 9
        return $this->charset;
73
    }
74
75
    /**
76
     * @return bool|DateTimeInterface|int|string
77
     */
78 3
    public function getExpiration()
79
    {
80 3
        return $this->expiration;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 3
    public function send(IRequest $httpRequest, IResponse $httpResponse): void
87
    {
88 3
        $httpResponse->setContentType($this->getContentType(), $this->getCharset());
89 3
        $httpResponse->setExpiration($this->getExpiration());
90 3
        $result = Json::encode($this->getPayload());
91 3
        $httpResponse->setHeader('Content-Length', strlen($result));
92 3
        echo $result;
93 3
    }
94
}
95