Completed
Push — master ( 1bb378...803221 )
by Florian
04:44
created

BaseApiController::returnActiveEvent()   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
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\Answer;
15
use App\Entity\Event;
16
use App\Entity\Semester;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\Serializer\SerializerInterface;
19
20
class BaseApiController extends BaseDoctrineController
21
{
22
    public static function getSubscribedServices()
23
    {
24
        return parent::getSubscribedServices() +
25
            [
26
                'serializer' => SerializerInterface::class,
27
            ];
28
    }
29
30
    /**
31
     * @return SerializerInterface
32
     */
33
    private function getSerializer()
34
    {
35
        return $this->get('serializer');
36
    }
37
38
    /**
39
     * @param Semester|Semester[] $semester
40
     *
41
     * @return JsonResponse
42
     */
43
    protected function returnSemester($semester)
44
    {
45
        return $this->returnJson($semester, ['name', 'events' => ['id', 'name', 'date']]);
46
    }
47
48
    /**
49
     * @param Event|Event[] $events
50
     *
51
     * @return JsonResponse
52
     */
53
    protected function returnActiveEvent($events)
54
    {
55
        return $this->returnJson($events, ['id', 'name', 'date', 'template', 'categoryWhitelist']);
56
    }
57
58
    /**
59
     * @param Answer[]|Answer $answers
60
     *
61
     * @return JsonResponse
62
     */
63
    protected function returnAnswers($answers)
64
    {
65
        return $this->returnJson($answers, ['questionNumber', 'value']);
66
    }
67
68
    /**
69
     * @param Event|Event[] $events
70
     *
71
     * @return JsonResponse
72
     */
73
    protected function returnEventPublic($events)
74
    {
75
        return $this->returnJson($events, ['id', 'name', 'date', 'template', 'categoryWhitelist', 'publicFeedback' => [
76
            'questionNumber', 'value',
77
        ]]);
78
    }
79
80
    /**
81
     * @param Event|Event[] $events
82
     *
83
     * @return JsonResponse
84
     */
85
    protected function returnEventPrivate($events)
86
    {
87
        return $this->returnJson($events, ['id', 'name', 'date', 'template', 'categoryWhitelist', 'participants' => [
88
            'timeNeededInMinutes', 'answers' => [
89
                'questionNumber', 'value',
90
            ],
91
        ]]);
92
    }
93
94
    /**
95
     * @param $content
96
     * @param array $attributes
97
     *
98
     * @return JsonResponse
99
     */
100
    private function returnJson($content, $attributes = [])
101
    {
102
        $addition = \count($attributes) > 0 ? ['attributes' => $attributes] : [];
103
104
        return new JsonResponse(
105
            $this->getSerializer()->serialize(
106
                $content,
107
                'json',
108
                ['json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS | JSON_UNESCAPED_UNICODE] + $addition
109
            ),
110
            200,
111
            [],
112
            true
113
        );
114
    }
115
}
116