Completed
Push — master ( 825c9b...9f04f3 )
by Florian
05:31
created

BaseApiController::getSerializer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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 returnActiveEvent($events)
53
    {
54
        return $this->returnJson($events, ['id', 'name', 'date', 'template', 'categoryWhitelist']);
55
    }
56
57
    /**
58
     * @param Event|Event[] $events
59
     *
60
     * @return JsonResponse
61
     */
62
    protected function returnEventPublic($events)
63
    {
64
        return $this->returnJson($events, ['id', 'name', 'date', 'template', 'categoryWhitelist', 'publicFeedback' => [
65
            'questionNumber', 'value',
66
        ]]);
67
    }
68
69
    /**
70
     * @param Event|Event[] $events
71
     *
72
     * @return JsonResponse
73
     */
74
    protected function returnEventPrivate($events)
75
    {
76
        return $this->returnJson($events, ['id', 'name', 'date', 'template', 'categoryWhitelist', 'participants' => [
77
            'timeNeededInMinutes', 'answers' => [
78
                'questionNumber', 'value',
79
            ],
80
        ]]);
81
    }
82
83
    /**
84
     * @param $content
85
     * @param array $attributes
86
     *
87
     * @return JsonResponse
88
     */
89
    private function returnJson($content, $attributes = [])
90
    {
91
        $addition = \count($attributes) > 0 ? ['attributes' => $attributes] : [];
92
93
        return new JsonResponse(
94
            $this->getSerializer()->serialize(
95
                $content,
96
                'json',
97
                ['json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS | JSON_UNESCAPED_UNICODE] + $addition
98
            ),
99
            200,
100
            [],
101
            true
102
        );
103
    }
104
}
105