TmdbApiService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Film;
  4. use App\Entity\Genre;
  5. use App\Entity\MediaVideo;
  6. use App\Entity\Realisateur;
  7. use Symfony\Contracts\HttpClient\HttpClientInterface;
  8. class TmdbApiService
  9. {
  10. protected string $content = '';
  11. protected int $nbResults = 0;
  12. protected int $nbPages = 0;
  13. protected array $films = [];
  14. public function __construct(
  15. protected HttpClientInterface $tmdbClient,
  16. protected UniciteCollections $uc
  17. )
  18. {
  19. }
  20. public function query(string $titre, int $page = 1): bool
  21. {
  22. try {
  23. $response = $this->tmdbClient->request(
  24. 'GET',
  25. 'search/movie',[
  26. 'query' => [
  27. 'query' => $titre,
  28. 'page' => $page
  29. ]
  30. ]
  31. );
  32. $this->content = $response->getContent();
  33. $this->jsonDecode();
  34. } catch (\Exception $e) {
  35. return false;
  36. }
  37. return true;
  38. }
  39. public function getDetailMovie(string $id): ?string
  40. {
  41. $detail = null;
  42. try {
  43. $response = $this->tmdbClient->request(
  44. 'GET',
  45. 'movie/'. $id ,[
  46. 'query' => [
  47. 'append_to_response' => 'credits,videos',
  48. ]
  49. ]
  50. );
  51. $detail = $response->getContent();
  52. } catch (\Exception $e) {
  53. return null;
  54. }
  55. return $detail;
  56. }
  57. public function getFilms(): array
  58. {
  59. return $this->films;
  60. }
  61. public function countResults(): int
  62. {
  63. return $this->nbResults;
  64. }
  65. public function countPages(): int
  66. {
  67. return $this->nbPages;
  68. }
  69. protected function jsonDecode(): void
  70. {
  71. $json = json_decode($this->content);
  72. $this->nbResults = $json->total_results;
  73. $this->nbPages = $json->total_pages;
  74. $entrees = $json->results;
  75. foreach($entrees as $entree) {
  76. $film = $this->hydrateFilm($entree->id);
  77. $this->films[$entree->id] = $film;
  78. }
  79. }
  80. public function hydrateFilm($filmTmdbId): Film
  81. {
  82. $filmTmdb = json_decode($this->getDetailMovie($filmTmdbId));
  83. $film = new Film();
  84. $film
  85. ->setTitre($filmTmdb->title)
  86. ->setInformation($filmTmdb->overview);
  87. if (preg_match ("/^\d{4}-\d{2}-\d{2}$/", $filmTmdb->release_date))
  88. {
  89. $dateSortie = $filmTmdb->release_date;
  90. $annee = substr($dateSortie, 0, 4) . '-01-01';
  91. $film
  92. ->setDateSortie(new \DateTimeImmutable($dateSortie))
  93. ->setAnnee(new \DateTimeImmutable($annee))
  94. ;
  95. } elseif (preg_match("/^\d{4}/", $filmTmdb->release_date)) {
  96. $annee = substr($filmTmdb->release_date, 0, 4) . '-01-01';
  97. $film
  98. ->setAnnee(new \DateTimeImmutable($annee));
  99. ;
  100. }
  101. ;
  102. foreach ($filmTmdb->genres as $genre) {
  103. $film->addGenre((new Genre())->setName($genre->name));
  104. }
  105. foreach ($this->getRealisateurs($filmTmdb) as $realisateur) {
  106. $film->addRealisateur($realisateur);
  107. }
  108. $videoTmdb = $this->getOneVideo($filmTmdb);
  109. if (!empty($videoTmdb)) {
  110. $video = new MediaVideo();
  111. $video->setFromTmdb($videoTmdb['type'], $videoTmdb['identif']);
  112. $film->setMediaVideo($video);
  113. }
  114. $film = $this->uc->assureUniciteCollections($film);
  115. return $film;
  116. }
  117. public function getRealisateurs($detailMovie): array
  118. {
  119. $directors = array();
  120. foreach($detailMovie->credits->crew as $crew) {
  121. if ($crew->job === 'Director') {
  122. $realisateur = new Realisateur();
  123. $realisateur->setNomComplet($crew->name);
  124. $directors[] = $realisateur;
  125. }
  126. }
  127. return $directors;
  128. }
  129. public function getOneVideo($detailMovie): array
  130. {
  131. foreach($detailMovie->videos->results as $video) {
  132. if (
  133. ($video->site === "YouTube" || $video->site === "Vimeo" || $video->site === "DailyMotion") &&
  134. $video->type === "Trailer" &&
  135. $video->iso_639_1 === "fr" && $video->iso_3166_1 === "FR"
  136. ) {
  137. return [
  138. 'type' => strtolower($video->site),
  139. 'identif' => $video->key
  140. ];
  141. }
  142. }
  143. return [];
  144. }
  145. }