Test Failed
Push — master ( c1d2e0...874a5e )
by Sven
13:19
created

ToDoController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 11
Bugs 1 Features 3
Metric Value
wmc 6
eloc 21
c 11
b 1
f 3
dl 0
loc 142
rs 10
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\ToDo;
6
use App\Repository\ToDoRepository;
7
use App\Service\ToDoSerializer;
8
use App\Service\ToDoValidator;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Nelmio\ApiDocBundle\Annotation\Model;
11
use OpenApi\Annotations as OA;
12
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Routing\Annotation\Route;
16
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17
18
class ToDoController extends AbstractController
19
{
20
    public function __construct(
21
        private EntityManagerInterface $entityManager,
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_PRIVATE, expecting T_VARIABLE
Loading history...
22
        private ToDoRepository $toDoRepository,
23
        private ToDoSerializer $toDoSerializer,
24
        private ToDoValidator $toDoValidator,
25
        private UrlGeneratorInterface $urlGenerator
26
    ) {
27
    }
28
29
    /**
30
     * Add a new ToDo item
31
     *
32
     * @Route("/api/todos", name="add_todo", methods={"POST"})
33
     * @OA\Tag(name="ToDo")
34
     * @OA\RequestBody(
35
     *     request="ToDo",
36
     *     description="The ToDo item to be created",
37
     *     required=true,
38
     *     @OA\JsonContent(ref=@Model(type=ToDo::class, groups={"request"}))
39
     * )
40
     * @OA\Response(
41
     *     response=201,
42
     *     description="The created ToDo item",
43
     *     @OA\JsonContent(ref=@Model(type=ToDo::class))
44
     * )
45
     * @OA\Response(
46
     *     response=400,
47
     *     description="Bad Request",
48
     *     @OA\JsonContent(ref="#/components/schemas/BadRequestError")
49
     * ),
50
     * @OA\Response(response="default", description="Unexpected error", @OA\JsonContent(ref="#/components/schemas/ErrorModel"))
51
     */
52
    public function create(Request $request): JsonResponse
53
    {
54
        $toDo = $this->toDoSerializer->deserializeRequestIntoNew($request->getContent());
55
        $this->toDoValidator->validate($toDo);
56
57
        $this->entityManager->persist($toDo);
58
        $this->entityManager->flush();
59
60
        $resourceUrl = $this->urlGenerator->generate(
61
            'show_todo',
62
            ['id' => $toDo->getId()],
63
            UrlGeneratorInterface::ABSOLUTE_URL
64
        );
65
66
        return $this->json($toDo, JsonResponse::HTTP_CREATED, ['Location' => $resourceUrl, ]);
67
    }
68
69
    /**
70
     * List all ToDo items
71
     *
72
     * @Route("/api/todos", name="list_todos", methods={"GET"})
73
     * @OA\Tag(name="ToDo")
74
     * @OA\Response(
75
     *     response=200,
76
     *     description="All existing ToDo items",
77
     *     @OA\JsonContent(
78
     *        type="array",
79
     *        @OA\Items(ref=@Model(type=ToDo::class))
80
     *     )
81
     * ),
82
     * @OA\Response(response="default", description="Unexpected error", @OA\JsonContent(ref="#/components/schemas/ErrorModel"))
83
     */
84
    public function list(): JsonResponse
85
    {
86
        $toDos = $this->toDoRepository->findAll();
87
88
        return $this->json($toDos);
89
    }
90
91
    /**
92
     * Show a single ToDo item
93
     *
94
     * @Route("/api/todos/{id<\d+>}", name="show_todo", methods={"GET"})
95
     * @OA\Tag(name="ToDo")
96
     * @OA\Response(
97
     *     response=200,
98
     *     description="ToDo item",
99
     *     @OA\JsonContent(ref=@Model(type=ToDo::class))
100
     * )
101
     * @OA\Response(response=404, description="ToDo item not found"),
102
     * @OA\Response(response="default", description="Unexpected error", @OA\JsonContent(ref="#/components/schemas/ErrorModel"))
103
     */
104
    public function show(ToDo $toDo): JsonResponse
105
    {
106
        return $this->json($toDo);
107
    }
108
109
    /**
110
     * Delete a ToDo item
111
     *
112
     * @Route("/api/todos/{id<\d+>}", name="delete_todo", methods={"DELETE"})
113
     * @OA\Tag(name="ToDo")
114
     * @OA\Response(
115
     *     response=204,
116
     *     description="ToDo deleted"
117
     * )
118
     * @OA\Response(response=404, description="ToDo item not found"),
119
     * @OA\Response(response="default", description="Unexpected error", @OA\JsonContent(ref="#/components/schemas/ErrorModel"))
120
     */
121
    public function delete(ToDo $toDo): JsonResponse
122
    {
123
        $this->entityManager->remove($toDo);
124
        $this->entityManager->flush();
125
126
        return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT);
127
    }
128
129
    /**
130
     * Update existing ToDo
131
     *
132
     * @Route("/api/todos/{id<\d+>}", name="update_todo", methods={"PUT"})
133
     * @OA\Tag(name="ToDo")
134
     * @OA\RequestBody(
135
     *     request="ToDo",
136
     *     description="The ToDo item to be updated",
137
     *     required=true,
138
     *     @OA\JsonContent(ref=@Model(type=ToDo::class, groups={"request"}))
139
     * )
140
     * @OA\Response(
141
     *     response=200,
142
     *     description="The updated ToDo item",
143
     *     @OA\JsonContent(ref=@Model(type=ToDo::class))
144
     * )
145
     * @OA\Response(
146
     *     response=400,
147
     *     description="Bad Request",
148
     *     @OA\JsonContent(ref="#/components/schemas/BadRequestError")
149
     * ),
150
     * @OA\Response(response="default", description="Unexpected error", @OA\JsonContent(ref="#/components/schemas/ErrorModel"))
151
     */
152
    public function update(ToDo $toDo, Request $request): JsonResponse
153
    {
154
        $this->toDoSerializer->deserializeRequestIntoExisting($request->getContent(), $toDo);
155
        $this->toDoValidator->validate($toDo);
156
157
        $this->entityManager->flush();
158
159
        return $this->json($toDo);
160
    }
161
}
162