wip
This commit is contained in:
@@ -2,33 +2,43 @@
|
||||
|
||||
namespace App\Gateway;
|
||||
|
||||
use App\Context\TMDB\MovieSearchContext;
|
||||
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 function Symfony\Component\String\u;
|
||||
|
||||
class TMDBGateway
|
||||
{
|
||||
private const TMDB_URL = 'https://api.themoviedb.org/3';
|
||||
private const SEARCH_URI = '/search/movie';
|
||||
private const string SEARCH_URI = '/search/movie';
|
||||
|
||||
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)%')]
|
||||
private readonly string $host,
|
||||
) {
|
||||
}
|
||||
|
||||
public function findMovie(string $movieName)
|
||||
public function searchMovie(string $movieName): ?MovieSearchContext
|
||||
{
|
||||
$url = self::TMDB_URL.self::SEARCH_URI.'?'.http_build_query(['query' => $movieName]);
|
||||
$cacheKey = 'tmdb_movie.'.u($movieName)->snake();
|
||||
|
||||
try {
|
||||
$response = $this->client->request('GET', $url, ['headers' => $this->getHeaders()]);
|
||||
$result = $response->getContent();
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return json_decode($this->client->request('GET', $url, ['headers' => $this->getHeaders()])->getContent(), associative: true);
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function getHeaders(): array
|
||||
|
||||
Reference in New Issue
Block a user