Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
94 / 94
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
TaskController
100.00% covered (success)
100.00%
94 / 94
100.00% covered (success)
100.00%
8 / 8
31
100.00% covered (success)
100.00%
1 / 1
 listAction
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 createAction
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
5
 editTaskAction
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
6
 toggleTaskAction
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 deleteTaskAction
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 doneTasksAction
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 userTasksAction
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 anonymousTasksAction
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace App\Controller;
4
5use App\Entity\Task;
6use App\Entity\User;
7use App\Security\UserAccessVoter;
8use App\Form\TaskType;
9use Doctrine\ORM\EntityManagerInterface;
10use Symfony\Component\HttpFoundation\Request;
11use Symfony\Component\HttpFoundation\Response;
12use Symfony\Component\Routing\Annotation\Route;
13use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
14
15class TaskController extends AbstractController
16{
17    /**
18     * Retrieves and displays a list of tasks.
19     *
20     * @param EntityManagerInterface $emi The entity manager interface.
21     * @return Response The response object.
22     */
23    #[Route('/tasks', name: 'app_tasks_list', methods: ['GET'])]
24    public function listAction(EntityManagerInterface $emi): Response
25    {
26        if (!$this->getUser()) {
27            $this->addFlash('danger', 'Vous devez être connecté pour créer une tâche.');
28
29            return $this->redirectToRoute('app_login');
30        }
31
32        return $this->render('task/list.html.twig', [
33            'controller_name' => 'TaskController',
34            'title' => 'Liste des tâches',
35            'tasks' => $emi->getRepository(Task::class)->findAll(),
36        ]);
37    }
38
39
40
41    /**
42     * Creates a new task.
43     *
44     * This method is responsible for creating a new task. It checks if the user is authenticated,
45     * and if not, it redirects them to the login page.
46     * It creates a new instance of the Task entity and a form to handle the task creation.
47     * If the form is submitted and valid, the task is persisted to the database and a
48     * success flash message is displayed. Finally, the user is redirected to the task list page.
49     *
50     * @param EntityManagerInterface $emi The entity manager interface.
51     * @param Request $request The request object.
52     * @return Response The response object.
53     */
54    #[Route('/tasks/create', name: 'app_task_create', methods: ['GET', 'POST'])]
55    public function createAction(EntityManagerInterface $emi, Request $request): Response
56    {
57        if (!$this->getUser()) {
58            $this->addFlash('danger', 'Vous devez être connecté pour créer une tâche.');
59
60            return $this->redirectToRoute('app_login');
61        }
62
63        $task = new Task();
64
65        $form = $this->createForm(TaskType::class, $task, [
66            'action' => $this->generateUrl('app_task_create')
67        ]);
68
69        $form->handleRequest($request);
70
71        if ($form->isSubmitted() && $form->isValid()) {
72            $task = $form->getData();
73
74            $task->getAuthor() === null ? $task->setAuthor($this->getUser()) : null;
75
76            $emi->persist($task);
77            $emi->flush();
78
79            $this->addFlash('success', 'La tâche a bien été ajoutée.');
80
81            return $this->redirectToRoute('app_tasks_list');
82        }
83
84        return $this->render('task/create.html.twig', [
85            'controller_name' => 'TaskController',
86            'form' => $form->createView(),
87        ]);
88    }
89
90
91
92    /**
93     * Edit a task.
94     *
95     * This method allows the user to edit a task. If the user is not logged in,
96     * they will be redirected to the login page.
97     * If the user is not the author of the task and does not have the ROLE_ADMIN role,
98     * they will be redirected to the task list page.
99     * If the form is submitted and valid, the task will be updated and the user will be
100     * redirected to the task list page.
101     *
102     * @param Task $task The task to be edited.
103     * @param EntityManagerInterface $emi The entity manager interface.
104     * @param Request $request The request object.
105     * @return Response The response object.
106     */
107    #[Route('/tasks/{id}/edit', name: 'app_task_edit', methods: ['GET', 'POST'])]
108    public function editTaskAction(Task $task, EntityManagerInterface $emi, Request $request): Response
109    {
110        if (!$this->getUser()) {
111            $this->addFlash('danger', 'Vous devez être connecté pour modifier une tâche.');
112
113            return $this->redirectToRoute('app_login');
114        } elseif ($this->getUser() !== $task->getAuthor() && !$this->isGranted('ROLE_ADMIN')) {
115            $this->addFlash('danger', 'Vous ne pouvez pas modifier une tâche qui ne vous appartient pas.');
116
117            return $this->redirectToRoute('app_tasks_list');
118        }
119
120        $form = $this->createForm(TaskType::class, $task);
121
122        $form->handleRequest($request);
123
124        if ($form->isSubmitted() && $form->isValid()) {
125            // $task->setAuthor($emi->getRepository(User::class)->findOneBy(
126            //      ['username' => $form->get('author')->getData()]));
127            $emi->flush();
128
129            $this->addFlash('success', 'La tâche a bien été modifiée.');
130
131            return $this->redirectToRoute('app_tasks_list');
132        }
133
134        return $this->render('task/edit.html.twig', [
135            'controller_name' => 'TaskController',
136            'form' => $form->createView(),
137            'task' => $task
138        ]);
139    }
140
141
142
143    /**
144     * Toggle the status of a task.
145     *
146     * This method toggles the status of a task between "done" and "not done".
147     * If the user is not logged in, they will be redirected to the login page.
148     * After toggling the status, a success flash message is added and the user is redirected to the task list page.
149     *
150     * @param Task $task The task to toggle.
151     * @param EntityManagerInterface $emi The entity manager interface.
152     * @return Response The response object.
153     */
154    #[Route('/tasks/{id}/toggle', name: 'app_task_toggle', methods: ['GET', 'POST'])]
155    public function toggleTaskAction(Task $task, EntityManagerInterface $emi): Response
156    {
157        if (!$this->getUser()) {
158            $this->addFlash('danger', 'Vous devez être connecté pour clôturer une tâche.');
159
160            return $this->redirectToRoute('app_login');
161        }
162
163
164        if ($this->getUser() !== $task->getAuthor() && !$this->isGranted('ROLE_ADMIN')) {
165            $this->addFlash('danger', 'Vous ne pouvez pas clôturer une tâche qui ne vous appartient pas.');
166
167            return $this->redirectToRoute('app_tasks_list');
168        }
169
170        $task->setIsDone(!$task->isIsDone());
171        $emi->flush();
172
173        $this->addFlash('success', sprintf('La tâche %s a bien été marquée comme terminée.', $task->getTitle()));
174
175        return $this->redirectToRoute('app_tasks_list');
176    }
177
178
179
180    /**
181     * Deletes a task.
182     *
183     * This method is responsible for deleting a task from the database.
184     * It checks if the user is authenticated and has the necessary permissions to delete the task.
185     * If the user is not authenticated, they are redirected to the login page with a flash message
186     * indicating that they need to be logged in to delete a task.
187     * If the user is not the author of the task and does not have the ROLE_ADMIN role,
188     * they are redirected to the task list page with a flash message indicating that they
189     * cannot delete a task that does not belong to them.
190     * If the user is authenticated and has the necessary permissions,
191     * the task is removed from the database and a success flash message is displayed.
192     * Finally, the user is redirected to the task list page.
193     *
194     * @param Task $task The task to be deleted.
195     * @param EntityManagerInterface $emi The entity manager interface used to interact with the database.
196     * @return Response The response object.
197     */
198    #[Route('/tasks/{id}/delete', name: 'app_task_delete', methods: ['GET', 'DELETE'])]
199    public function deleteTaskAction(Task $task, EntityManagerInterface $emi): Response
200    {
201        if (!$this->getUser()) {
202            $this->addFlash('danger', 'Vous devez être connecté pour supprimer une tâche.');
203
204            return $this->redirectToRoute('app_login');
205        } elseif ($this->getUser() !== $task->getAuthor() && !$this->isGranted('ROLE_ADMIN')) {
206            $this->addFlash('danger', 'Vous ne pouvez pas supprimer une tâche qui ne vous appartient pas.');
207
208            return $this->redirectToRoute('app_tasks_list');
209        }
210
211        $emi->remove($task);
212        $emi->flush();
213
214        $this->addFlash('success', 'La tâche a bien été supprimée.');
215
216        return $this->redirectToRoute('app_tasks_list');
217    }
218
219
220
221    /**
222     * Retrieves and displays the list of completed tasks.
223     *
224     * @param EntityManagerInterface $emi The entity manager interface.
225     * @return Response The response object.
226     *
227     */
228    #[Route('/tasks/done', name: 'app_tasks_done', methods: ['GET'])]
229    public function doneTasksAction(EntityManagerInterface $emi): Response
230    {
231        if (!$this->getUser()) {
232            $this->addFlash('danger', 'Vous devez être connecté accéder aux tâches clôturées.');
233
234            return $this->redirectToRoute('app_login');
235        }
236
237        return $this->render('task/list.html.twig', [
238            'controller_name' => 'TaskController',
239            'tasks' => $emi->getRepository(Task::class)->findBy(['isDone' => true]),
240            'title' => 'Tâches clôturées'
241        ]);
242    }
243
244
245
246    /**
247     * Retrieves and displays the tasks of a specific user.
248     *
249     * @param User $user The user whose tasks are to be displayed.
250     * @param EntityManagerInterface $emi The entity manager interface.
251     * @return Response The response object.
252     */
253    #[Route('/tasks/user/{id}', name: 'app_tasks_user', methods: ['GET'])]
254    public function userTasksAction(User $user, EntityManagerInterface $emi): Response
255    {
256        if (!$this->getUser()) {
257            $this->addFlash('danger', 'Vous devez être connecté pour accéder votre liste des tâches.');
258
259            return $this->redirectToRoute('app_login');
260        } elseif ($this->getUser() !== $user && !$this->isGranted('ROLE_ADMIN')) {
261            $this->addFlash('danger', 'Vous ne pouvez pas accéder à la liste des tâches d\'un autre utilisateur.');
262
263            return $this->redirectToRoute('app_tasks_list');
264        }
265
266        return $this->render('task/list.html.twig', [
267            'controller_name' => 'TaskController',
268            'title' => 'Tâches de ' . $user->getUsername(),
269            'tasks' => $emi->getRepository(Task::class)->findBy(['author' => $user]),
270        ]);
271    }
272
273    /**
274     * We create an action to affect all tasks without author to the anonymous user.
275     *
276     */
277    #[Route('/tasks/anonymous', name: 'app_tasks_anonymous', methods: ['GET'])]
278    public function anonymousTasksAction(EntityManagerInterface $emi): Response
279    {
280        if (!$this->getUser()) {
281            $this->addFlash('danger', 'Vous devez être connecté pour effectuer cette opération.');
282
283            return $this->redirectToRoute('app_login');
284        } elseif (!$this->isGranted('ROLE_ADMIN')) {
285            $this->addFlash('danger', 'Vous ne possédez pas les droits nécessaires pour cette action.');
286
287            return $this->redirectToRoute('app_tasks_list');
288        }
289
290        $tasks = $emi->getRepository(Task::class)->findBy(['author' => null]);
291
292        foreach ($tasks as $task) {
293            $task->setAuthor($emi->getRepository(User::class)->findOneBy(['username' => 'anonymous']));
294        }
295
296        $emi->flush();
297
298        return $this->redirectToRoute('app_admin');
299    }
300}