Completed
Pull Request — master (#17)
by Luc
07:43 queued 04:45
created

QuestionViewController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

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

3 Methods

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