Completed
Push — feature/VSVGVQ-44 ( 8fbc9c...df9f84 )
by Luc
03:16
created

QuestionViewController::createQuestionForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\Question\Controllers;
4
5
use Ramsey\Uuid\UuidFactoryInterface;
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Symfony\Component\Form\FormInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use VSV\GVQ_API\Common\ValueObjects\Languages;
11
use VSV\GVQ_API\Image\Controllers\ImageController;
12
use VSV\GVQ_API\Question\Forms\QuestionFormType;
13
use VSV\GVQ_API\Question\Models\Question;
14
use VSV\GVQ_API\Question\Repositories\CategoryRepository;
15
use VSV\GVQ_API\Question\Repositories\QuestionRepository;
16
17
class QuestionViewController extends AbstractController
18
{
19
    /**
20
     * @var UuidFactoryInterface
21
     */
22
    private $uuidFactory;
23
24
    /**
25
     * @var QuestionRepository
26
     */
27
    private $questionRepository;
28
29
    /**
30
     * @var CategoryRepository
31
     */
32
    private $categoryRepository;
33
34
    /**
35
     * @var ImageController
36
     */
37
    private $imageController;
38
39
    /**
40
     * @var QuestionFormType
41
     */
42
    private $questionFormType;
43
44
    /**
45
     * @param UuidFactoryInterface $uuidFactory
46
     * @param QuestionRepository $questionRepository
47
     * @param CategoryRepository $categoryRepository
48
     * @param ImageController $imageController
49
     */
50
    public function __construct(
51
        UuidFactoryInterface $uuidFactory,
52
        QuestionRepository $questionRepository,
53
        CategoryRepository $categoryRepository,
54
        ImageController $imageController
55
    ) {
56
        $this->uuidFactory = $uuidFactory;
57
        $this->questionRepository = $questionRepository;
58
        $this->categoryRepository = $categoryRepository;
59
        $this->imageController = $imageController;
60
61
        $this->questionFormType = new QuestionFormType();
62
    }
63
64
    /**
65
     * @return Response
66
     */
67
    public function index(): Response
68
    {
69
        $questions = $this->questionRepository->getAll();
70
71
        return $this->render(
72
            'questions/index.html.twig',
73
            [
74
                'questions' => $questions ? $questions->toArray() : [],
75
            ]
76
        );
77
    }
78
79
    /**
80
     * @param Request $request
81
     * @return Response
82
     * @throws \League\Flysystem\FileExistsException
83
     */
84
    public function add(Request $request): Response
85
    {
86
        $form = $this->createQuestionForm(null);
87
        $form->handleRequest($request);
88
89
        if ($form->isSubmitted() && $form->isValid()) {
90
            $data = $form->getData();
91
92
            $fileName = $this->imageController->handleImage($data['image']);
93
94
            $question = $this->questionFormType->newQuestionFromData(
95
                $this->uuidFactory,
96
                $fileName,
97
                $data
98
            );
99
            $this->questionRepository->save($question);
100
101
            $this->addFlash('success', 'De nieuwe vraag is bewaard.');
102
        }
103
104
        return $this->render(
105
            'questions/add.html.twig',
106
            [
107
                'form' => $form->createView()
108
            ]
109
        );
110
    }
111
112
    /**
113
     * @param Request $request
114
     * @param string $id
115
     * @return Response
116
     */
117
    public function edit(Request $request, string $id): Response
118
    {
119
        $question = $this->questionRepository->getById(
120
            $this->uuidFactory->fromString($id)
121
        );
122
123
        if ($question) {
124
            $form = $this->createQuestionForm($question);
125
            $form->handleRequest($request);
126
127
            if ($form->isSubmitted() && $form->isValid()) {
128
                $question = $this->questionFormType->updateQuestionFromData(
129
                    $question,
130
                    $form->getData()
131
                );
132
                $this->questionRepository->update($question);
133
134
                $this->addFlash('success', 'De vraag is aangepast.');
135
            }
136
137
            return $this->render(
138
                'questions/add.html.twig',
139
                [
140
                    'form' => $form->createView()
141
                ]
142
            );
143
        } else {
144
            $this->addFlash('warning', 'Geen vraag gevonden.');
145
146
            return $this->render('questions/add.html.twig');
147
        }
148
    }
149
150
    /**
151
     * @param Request $request
152
     * @param string $id
153
     * @return Response
154
     */
155
    public function delete(Request $request, string $id): Response
156
    {
157
        if ($request->getMethod() === 'POST') {
158
            $this->questionRepository->delete(
159
                $this->uuidFactory->fromString($id)
160
            );
161
162
            $this->addFlash('success', 'De vraag is verwijderd.');
163
164
            return $this->redirectToRoute('questions_view_index');
165
        }
166
167
        return $this->render(
168
            'questions/delete.html.twig',
169
            [
170
                'id' => $id,
171
            ]
172
        );
173
    }
174
175
    /**
176
     * @param null|Question $question
177
     * @return FormInterface
178
     */
179
    private function createQuestionForm(?Question $question): FormInterface
180
    {
181
        $formBuilder = $this->createFormBuilder();
182
183
        $this->questionFormType->buildForm(
184
            $formBuilder,
185
            [
186
                'languages' => new Languages(),
187
                'categories' => $this->categoryRepository->getAll(),
188
                'question' => $question,
189
            ]
190
        );
191
192
        return $formBuilder->getForm();
193
    }
194
}
195