Completed
Pull Request — master (#17)
by Luc
06:56 queued 03:33
created

QuestionViewController::add()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 2
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 '.$question->getId()->toString().' is bewaard.');
102
            return $this->redirectToRoute('questions_view_index');
103
        }
104
105
        return $this->render(
106
            'questions/add.html.twig',
107
            [
108
                'form' => $form->createView()
109
            ]
110
        );
111
    }
112
113
    /**
114
     * @param Request $request
115
     * @param string $id
116
     * @return Response
117
     */
118
    public function edit(Request $request, string $id): Response
119
    {
120
        $question = $this->questionRepository->getById(
121
            $this->uuidFactory->fromString($id)
122
        );
123
124
        if (!$question) {
125
            $this->addFlash('warning', 'Geen vraag gevonden met id '.$id.' om aan te passen.');
126
            return $this->redirectToRoute('questions_view_index');
127
        }
128
129
        $form = $this->createQuestionForm($question);
130
        $form->handleRequest($request);
131
132
        if ($form->isSubmitted() && $form->isValid()) {
133
            $question = $this->questionFormType->updateQuestionFromData(
134
                $question,
135
                $form->getData()
136
            );
137
            $this->questionRepository->update($question);
138
139
            $this->addFlash('success', 'Vraag '.$id.' is aangepast.');
140
            return $this->redirectToRoute('questions_view_index');
141
        }
142
143
        return $this->render(
144
            'questions/add.html.twig',
145
            [
146
                'form' => $form->createView()
147
            ]
148
        );
149
    }
150
151
    /**
152
     * @param Request $request
153
     * @param string $id
154
     * @return Response
155
     */
156
    public function delete(Request $request, string $id): Response
157
    {
158
        if ($request->getMethod() === 'POST') {
159
            $this->questionRepository->delete(
160
                $this->uuidFactory->fromString($id)
161
            );
162
163
            $this->addFlash('success', 'Vraag '.$id.' is verwijderd.');
164
165
            return $this->redirectToRoute('questions_view_index');
166
        }
167
168
        return $this->render(
169
            'questions/delete.html.twig',
170
            [
171
                'id' => $id,
172
            ]
173
        );
174
    }
175
176
    /**
177
     * @param null|Question $question
178
     * @return FormInterface
179
     */
180
    private function createQuestionForm(?Question $question): FormInterface
181
    {
182
        $formBuilder = $this->createFormBuilder();
183
184
        $this->questionFormType->buildForm(
185
            $formBuilder,
186
            [
187
                'languages' => new Languages(),
188
                'categories' => $this->categoryRepository->getAll(),
189
                'question' => $question,
190
            ]
191
        );
192
193
        return $formBuilder->getForm();
194
    }
195
}
196