UniciteCollections.php 2.0 KB

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