From dcc47fcb653973d475c52cdf11d4aeb5dbf606c9 Mon Sep 17 00:00:00 2001 From: thibaud-leclere Date: Thu, 15 Jan 2026 14:01:45 +0100 Subject: [PATCH] Starting actors populate --- src/Context/TMDB/MovieCreditsContext.php | 13 ++++++ src/Controller/HomepageController.php | 26 ++++++------ src/Exception/GatewayException.php | 15 +++++++ src/Gateway/TMDBGateway.php | 52 +++++++++++++++++++----- src/Model/TMDB/TMDBActor.php | 13 ++++++ 5 files changed, 95 insertions(+), 24 deletions(-) create mode 100644 src/Context/TMDB/MovieCreditsContext.php create mode 100644 src/Exception/GatewayException.php create mode 100644 src/Model/TMDB/TMDBActor.php diff --git a/src/Context/TMDB/MovieCreditsContext.php b/src/Context/TMDB/MovieCreditsContext.php new file mode 100644 index 0000000..38b3c8d --- /dev/null +++ b/src/Context/TMDB/MovieCreditsContext.php @@ -0,0 +1,13 @@ + $this->cast; }, + ) {} +} diff --git a/src/Controller/HomepageController.php b/src/Controller/HomepageController.php index eef8176..8291137 100644 --- a/src/Controller/HomepageController.php +++ b/src/Controller/HomepageController.php @@ -5,7 +5,7 @@ declare(strict_types=1); namespace App\Controller; use App\Gateway\TMDBGateway; -use App\Model\Ltbxd\LtbxdMovie; +use App\Repository\MovieRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; @@ -14,23 +14,23 @@ use Symfony\Component\Serializer\SerializerInterface; class HomepageController extends AbstractController { public function __construct( - private readonly TMDBGateway $TMDBGateway, private readonly SerializerInterface $serializer, + private readonly MovieRepository $movieRepository, + private readonly TMDBGateway $TMDBGateway, ) {} #[Route('/')] public function index(SerializerInterface $serializer): Response { -// $file = file_get_contents('files/watched.csv'); -// $ltbxdMovies = $this->serializer->deserialize($file, LtbxdMovie::class.'[]', 'csv'); -// /** @var LtbxdMovie $ltbxdMovie */ -// $films = []; -// foreach ($ltbxdMovies as $ltbxdMovie) { -// // Search movie on TMDB -// $film = $this->TMDBGateway->searchMovie($ltbxdMovie->getName()); -// if ($film) { -// $films[] = $film; -// } -// } + $movie = $this->movieRepository->findOneBy([]); + $creditsContext = $this->TMDBGateway->getMovieCredits($movie->getTmdbId()); + $cast = $creditsContext->cast; + $actors = []; + foreach ($cast as $actor) { + if (2 <= $actor->popularity) { + $actors[] = $actor; + } + } + dd($actors); return $this->render('homepage/index.html.twig'); } diff --git a/src/Exception/GatewayException.php b/src/Exception/GatewayException.php new file mode 100644 index 0000000..fe4c2a9 --- /dev/null +++ b/src/Exception/GatewayException.php @@ -0,0 +1,15 @@ + $this->gateway; }, + string $message = '', + ?\Throwable $previous = null, + ) + { + parent::__construct($message, previous: $previous); + } +} diff --git a/src/Gateway/TMDBGateway.php b/src/Gateway/TMDBGateway.php index 3df943e..d2576a8 100644 --- a/src/Gateway/TMDBGateway.php +++ b/src/Gateway/TMDBGateway.php @@ -2,18 +2,22 @@ namespace App\Gateway; +use App\Context\TMDB\MovieCreditsContext; use App\Context\TMDB\MovieSearchContext; +use App\Exception\GatewayException; use App\Model\TMDB\TMDBMovie; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\ItemInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; +use Symfony\Contracts\HttpClient\ResponseInterface; use function Symfony\Component\String\u; class TMDBGateway { private const string SEARCH_URI = '/search/movie'; + private const string MOVIE_CREDITS_URI = '/movie/{id}/credits'; public function __construct( private readonly HttpClientInterface $client, @@ -25,24 +29,50 @@ class TMDBGateway ) { } + /** + * @throws GatewayException + */ public function searchMovie(string $movieName): ?TMDBMovie { $url = $this->host.self::SEARCH_URI.'?'.http_build_query(['query' => $movieName]); - try { - $response = $this->client->request('GET', $url, ['headers' => $this->getHeaders()]); - $result = $response->getContent(); - $searchContext = $this->serializer->deserialize($result, MovieSearchContext::class, 'json'); - if (empty($searchResult = $searchContext->getResults())) { - return null; - } - return reset($searchResult); - } catch (\Throwable) { + $searchContext = $this->fetchSerialized('GET', $url, MovieSearchContext::class); + if (empty($searchResult = $searchContext->getResults())) { return null; } + return reset($searchResult); + } + + /** + * @throws GatewayException + */ + public function getMovieCredits(int $movieId): ?MovieCreditsContext + { + $url = str_replace('{id}', $movieId, $this->host.self::MOVIE_CREDITS_URI); + return $this->fetchSerialized('GET', $url, MovieCreditsContext::class); + } + + /** + * @throws GatewayException + */ + private function fetch(string $method, string $url): ResponseInterface + { + try { + return $this->client->request($method, $url, ['headers' => ['Authorization' => 'Bearer '.$this->apiToken, 'accept' => 'application/json']]); + } catch (\Throwable $e) { + throw new GatewayException(self::class, $e->getMessage(), $e); + } } - private function getHeaders(): array + /** + * @throws GatewayException + */ + private function fetchSerialized(string $method, string $url, string $class, string $type = 'json'): mixed { - return ['Authorization' => 'Bearer '.$this->apiToken, 'accept' => 'application/json']; + $result = $this->fetch($method, $url); + try { + return $this->serializer->deserialize($result->getContent(), $class, $type); + } catch (\Throwable $e) { + throw new GatewayException(self::class, $e->getMessage(), $e); + } } } diff --git a/src/Model/TMDB/TMDBActor.php b/src/Model/TMDB/TMDBActor.php new file mode 100644 index 0000000..e1522c7 --- /dev/null +++ b/src/Model/TMDB/TMDBActor.php @@ -0,0 +1,13 @@ + $this->id; }, + public string $name { get => $this->name; }, + public float $popularity { get => $this->popularity; }, + public string $character { get => $this->character; }, + ) {} +}