init
This commit is contained in:
38
src/Gateway/TMDBGateway.php
Normal file
38
src/Gateway/TMDBGateway.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Gateway;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
class TMDBGateway
|
||||
{
|
||||
private const TMDB_URL = 'https://api.themoviedb.org/3';
|
||||
private const SEARCH_URI = '/search/movie';
|
||||
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $client,
|
||||
#[Autowire('%env(TMDB_API_TOKEN)%')]
|
||||
private readonly string $apiToken,
|
||||
) {
|
||||
}
|
||||
|
||||
public function findMovie(string $movieName)
|
||||
{
|
||||
$url = self::TMDB_URL.self::SEARCH_URI.'?'.http_build_query(['query' => $movieName]);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private function getHeaders(): array
|
||||
{
|
||||
return ['Authorization' => 'Bearer '.$this->apiToken, 'accept' => 'application/json'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user