Response::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Common;
5
6
use JMS\Serializer\SerializerBuilder;
7
use Symfony\Component\HttpFoundation\Response as HttpResponse;
8
9
/**
10
 * Serializer
11
 *
12
 * @author Thiago Paes <[email protected]>
13
 */
14
class Response extends HttpResponse
15
{
16
    /**
17
     * @inheritDoc
18
     */
19
    public function __construct($content, int $status = HttpResponse::HTTP_OK)
20
    {
21
        $data = [
22
            'data' => $content,
23
            'code' => $status,
24
        ];
25
26
        parent::__construct(self::serialize($data), $status, ['Content-type' => 'application/json']);
27
    }
28
29
    /**
30
     * Serialize data
31
     *
32
     * @param  mixed $data
33
     * @return string
34
     */
35
    private static function serialize($data): string
36
    {
37
        $serializer = SerializerBuilder::create()->build();
38
39
        return $serializer->serialize($data, 'json');
40
    }
41
}
42