Completed
Pull Request — master (#17)
by Luc
05:28 queued 02:44
created

QuestionViewController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 92
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B add() 0 32 4
A __construct() 0 10 1
A index() 0 8 2
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\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use VSV\GVQ_API\Common\ValueObjects\Languages;
10
use VSV\GVQ_API\Image\Controllers\ImageController;
11
use VSV\GVQ_API\Question\Forms\QuestionFormDTO;
12
use VSV\GVQ_API\Question\Forms\QuestionFormType;
13
use VSV\GVQ_API\Question\Repositories\CategoryRepository;
14
use VSV\GVQ_API\Question\Repositories\QuestionRepository;
15
16
class QuestionViewController extends AbstractController
17
{
18
    /**
19
     * @var UuidFactoryInterface
20
     */
21
    private $uuidFactory;
22
23
    /**
24
     * @var QuestionRepository
25
     */
26
    private $questionRepository;
27
28
    /**
29
     * @var CategoryRepository
30
     */
31
    private $categoryRepository;
32
33
    /**
34
     * @var ImageController
35
     */
36
    private $imageController;
37
38
    /**
39
     * @param UuidFactoryInterface $uuidFactory
40
     * @param QuestionRepository $questionRepository
41
     * @param CategoryRepository $categoryRepository
42
     * @param ImageController $imageController
43
     */
44
    public function __construct(
45
        UuidFactoryInterface $uuidFactory,
46
        QuestionRepository $questionRepository,
47
        CategoryRepository $categoryRepository,
48
        ImageController $imageController
49
    ) {
50
        $this->uuidFactory = $uuidFactory;
51
        $this->questionRepository = $questionRepository;
52
        $this->categoryRepository = $categoryRepository;
53
        $this->imageController = $imageController;
54
    }
55
56
    /**
57
     * @return Response
58
     */
59
    public function index(): Response
60
    {
61
        $questions = $this->questionRepository->getAll();
62
63
        return $this->render(
64
            'questions/index.html.twig',
65
            [
66
                'questions' => $questions ? $questions->toArray() : [],
67
            ]
68
        );
69
    }
70
71
    /**
72
     * @param Request $request
73
     * @return Response
74
     * @throws \League\Flysystem\FileExistsException
75
     */
76
    public function add(Request $request): Response
77
    {
78
        $categories = $this->categoryRepository->getAll();
79
80
        $questionFormDTO = new QuestionFormDTO();
81
82
        $form = $this->createForm(
83
            QuestionFormType::class,
84
            $questionFormDTO,
85
            [
86
                'languages' => new Languages(),
87
                'categories' => $categories,
88
            ]
89
        );
90
91
        $form->handleRequest($request);
92
93
        if ($form->isSubmitted() && $form->isValid()) {
94
            $fileName = $this->imageController->handleImage($questionFormDTO->image);
95
96
            $question = $questionFormDTO->toQuestion(
97
                $this->uuidFactory,
98
                $fileName
99
            );
100
            $this->questionRepository->save($question);
101
        }
102
103
        return $this->render(
104
            'questions/add.html.twig',
105
            [
106
                'categories' => $categories ? $categories->toArray() : [],
107
                'form' => $form->createView()
108
            ]
109
        );
110
    }
111
}
112