François Drouhard 5 жил өмнө
parent
commit
4d51c77388

+ 4 - 11
src/Controller/VideothequeController.php

@@ -84,14 +84,13 @@ class VideothequeController extends AbstractController
 	/**
 	 * @Route("/modifier/{id}", name="videotheque_modifier")
 	 */
-	public function modifierAction(Request $request, Film $film, UniciteCollections $uniciteCollections, EntityManagerInterface $em)
+	public function modifierAction(Request $request, Film $film, FilmManager $filmManager)
 	{
 		$form = $this->createForm(FilmType::class, $film);
 		$form->handleRequest($request);
 		if ($form->isSubmitted() && $form->isValid())
 		{
-            $film = $uniciteCollections->assureUniciteCollections($film);
-			$em->flush();
+            $filmManager->editFilm($film);
 			$this->addFlash('success', 'Le film a été modifié');
 			return $this->redirectToRoute('videotheque_voirfilm',array('id'=>$film->getId()));
 		}
@@ -105,19 +104,13 @@ class VideothequeController extends AbstractController
 	 * @Route("/supprimer/{id}", name="videotheque_supprimer")
      * @IsGranted("ROLE_ADMIN")
 	 */
-	public function supprimerAction(Request $request, Film $film, EntityManagerInterface $em)
+	public function supprimerAction(Request $request, Film $film, FilmManager $filmManager)
 	{
         $form = $this->get('form.factory')->create();
         $form->handleRequest($request);
         if ($form->isSubmitted() && $form->isValid())
         {
-            $commentaire = $em->getRepository('App:Commentaire')->findOneBy(array('film'=>$film));
-            if ($commentaire != null)
-            {
-                $em->remove($commentaire);
-            }
-            $em->remove($film);
-            $em->flush();
+            $filmManager->delFilm($film);
             $this->addFlash('success', 'Le film '.$film->getTitre().' a bien été supprimé.');
             return $this->redirectToRoute('videotheque_liste');
         }

+ 1 - 1
src/Entity/Commentaire.php

@@ -37,7 +37,7 @@ class Commentaire
     private $note;
 
     /**
-     * @ORM\ManyToOne(targetEntity="App\Entity\Film")
+     * @ORM\ManyToOne(targetEntity="App\Entity\Film", inversedBy="commentaires")
      * @ORM\JoinColumn(nullable=false)
      */
     private $film;

+ 45 - 0
src/Entity/Film.php

@@ -104,6 +104,12 @@ class Film
      */
     private $realisateurs;
 
+    /**
+     * @ORM\OneToMany(targetEntity="App\Entity\Commentaire", mappedBy="film", orphanRemoval=true)
+     * @var \Doctrine\Common\Collections\Collection
+     */
+    private $commentaires;
+
     /**
      * @ORM\ManyToOne(targetEntity="App\Entity\User")
      * @ORM\JoinColumn(nullable=true)
@@ -245,9 +251,48 @@ class Film
         $this->usersWantToView = new \Doctrine\Common\Collections\ArrayCollection();
         $this->usersWhoSeen = new \Doctrine\Common\Collections\ArrayCollection();
         $this->genres = new \Doctrine\Common\Collections\ArrayCollection();
+        $this->commentaires = new \Doctrine\Common\Collections\ArrayCollection();
         $this->setDateSubmited(new \DateTime());
     }
 
+    /**
+     * Add Commentaire
+     * 
+     * @param \App\Entity\Commentaire $commentaire
+     * 
+     * @return Film
+     */
+    public function addCommentaire(\App\Entity\Commentaire $commentaire)
+    {
+        $this->commentaires[] = $commentaire;
+
+        return $this;
+    }
+
+    /**
+     * Remove commentaire
+     * 
+     * @param \App\Entity\Commentaire $commentaire
+     * 
+     * @return Film
+     */
+    public function removeCommentaire(\App\Entity\Commentaire $commentaire)
+    {
+        $this->commentaires->removeElement($commentaire);
+
+        return $this;
+    }
+
+    /**
+     * Get Commentaires
+     *
+     * @return \Doctrine\Common\Collections\Collection
+     */
+    public function getCommentaires()
+    {
+        return $this->commentaires;
+    }
+
     /**
      * Add realisateur
      *

+ 48 - 0
src/Service/FilmManager.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace App\Service;
+
+use App\Entity\Film;
+use Doctrine\ORM\EntityManagerInterface;
+use Symfony\Component\Security\Core\Security;
+
+/**
+ * Class FilmManager
+ */
+class FilmManager {
+    private $em;
+    private $uc;
+    private $user;
+
+    public function __construct(EntityManagerInterface $em, UniciteCollections $uc, Security $security)
+    {
+        $this->em = $em;
+        $this->uc = $uc;
+        $this->user = $security->getUser();
+    }
+
+    public function addFilm (Film $film)
+    {
+        $film->setAuthered($this->user);
+        $film = $this->uc->assureUniciteCollections($film);
+        $this->em->persist($film);
+        $this->em->flush();
+    }
+
+    public function editFilm(Film $film)
+    {
+        $film = $this->uc->assureUniciteCollections($film);
+        $this->em->flush();
+    }
+
+    public function delFilm(Film $film)
+    {
+        /*$commentaire = $this->em->getRepository('App:Commentaire')->findOneBy(array('film'=>$film));
+        if ($commentaire != null)
+        {
+            $this->em->remove($commentaire);
+        }*/
+        $this->em->remove($film);
+        $this->em->flush();
+    }
+}