Эх сурвалжийг харах

Passage de la vérification d'unicité des collection en service

François 6 жил өмнө
parent
commit
f31b97d24e

+ 6 - 0
app/config/services.yml

@@ -42,4 +42,10 @@ services:
         class: AppBundle\Service\Search
         public: true
         arguments:
+        - "@doctrine.orm.entity_manager"
+
+    film.unicite_collections:
+        class: AppBundle\Service\UniciteCollections
+        public: true
+        arguments:
         - "@doctrine.orm.entity_manager"

+ 4 - 53
src/AppBundle/Controller/VideothequeController.php

@@ -78,33 +78,8 @@ class VideothequeController extends Controller
 		$form->handleRequest($request);
 		if ($form->isSubmitted() && $form->isValid())
 		{
-            $realisateurs = $film->getRealisateurs();
-            foreach ($realisateurs as $realisateur)
-            {
-                if ($realisateur->getId() == null)
-                {
-                    $recherche = $em->getRepository('AppBundle:Realisateur')->findOneBy(array('nomComplet'=>$realisateur->getNomComplet()));
-                    if ($recherche != null)
-                    {
-                        $film->removeRealisateur($realisateur);
-                        $film->addRealisateur($recherche);
-                    }
-                }
-            }
-            $genres = $film->getGenres();
-            foreach ($genres as $genre)
-            {
-                if ($genre->getId() == null)
-                {
-                    $recherche = $em->getRepository('AppBundle:Genre')->findOneBy(array('name'=>$genre->getName()));
-                    if ($recherche != null)
-                    {
-                        $film->removeGenre($genre);
-                        $film->addGenre($recherche);
-                    }
-                }
-            }
-
+            $uniciteCollections = $this->get('film.unicite_collections');
+            $film = $uniciteCollections->assureUniciteCollections($film);
 			$em->persist($film);
 			$em->flush();
 			$this->addFlash('success', 'Le film a été ajouté');
@@ -126,32 +101,8 @@ class VideothequeController extends Controller
         $em = $this->getDoctrine()->getManager();
 		if ($form->isSubmitted() && $form->isValid())
 		{
-            $realisateurs = $film->getRealisateurs();
-            foreach ($realisateurs as $realisateur)
-            {
-                if ($realisateur->getId() == null)
-                {
-                    $recherche = $em->getRepository('AppBundle:Realisateur')->findOneBy(array('nomComplet'=>$realisateur->getNomComplet()));
-                    if ($recherche != null)
-                    {
-                        $film->removeRealisateur($realisateur);
-                        $film->addRealisateur($recherche);
-                    }
-                }
-            }
-            $genres = $film->getGenres();
-            foreach ($genres as $genre)
-            {
-                if ($genre->getId() == null)
-                {
-                    $recherche = $em->getRepository('AppBundle:Genre')->findOneBy(array('name'=>$genre->getName()));
-                    if ($recherche != null)
-                    {
-                        $film->removeGenre($genre);
-                        $film->addGenre($recherche);
-                    }
-                }
-            }
+            $uniciteCollections = $this->get('film.unicite_collections');
+            $film = $uniciteCollections->assureUniciteCollections($film);
 			$em->flush();
 			$this->addFlash('success', 'Le film a été modifié');
 			return $this->redirectToRoute('videotheque_voirfilm',array('id'=>$film->getId()));

+ 79 - 0
src/AppBundle/Service/UniciteCollections.php

@@ -0,0 +1,79 @@
+<?php
+
+namespace AppBundle\Service;
+
+use Doctrine\ORM\EntityManagerInterface;
+
+/**
+ * Faire des recherches de films
+ */
+class UniciteCollections
+{
+    protected $em;
+
+    /**
+     * Search constructor.
+     * @param EntityManagerInterface $em
+     */
+    public function __construct(EntityManagerInterface $em)
+    {
+        $this->em = $em;
+    }
+
+    /**
+     * @param \AppBundle\Entity\Film $film
+     * @return \AppBundle\Entity\Film
+     */
+    public function assureUniciteCollections(\AppBundle\Entity\Film $film)
+    {
+        $film = $this->checkRealisateurs($film);
+        $film = $this->checkGenres($film);
+        return $film;
+    }
+
+    /**
+     * @param \AppBundle\Entity\Film $film
+     * @return \AppBundle\Entity\Film
+     */
+    protected function checkRealisateurs(\AppBundle\Entity\Film $film)
+    {
+        $realisateurs = $film->getRealisateurs();
+        foreach ($realisateurs as $realisateur)
+        {
+            if ($realisateur->getId() == null)
+            {
+                $recherche = $this->em->getRepository('AppBundle:Realisateur')->findOneBy(array('nomComplet'=>$realisateur->getNomComplet()));
+                if ($recherche != null)
+                {
+                    $film->removeRealisateur($realisateur);
+                    $film->addRealisateur($recherche);
+                }
+            }
+        }
+
+        return $film;
+    }
+
+    /**
+     * @param \AppBundle\Entity\Film $film
+     * @return \AppBundle\Entity\Film
+     */
+    protected function checkGenres(\AppBundle\Entity\Film $film)
+    {
+        $genres = $film->getGenres();
+        foreach ($genres as $genre)
+        {
+            if ($genre->getId() == null)
+            {
+                $recherche = $this->em->getRepository('AppBundle:Genre')->findOneBy(array('name'=>$genre->getName()));
+                if ($recherche != null)
+                {
+                    $film->removeGenre($genre);
+                    $film->addGenre($recherche);
+                }
+            }
+        }
+
+        return $film;
+    }
+}