Browse Source

Un peu de factorisation des controlleurs

Sangfroid 9 months ago
parent
commit
57e5f44507

+ 27 - 0
src/Controller/FilmAbstractController.php

@@ -0,0 +1,27 @@
+<?php
+
+namespace App\Controller;
+
+use App\Dto\FiltersDTO;
+use App\Form\FiltersType;
+use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\Form\FormInterface;
+use Symfony\Component\HttpFoundation\Request;
+
+abstract class FilmAbstractController extends AbstractController
+{
+    public function formPagination(Request $request): FormInterface
+    {
+        $session = $request->getSession();
+
+        $filterDto = $session->get('filters', new FiltersDTO());
+        $filtersForm = $this->createForm(FiltersType::class, $filterDto);
+
+        $filtersForm->handleRequest($request);
+
+        if ($filtersForm->isSubmitted() && $filtersForm->isValid()) {
+            $session->set('filters', $filterDto);
+        }
+        return $filtersForm;
+    }
+}

+ 9 - 42
src/Controller/VideothequeController.php

@@ -2,10 +2,8 @@
 
 namespace App\Controller;
 
-use App\Dto\FiltersDTO;
 use App\Entity\Commentaire;
 use App\Entity\Realisateur;
-use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\Routing\Annotation\Route;
 use Symfony\Component\HttpFoundation\Request;
@@ -13,7 +11,6 @@ use App\Entity\Film;
 use App\Entity\Genre;
 use App\Form\CommentaireType;
 use App\Form\FilmType;
-use App\Form\FiltersType;
 use App\Repository\CommentaireRepository;
 use App\Repository\FilmRepository;
 use App\Repository\GenreRepository;
@@ -27,23 +24,13 @@ use Symfony\Component\Form\FormFactoryInterface;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Security\Http\Attribute\IsGranted;
 
