UniciteCollections.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Service;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. /**
  5. * Faire des recherches de films
  6. */
  7. class UniciteCollections
  8. {
  9. protected $em;
  10. /**
  11. * Search constructor.
  12. * @param EntityManagerInterface $em
  13. */
  14. public function __construct(EntityManagerInterface $em)
  15. {
  16. $this->em = $em;
  17. }
  18. /**
  19. * @param \App\Entity\Film $film
  20. * @return \App\Entity\Film
  21. */
  22. public function assureUniciteCollections(\App\Entity\Film $film)
  23. {
  24. $film = $this->checkRealisateurs($film);
  25. $film = $this->checkGenres($film);
  26. return $film;
  27. }
  28. /**
  29. * @param \App\Entity\Film $film
  30. * @return \App\Entity\Film
  31. */
  32. protected function checkRealisateurs(\App\Entity\Film $film)
  33. {
  34. $realisateurs = $film->getRealisateurs();
  35. foreach ($realisateurs as $realisateur)
  36. {
  37. if ($realisateur->getId() == null)
  38. {
  39. $recherche = $this->em->getRepository('App:Realisateur')->findOneBy(array('nomComplet'=>$realisateur->getNomComplet()));
  40. if ($recherche != null)
  41. {
  42. $film->removeRealisateur($realisateur);
  43. $film->addRealisateur($recherche);
  44. }
  45. }
  46. }
  47. return $film;
  48. }
  49. /**
  50. * @param \App\Entity\Film $film
  51. * @return \App\Entity\Film
  52. */
  53. protected function checkGenres(\App\Entity\Film $film)
  54. {
  55. $genres = $film->getGenres();
  56. foreach ($genres as $genre)
  57. {
  58. if ($genre->getId() == null)
  59. {
  60. $recherche = $this->em->getRepository('App:Genre')->findOneBy(array('name'=>$genre->getName()));
  61. if ($recherche != null)
  62. {
  63. $film->removeGenre($genre);
  64. $film->addGenre($recherche);
  65. }
  66. }
  67. }
  68. return $film;
  69. }
  70. }