Response   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 3
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A serialize() 0 6 1
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