TmdbApiService.php 3.5 KB

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