Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
85.71% |
6 / 7 |
CRAP | |
98.46% |
64 / 65 |
| TaskController | |
0.00% |
0 / 1 |
|
85.71% |
6 / 7 |
17 | |
98.46% |
64 / 65 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
6 / 6 |
|||
| listAction | |
100.00% |
1 / 1 |
3 | |
100.00% |
11 / 11 |
|||
| listDone | |
100.00% |
1 / 1 |
3 | |
100.00% |
11 / 11 |
|||
| createAction | |
100.00% |
1 / 1 |
3 | |
100.00% |
11 / 11 |
|||
| editAction | |
100.00% |
1 / 1 |
3 | |
100.00% |
11 / 11 |
|||
| toggleTaskAction | |
0.00% |
0 / 1 |
2.01 | |
87.50% |
7 / 8 |
|||
| deleteTaskAction | |
100.00% |
1 / 1 |
2 | |
100.00% |
7 / 7 |
|||
| <?php | |
| namespace App\Controller; | |
| use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
| use Symfony\Component\Routing\Annotation\Route; | |
| use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
| use Symfony\Component\HttpFoundation\Request; | |
| use Symfony\Component\HttpFoundation\Response; | |
| use Symfony\Component\HttpFoundation\RedirectResponse; | |
| use Symfony\Component\Security\Core\Security; | |
| use Doctrine\ORM\EntityManagerInterface; | |
| // Voir Doc Symfony : https://symfony.com/doc/current/components/security/authorization.html | |
| use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | |
| use App\Entity\Tasktodo; | |
| use App\Form\TasktodoType; | |
| use App\Repository\TasktodoRepository; | |
| use App\Handler\PagingHandler; | |
| use App\Security\Voter\TaskVoter; | |
| class TaskController extends AbstractController | |
| { | |
| /** | |
| * @var Security | |
| */ | |
| private $security; | |
| /** | |
| * @var EntityManagerInterface | |
| */ | |
| private $manager; | |
| /** | |
| * @var AuthorizationCheckerInterface | |
| */ | |
| private $authorization; | |
| /** | |
| * @var TasktodoRepository | |
| */ | |
| private $tasktodoRepo; | |
| /** | |
| * @var PagingHandler | |
| */ | |
| private $pagingHandler; | |
| public function __construct( | |
| Security $security, | |
| EntityManagerInterface $manager, | |
| AuthorizationCheckerInterface $authorization, | |
| TasktodoRepository $tasktodoRepo, | |
| PagingHandler $pagingHandler | |
| ) | |
| { | |
| $this->security = $security; | |
| $this->manager = $manager; | |
| $this->authorization = $authorization; | |
| $this->tasktodoRepo = $tasktodoRepo; | |
| $this->pagingHandler = $pagingHandler; | |
| } | |
| /** | |
| * @Route("/tasks", name="task_list", methods={"GET"}) | |
| * @return Response | |
| */ | |
| public function listAction(Request $request): Response | |
| { | |
| // PAGINATION | |
| $pageValues = $this->pagingHandler->handle($request); | |
| if (!$pageValues[0] || !$pageValues[1]) { | |
| $start = 0; | |
| $limit = 10; | |
| } else { | |
| $start = (int) strip_tags($pageValues[0]); | |
| $limit = (int) strip_tags($pageValues[1]); | |
| } | |
| // $tasks = $this->tasktodoRepo->findAll(); | |
| // RÉVISION DE L'APPEL DU REPOSITORY | |
| $tasks = $this->tasktodoRepo->findBy(array('isDone' => false), array('createdAt' => 'DESC')); | |
| return $this->render('task/list.html.twig', [ | |
| 'limit' => $limit, | |
| 'start' => $start, | |
| 'tasks' => $tasks | |
| // DÉSACTIVATION DE CETTE MÉTHODE DU PROJET DE DÉPART : | |
| // 'tasks' => $this->getDoctrine()->getRepository('App:Tasktodo')->findAll() | |
| ]); | |
| } | |
| /** | |
| * @Route("/tasks/done", name="task_done", methods={"GET"}) | |
| * @return Response | |
| */ | |
| public function listDone(Request $request): Response | |
| { | |
| $pageValues = $this->pagingHandler->handle($request); | |
| if (!$pageValues[0] || !$pageValues[1]) { | |
| $start = 0; | |
| $limit = 10; | |
| } else { | |
| $start = (int) strip_tags($pageValues[0]); | |
| $limit = (int) strip_tags($pageValues[1]); | |
| } | |
| $tasks = $this->tasktodoRepo->findBy(array('isDone' => true), array('freshDate' => 'DESC')); | |
| return $this->render('task/list.html.twig', [ | |
| 'limit' => $limit, | |
| 'start' => $start, | |
| 'tasks' => $tasks | |
| // DÉSACTIVATION DE CETTE MÉTHODE DU PROJET DE DÉPART : | |
| // 'tasks' => $this->getDoctrine()->getRepository('App:Tasktodo')->findBy(['isDone' => true]) | |
| ]); | |
| } | |
| /** | |
| * @Route("/tasks/create", name="task_create", methods={"GET","POST"}) | |
| * @return Response | |
| */ | |
| public function createAction(Request $request): Response | |
| { | |
| $task = new Tasktodo(); | |
| $form = $this->createForm(TasktodoType::class, $task); | |
| $form->handleRequest($request); | |
| if ($form->isSubmitted() && $form->isValid()) { | |
| // $em = $this->getDoctrine()->getManager(); | |
| $task->setUsertodo($this->security->getUser()); | |
| // $em->persist($task); | |
| // $em->flush(); | |
| // RÉVISION DU PROJET DE DÉPART AVEC EntityManagerInterface | |
| $this->manager->persist($task); | |
| $this->manager->flush(); | |
| $this->addFlash('success', 'La tâche a été bien été ajoutée.'); | |
| return $this->redirectToRoute('task_list'); | |
| } | |
| return $this->render('task/create.html.twig', [ | |
| 'form' => $form->createView() | |
| ]); | |
| } | |
| /** | |
| * @Route("/tasks/{id}/edit", name="task_edit", requirements={"id": "\d+"}, methods={"GET","POST"}) | |
| * @return Response | |
| */ | |
| public function editAction(Tasktodo $task, Request $request): Response | |
| { | |
| $form = $this->createForm(TasktodoType::class, $task); | |
| $form->handleRequest($request); | |
| if ($form->isSubmitted() && $form->isValid()) { | |
| // $this->getDoctrine()->getManager()->flush(); | |
| // $em = $this->getDoctrine()->getManager(); | |
| // AJOUTÉ POUR LA DATE DE MISE À JOUR | |
| $task->setFreshDate(new \Datetime()); | |
| // $em->persist($task); | |
| // $em->flush(); | |
| // RÉVISION DU PROJET DE DÉPART AVEC EntityManagerInterface | |
| $this->manager->persist($task); | |
| $this->manager->flush(); | |
| $this->addFlash('success', 'La tâche a bien été modifiée.'); | |
| return $this->redirectToRoute('task_list'); | |
| } | |
| return $this->render('task/edit.html.twig', [ | |
| 'form' => $form->createView(), | |
| 'task' => $task, | |
| ]); | |
| } | |
| /** | |
| * @Route("/tasks/{id}/toggle", name="task_toggle", requirements={"id": "\d+"}, methods={"GET", "POST"}) | |
| * @return Response | |
| */ | |
| public function toggleTaskAction(Tasktodo $task): Response | |
| { | |
| $task->toggle(!$task->isDone()); | |
| // MISE À JOUR CORRÉLATIVE | |
| $task->setFreshDate(new \Datetime()); | |
| // $this->getDoctrine()->getManager()->flush(); | |
| // RÉVISION DU PROJET DE DÉPART AVEC EntityManagerInterface | |
| $this->manager->persist($task); | |
| $this->manager->flush(); | |
| // AJOUTÉ POUR GÉRER LES MESSAGES TÂCHE TERMINÉE OU RÉOUVERTE | |
| if ($task->isDone() === true) { | |
| $this->addFlash('success', sprintf('La tâche " %s " a bien été marquée comme faite.', $task->getTitle())); | |
| } else { | |
| $this->addFlash('success', sprintf('La tâche " %s " est réouverte.', $task->getTitle())); | |
| } | |
| return $this->redirectToRoute('task_list'); | |
| } | |
| /** | |
| * @Route("/tasks/{id}/delete", name="task_delete", requirements={"id": "\d+"}, methods={"GET", "DELETE"}) | |
| * @return RedirectResponse | |
| */ | |
| public function deleteTaskAction(Tasktodo $task): RedirectResponse | |
| { | |
| // AJOUTÉ POUR EMPÊCHER QU'UN UTILISATEUR SIMPLE NE SUPPRIME UNE TÂCHE DONT IL N'EST PAS L'AUTEUR | |
| // On vérifie : si l'utilisateur connecté est différent de l'auteur de la tâche | |
| /* if ($this->security->getUser() !== $task->getUsertodo()) | |
| { | |
| // Et si l'utilisateur connecté n'a pas le rôle ADMIN | |
| if(!$this->isGranted('ROLE_ADMIN')) | |
| { | |
| // Alors Erreur ! | |
| $this->addFlash('error', 'Erreur. Opération réservée aux auteurs des tâches ou aux administrateurs'); | |
| return $this->redirectToRoute('task_list'); | |
| } | |
| } */ | |
| // $this->denyAccessUnlessGranted('delete', $task); | |
| if (!$this->authorization->isGranted(TaskVoter::DELETE, $task)) { | |
| $this->addFlash('error', 'Erreur. Opération réservée aux auteurs des tâches ou aux administrateurs'); | |
| return $this->redirectToRoute('task_list'); | |
| } | |
| // $em = $this->getDoctrine()->getManager(); | |
| // $em->remove($task); | |
| // $em->flush(); | |
| // RÉVISION DU PROJET DE DÉPART AVEC EntityManagerInterface | |
| $this->manager->remove($task); | |
| $this->manager->flush(); | |
| $this->addFlash('success', 'La tâche a bien été supprimée.'); | |
| return $this->redirectToRoute('task_list'); | |
| } | |
| } |