Add db, sync movies command

This commit is contained in:
thibaud-leclere
2026-01-14 00:54:49 +01:00
parent e5d5fe4343
commit 5c35aff23b
11 changed files with 264 additions and 32 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Gateway;
use App\Context\TMDB\MovieSearchContext;
use App\Model\TMDB\TMDBMovie;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\Cache\CacheInterface;
@@ -17,7 +18,6 @@ class TMDBGateway
public function __construct(
private readonly HttpClientInterface $client,
private readonly SerializerInterface $serializer,
private readonly CacheInterface $cache,
#[Autowire('%env(TMDB_API_TOKEN)%')]
private readonly string $apiToken,
#[Autowire('%env(TMDB_HOST)%')]
@@ -25,20 +25,20 @@ class TMDBGateway
) {
}
public function searchMovie(string $movieName): ?MovieSearchContext
public function searchMovie(string $movieName): ?TMDBMovie
{
$cacheKey = 'tmdb_movie.'.u($movieName)->snake();
return $this->cache->get($cacheKey, function (ItemInterface $item) use ($movieName) {
$url = $this->host.self::SEARCH_URI.'?'.http_build_query(['query' => $movieName]);
try {
$response = $this->client->request('GET', $url, ['headers' => $this->getHeaders()]);
$result = $response->getContent();
return $this->serializer->deserialize($result, MovieSearchContext::class, 'json');
} catch (\Throwable) {
$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) {
return null;
}
}
private function getHeaders(): array