Passed
Push — master ( 732152...809fdb )
by Florian
03:32
created

BaseApiController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 14
dl 0
loc 57
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSerializer() 0 3 1
A returnSemester() 0 3 1
A getSubscribedServices() 0 5 1
A returnJson() 0 13 2
A returnEvent() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the feedback project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Controller\Base;
13
14
use App\Entity\Event;
15
use App\Entity\Semester;
16
use Symfony\Component\HttpFoundation\JsonResponse;
17
use Symfony\Component\Serializer\SerializerInterface;
18
19
class BaseApiController extends BaseDoctrineController
20
{
21
    public static function getSubscribedServices()
22
    {
23
        return parent::getSubscribedServices() +
24
            [
25
                'serializer' => SerializerInterface::class,
26
            ];
27
    }
28
29
    /**
30
     * @return SerializerInterface
31
     */
32
    private function getSerializer()
33
    {
34
        return $this->get('serializer');
35
    }
36
37
    /**
38
     * @param Semester|Semester[] $semester
39
     *
40
     * @return JsonResponse
41
     */
42
    protected function returnSemester($semester)
43
    {
44
        return $this->returnJson($semester, ['name', 'events' => ['id', 'name', 'date']]);
45
    }
46
47
    /**
48
     * @param Event|Event[] $events
49
     *
50
     * @return JsonResponse
51
     */
52
    protected function returnEvent($events)
53
    {
54
        return $this->returnJson($events, ['id', 'name', 'date', 'template', 'categoryWhitelist']);
55
    }
56
57
    /**
58
     * @param $content
59
     * @param array $attributes
60
     *
61
     * @return JsonResponse
62
     */
63
    private function returnJson($content, $attributes = [])
64
    {
65
        $addition = \count($attributes) > 0 ? ['attributes' => $attributes] : [];
66
67
        return new JsonResponse(
68
            $this->getSerializer()->serialize(
69
                $content,
70
                'json',
71
                ['json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS | JSON_UNESCAPED_UNICODE] + $addition
72
            ),
73
            200,
74
            [],
75
            true
76
        );
77
    }
78
}
79