Completed
Push — master ( f3e7b4...3000ca )
by Luc
11s
created

QuestionController::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\Serializer\SerializerInterface;
9
use VSV\GVQ_API\Question\Models\Question;
10
use VSV\GVQ_API\Question\Repositories\QuestionRepository;
11
12
class QuestionController
13
{
14
    /**
15
     * @var QuestionRepository
16
     */
17
    private $questionRepository;
18
19
    /**
20
     * @var SerializerInterface
21
     */
22
    private $serializer;
23
24
    /**
25
     * @var UuidFactoryInterface
26
     */
27
    private $uuidFactory;
28
29
    /**
30
     * @param QuestionRepository $questionRepository
31
     * @param SerializerInterface $serializer
32
     * @param UuidFactoryInterface $uuidFactory
33
     */
34
    public function __construct(
35
        QuestionRepository $questionRepository,
36
        SerializerInterface $serializer,
37
        UuidFactoryInterface $uuidFactory
38
    ) {
39
        $this->questionRepository = $questionRepository;
40
        $this->serializer = $serializer;
41
        $this->uuidFactory = $uuidFactory;
42
    }
43
44
    /**
45
     * @param Request $request
46
     * @return Response
47
     */
48
    public function save(Request $request): Response
49
    {
50
        $json = $request->getContent();
51
        /** @var Question $question */
52
        $question = $this->serializer->deserialize($json, Question::class, 'json');
53
        $this->questionRepository->save($question);
54
55
        $response = new Response('{"id":"'.$question->getId()->toString().'"}');
56
        $response->headers->set('Content-Type', 'application/json');
57
58
        return $response;
59
    }
60
61
    /**
62
     * @param string $id
63
     * @return Response
64
     */
65
    public function delete(string $id): Response
66
    {
67
        $this->questionRepository->delete(
68
            $this->uuidFactory->fromString($id)
69
        );
70
71
        $response = new Response('{"id":"'.$id.'"}');
72
        $response->headers->set('Content-Type', 'application/json');
73
74
        return $response;
75
    }
76
77
    /**
78
     * @return Response
79
     */
80
    public function getAll(): Response
81
    {
82
        $questions = $this->questionRepository->getAll();
83
84
        if ($questions === null) {
85
            $response = new Response('[]');
86
        } else {
87
            $questionsAsJson = $this->serializer->serialize($questions, 'json');
88
            $response = new Response($questionsAsJson);
89
        }
90
91
        $response->headers->set('Content-Type', 'application/json');
92
93
        return $response;
94
    }
95
}
96