JsonResponse   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 60
ccs 12
cts 12
cp 1
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getContent() 0 3 2
A setData() 0 4 1
A setCustomContent() 0 5 1
A getCustomContent() 0 3 1
A __construct() 0 9 2
A getCallback() 0 3 1
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