Add db, sync movies command
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user