Browse Source

genreManager

François Drouhard 5 years ago
parent
commit
0ca06ad4ee
2 changed files with 48 additions and 14 deletions
  1. 7 14
      src/Controller/GenreController.php
  2. 41 0
      src/Service/GenreManager.php

+ 7 - 14
src/Controller/GenreController.php

@@ -4,8 +4,8 @@ namespace App\Controller;
 
 use App\Entity\Genre;
 use App\Form\GenreType;
+use App\Service\GenreManager;
 use Doctrine\ORM\EntityManagerInterface;
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\Routing\Annotation\Route;
 use Symfony\Component\HttpFoundation\Request;
@@ -28,7 +28,7 @@ class GenreController extends AbstractController
 	/**
 	 * @Route("/genre/ajouter", name="genre_ajouter")
 	 */
-	public function ajouterAction(Request $request, EntityManagerInterface $em)
+	public function ajouterAction(Request $request, GenreManager $genreManager)
 	{
 		$genre = new Genre();
 		$form = $this->createForm(GenreType::class, $genre);
@@ -36,8 +36,7 @@ class GenreController extends AbstractController
 		$form->handleRequest($request);
 		if ($form->isSubmitted() && $form->isValid())
 		{
-			$em->persist($genre);
-			$em->flush();
+			$genreManager->add($genre);
 			$this->addFlash('success', 'Le genre a été ajouté');
 			return $this->redirectToRoute('genre_liste');
 		}
@@ -50,7 +49,7 @@ class GenreController extends AbstractController
 	/**
 	 * @Route("/genre/modifier/{id}", name="genre_modifier")
 	 */
-	public function modifierAction(Request $request, Genre $genre, EntityManagerInterface $em)
+	public function modifierAction(Request $request, Genre $genre, EntityManagerInterface $em, GenreManager $genreManager)
 	{
 		$form = $this->createForm(GenreType::class, $genre);
 		$form->handleRequest($request);
@@ -58,7 +57,7 @@ class GenreController extends AbstractController
 
 		if ($form->isSubmitted() && $form->isValid())
 		{
-			$em->flush();
+			$genreManager->edit($genre);
 			$this->addFlash('success', 'Le genre a été modifié');
 			return $this->redirectToRoute('genre_liste');
 		}
@@ -73,19 +72,13 @@ class GenreController extends AbstractController
 	/**
 	 * @Route("/genre/supprimer/{id}", name="genre_supprimer")
 	 */
-	public function supprimerAction(Request $request, Genre $genre, EntityManagerInterface $em)
+	public function supprimerAction(Request $request, Genre $genre, GenreManager $genreManager)
     {
         $form = $this->get('form.factory')->create();
         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid())
         {
-            $films = $em->getRepository('App:Film')->findFilmWithGenre(array($genre->getName()));
-            foreach ($films as $film)
-            {
-                $film->removeGenre($genre);
-            }
-            $em->remove($genre);
-            $em->flush();
+            $genreManager->del($genre);
             $this->addFlash('success', 'Le genre '.$genre->getName().' a bien été supprimé.');
             return $this->redirectToRoute('genre_liste');
         }

+ 41 - 0
src/Service/GenreManager.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Service;
+
+use App\Entity\Genre;
+use App\Entity\Film;
+use Doctrine\ORM\EntityManagerInterface;
+
+/**
+ * Class GenreManager
+ */
+class GenreManager {
+    private $em;
+
+    public function __construct(EntityManagerInterface $em)
+    {
+        $this->em = $em;
+    }
+
+    public function add(Genre $genre)
+    {
+        $this->em->persist($genre);
+        $this->em->flush();
+    }
+
+    public function edit(Genre $genre)
+    {
+        $this->em->flush();
+    }
+
+    public function del(Genre $genre)
+    {
+        $films = $this->em->getRepository(Film::class)->findFilmWithGenre(array($genre->getName()));
+        foreach ($films as $film)
+        {
+            $film->removeGenre($genre);
+        }
+        $this->em->remove($genre);
+        $this->em->flush();
+    }
+}