JsonpResponse   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 10
dl 0
loc 33
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A send() 0 11 3
1
<?php
2
3
namespace kalanis\Restful\Application\Responses;
4
5
6
use kalanis\Restful\Exceptions\InvalidArgumentException;
7
use kalanis\Restful\Mapping\IMapper;
8
use Nette\Http\IRequest;
9
use Nette\Http\IResponse as nette_iresponse;
10
use Nette\Utils\Strings;
11
12
13
/**
14
 * JSONP response
15
 * @package kalanis\Restful\Application\Responses
16
 */
17 1
class JsonpResponse extends BaseResponse
18
{
19
20
    /**
21
     * @param array<mixed> $data
22
     * @param IMapper $mapper
23
     * @param string|null $contentType
24
     */
25
    public function __construct(
26
        array   $data,
27
        IMapper $mapper,
28
        ?string $contentType = null,
29
    )
30
    {
31 1
        parent::__construct($mapper, $contentType);
32 1
        $this->data = $data;
33 1
    }
34
35
    /**
36
     * Send JSONP response to output
37
     * @throws InvalidArgumentException
38
     */
39
    public function send(IRequest $httpRequest, nette_iresponse $httpResponse): void
40
    {
41 1
        $httpResponse->setContentType($this->getContentType() ?: 'application/javascript', 'UTF-8');
42
43 1
        $data = [];
44 1
        $data['response'] = $this->data;
45 1
        $data['status'] = $httpResponse->getCode();
46 1
        $data['headers'] = $httpResponse->getHeaders();
47
48 1
        $callback = $httpRequest->getQuery('jsonp') ? Strings::webalize(strval($httpRequest->getQuery('jsonp')), null, false) : '';
49 1
        echo $callback . '(' . $this->mapper->stringify($data, $this->isPrettyPrint()) . ');';
50 1
    }
51
}
52