QuestionController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 93
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 14 2
A delete() 0 10 1
A save() 0 14 1
A __construct() 0 10 1
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\Question\Controllers;
4
5
use Ramsey\Uuid\UuidFactoryInterface;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\Serializer\SerializerInterface;
9
use VSV\GVQ_API\Common\Serializers\JsonEnricher;
10
use VSV\GVQ_API\Question\Models\Question;
11
use VSV\GVQ_API\Question\Repositories\QuestionRepository;
12
13
class QuestionController
14
{
15
    /**
16
     * @var QuestionRepository
17
     */
18
    private $questionRepository;
19
20
    /**
21
     * @var SerializerInterface
22
     */
23
    private $serializer;
24
25
    /**
26
     * @var UuidFactoryInterface
27
     */
28
    private $uuidFactory;
29
30
    /**
31
     * @var JsonEnricher
32
     */
33
    private $jsonEnricher;
34
35
    /**
36
     * @param QuestionRepository $questionRepository
37
     * @param SerializerInterface $serializer
38
     * @param UuidFactoryInterface $uuidFactory
39
     * @param JsonEnricher $jsonEnricher
40
     */
41
    public function __construct(
42
        QuestionRepository $questionRepository,
43
        SerializerInterface $serializer,
44
        UuidFactoryInterface $uuidFactory,
45
        JsonEnricher $jsonEnricher
46
    ) {
47
        $this->questionRepository = $questionRepository;
48
        $this->serializer = $serializer;
49
        $this->uuidFactory = $uuidFactory;
50
        $this->jsonEnricher = $jsonEnricher;
51
    }
52
53
    /**
54
     * @param Request $request
55
     * @return Response
56
     */
57
    public function save(Request $request): Response
58
    {
59
        /** @var string $json */
60
        $json = $request->getContent();
61
        $json = $this->jsonEnricher->enrich($json);
62
63
        /** @var Question $question */
64
        $question = $this->serializer->deserialize($json, Question::class, 'json');
65
        $this->questionRepository->save($question);
66
67
        $response = new Response('{"id":"'.$question->getId()->toString().'"}');
68
        $response->headers->set('Content-Type', 'application/json');
69
70
        return $response;
71
    }
72
73
    /**
74
     * @param string $id
75
     * @return Response
76
     */
77
    public function delete(string $id): Response
78
    {
79
        $this->questionRepository->delete(
80
            $this->uuidFactory->fromString($id)
81
        );
82
83
        $response = new Response('{"id":"'.$id.'"}');
84
        $response->headers->set('Content-Type', 'application/json');
85
86
        return $response;
87
    }
88
89
    /**
90
     * @return Response
91
     */
92
    public function getAll(): Response
93
    {
94
        $questions = $this->questionRepository->getAll();
95
96
        if ($questions === null) {
97
            $response = new Response('[]');
98
        } else {
99
            $questionsAsJson = $this->serializer->serialize($questions, 'json');
100
            $response = new Response($questionsAsJson);
101
        }
102
103
        $response->headers->set('Content-Type', 'application/json');
104
105
        return $response;
106
    }
107
}
108