-class VideothequeController extends AbstractController
+class VideothequeController extends FilmAbstractController
 {
 	#[Route("/", name: "videotheque_liste")]
 	public function listeAction(Request $request, FilmRepository $filmRepository, OptionsManager $options): Response
 	{
-        $session = $request->getSession();
-
-        $filterDto = $session->get('filters', new FiltersDTO());
-        $filtersForm = $this->createForm(FiltersType::class, $filterDto);
-
-        $filtersForm->handleRequest($request);
-
-        if ($filtersForm->isSubmitted() && $filtersForm->isValid()) {
-            $session->set('filters', $filterDto);
-        }
-        $filters = $filtersForm->getData();
-        $listeFilms = $filmRepository->findTous($filters->toArray());
+        $filtersForm = $this->formPagination($request);
+        $listeFilms = $filmRepository->findTous($filtersForm->getData()->toArray());
 
 		return $this->render('videotheque/liste_'.$options->vue().'.html.twig', array(
             'listeFilms'	=>	$listeFilms,
@@ -53,7 +40,7 @@ class VideothequeController extends AbstractController
     }
 
     #[Route("/prochaines-sorties", name: "prochaines_sorties")]
-    public function prochainesSorties(Request $request, FilmRepository $repo, OptionsManager $options): Response
+    public function prochainesSorties(FilmRepository $repo, OptionsManager $options): Response
     {
         $listeFilms = $repo->findProchaines();
 
@@ -64,20 +51,10 @@ class VideothequeController extends AbstractController
     }
 
     #[Route("/liste-by/{id}", name: "videotheque_listepargenre")]
-	public function  listeParGenreAction(Request $request, Genre $genre, FilmRepository $repo, OptionsManager $options): Response
+	public function listeParGenreAction(Request $request, Genre $genre, FilmRepository $repo, OptionsManager $options): Response
     {
-        $session = $request->getSession();
-
-        $filterDto = $session->get('filters', new FiltersDTO());
-        $filtersForm = $this->createForm(FiltersType::class, $filterDto);
-
-        $filtersForm->handleRequest($request);
-
-        if ($filtersForm->isSubmitted() && $filtersForm->isValid()) {
-            $session->set('filters', $filterDto);
-        }
-        $filters = $filtersForm->getData();
-        $films = $repo->findFilmWithGenre([$genre->getName()], $filters->toArray());
+        $filtersForm = $this->formPagination($request);
+        $films = $repo->findFilmWithGenre([$genre->getName()], $filtersForm->getData()->toArray());
 
         return $this->render('videotheque/liste_'.$options->vue().'.html.twig', array(
             'listeFilms'    => $films,
@@ -89,18 +66,8 @@ class VideothequeController extends AbstractController
     #[Route("/liste-by_real/{id}", name: "videotheque_listeparreal")]
     public function  listeParRealisateurAction(Request $request, Realisateur $realisateur, FilmRepository $repo, OptionsManager $options): Response
     {
-        $session = $request->getSession();
-
-        $filterDto = $session->get('filters', new FiltersDTO());
-        $filtersForm = $this->createForm(FiltersType::class, $filterDto);
-
-        $filtersForm->handleRequest($request);
-
-        if ($filtersForm->isSubmitted() && $filtersForm->isValid()) {
-            $session->set('filters', $filterDto);
-        }
-        $filters = $filtersForm->getData();
-        $films = $repo->findFilmWithReal([$realisateur->getNomComplet()], $filters->toArray());
+        $filtersForm = $this->formPagination($request);
+        $films = $repo->findFilmWithReal([$realisateur->getNomComplet()], $filtersForm->getData()->toArray());
 
         return $this->render('videotheque/liste_'.$options->vue().'.html.twig', array(
             'listeFilms'    => $films,

+ 3 - 17
src/Controller/VideothequePersonnelleController.php

@@ -2,36 +2,22 @@
 
 namespace App\Controller;
 
-use App\Dto\FiltersDTO;
-use App\Form\FiltersType;
 use App\Repository\FilmRepository;
 use App\Service\FilmManager;
-use App\Service\FiltersService;
 use App\Service\OptionsManager;
 use Doctrine\ORM\EntityManagerInterface;
-use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\Routing\Annotation\Route;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
 
-class VideothequePersonnelleController extends AbstractController
+class VideothequePersonnelleController extends FilmAbstractController
 {
 	#[Route("/maliste", name:"videothequepersonnelle_maliste")]
 	public function maListeAction (Request $request, OptionsManager $options, FilmRepository $filmRepository): Response
     {
-        $session = $request->getSession();
-
-        $filterDto = $session->get('filters', new FiltersDTO());
-        $filtersForm = $this->createForm(FiltersType::class, $filterDto);
-
-        $filtersForm->handleRequest($request);
-
-        if ($filtersForm->isSubmitted() && $filtersForm->isValid()) {
-            $session->set('filters', $filterDto);
-        }
-        $filters = $filtersForm->getData();
-        $listeFilms = $filmRepository->findTousFavoritesByUser($this->getUser(), $filters->toArray());
+        $filtersForm = $this->formPagination($request);
+        $listeFilms = $filmRepository->findTousFavoritesByUser($this->getUser(), $filtersForm->getData()->toArray());
         return $this->render('videotheque/liste_'.$options->vue().'.html.twig', array(
             'listeFilms'    =>  $listeFilms,
             'titre'         =>  'Ma liste de films à voir',

+ 2 - 2
src/Repository/FilmRepository.php

@@ -93,7 +93,7 @@ class FilmRepository extends ServiceEntityRepository
         ;
     }
 
-    public function findTous (array $filters = []): ?Paginator
+    public function findTous (array $filters = []): Paginator
     {
         $qb = $this->queryFilter($filters)
             ->leftJoin('f.authered', 'aut')->addSelect('aut')
@@ -103,7 +103,7 @@ class FilmRepository extends ServiceEntityRepository
         return new Paginator($qb->getQuery());
     }
 
-    public function findTousFavoritesByUser($user, array $filters = []): ?Paginator
+    public function findTousFavoritesByUser($user, array $filters = []): Paginator
     {
         $qb = $this->queryFilter($filters);
         $qb