JsonResponse::getCallback()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace MediaMonks\RestApi\Response;
4
5
use Symfony\Component\HttpFoundation\JsonResponse as BaseJsonResponse;
6
7
class JsonResponse extends BaseJsonResponse
8
    implements ExtendedResponseInterface
9
{
10
    protected mixed $customContent;
11
12
    /**
13
     * Constructor.
14
     *
15
     * @param mixed $data The response data
16 4
     * @param int $status The response status code
17
     * @param array $headers An array of response headers
18 4
     */
19
    public function __construct($data = null, int $status = 200, array $headers = [])
20 4
    {
21 2
        parent::__construct('', $status, $headers);
22 2
23
        if (null === $data) {
24 4
            $data = new \ArrayObject();
25 4
        }
26
27
        $this->setCustomContent($data);
28
    }
29
30
    /**
31
     * We need this because setData() does json encoding already and
32
     * this messes up the jsonp callback.
33
     * It is a performance hit to let it decode/encode a second time
34
     *
35 4
     * @param mixed $content
36
     * @return $this
37 4
     */
38
    public function setCustomContent(mixed $content): static
39 4
    {
40
        $this->customContent = $content;
41
42
        return $this;
43
    }
44
45 1
    public function getCustomContent(): mixed
46
    {
47 1
        return $this->customContent;
48
    }
49
50
    public function getContent(): string|false
51
    {
52
        return is_string($this->customContent) ? $this->customContent : json_encode($this->customContent);
53
    }
54
55
    public function setData(mixed $data = []): static
56
    {
57
        $this->setCustomContent($data);
58
        return parent::setData($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::setData($data) returns the type Symfony\Component\HttpFoundation\JsonResponse which includes types incompatible with the type-hinted return MediaMonks\RestApi\Response\JsonResponse.
Loading history...
59
    }
60
61
    /**
62
     * @return mixed
63
     */
64
    public function getCallback()
65
    {
66
        return $this->callback;
67
    }
68
}
69