Starting actors populate
This commit is contained in:
13
src/Context/TMDB/MovieCreditsContext.php
Normal file
13
src/Context/TMDB/MovieCreditsContext.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Context\TMDB;
|
||||||
|
|
||||||
|
use App\Model\TMDB\TMDBActor;
|
||||||
|
|
||||||
|
class MovieCreditsContext
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
/** @var TMDBActor[] */
|
||||||
|
public array $cast { get => $this->cast; },
|
||||||
|
) {}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use App\Gateway\TMDBGateway;
|
use App\Gateway\TMDBGateway;
|
||||||
use App\Model\Ltbxd\LtbxdMovie;
|
use App\Repository\MovieRepository;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
@@ -14,23 +14,23 @@ use Symfony\Component\Serializer\SerializerInterface;
|
|||||||
class HomepageController extends AbstractController
|
class HomepageController extends AbstractController
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly TMDBGateway $TMDBGateway, private readonly SerializerInterface $serializer,
|
private readonly MovieRepository $movieRepository,
|
||||||
|
private readonly TMDBGateway $TMDBGateway,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
#[Route('/')]
|
#[Route('/')]
|
||||||
public function index(SerializerInterface $serializer): Response
|
public function index(SerializerInterface $serializer): Response
|
||||||
{
|
{
|
||||||
// $file = file_get_contents('files/watched.csv');
|
$movie = $this->movieRepository->findOneBy([]);
|
||||||
// $ltbxdMovies = $this->serializer->deserialize($file, LtbxdMovie::class.'[]', 'csv');
|
$creditsContext = $this->TMDBGateway->getMovieCredits($movie->getTmdbId());
|
||||||
// /** @var LtbxdMovie $ltbxdMovie */
|
$cast = $creditsContext->cast;
|
||||||
// $films = [];
|
$actors = [];
|
||||||
// foreach ($ltbxdMovies as $ltbxdMovie) {
|
foreach ($cast as $actor) {
|
||||||
// // Search movie on TMDB
|
if (2 <= $actor->popularity) {
|
||||||
// $film = $this->TMDBGateway->searchMovie($ltbxdMovie->getName());
|
$actors[] = $actor;
|
||||||
// if ($film) {
|
}
|
||||||
// $films[] = $film;
|
}
|
||||||
// }
|
dd($actors);
|
||||||
// }
|
|
||||||
|
|
||||||
return $this->render('homepage/index.html.twig');
|
return $this->render('homepage/index.html.twig');
|
||||||
}
|
}
|
||||||
|
|||||||
15
src/Exception/GatewayException.php
Normal file
15
src/Exception/GatewayException.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exception;
|
||||||
|
|
||||||
|
class GatewayException extends \Exception
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public string $gateway { get => $this->gateway; },
|
||||||
|
string $message = '',
|
||||||
|
?\Throwable $previous = null,
|
||||||
|
)
|
||||||
|
{
|
||||||
|
parent::__construct($message, previous: $previous);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,18 +2,22 @@
|
|||||||
|
|
||||||
namespace App\Gateway;
|
namespace App\Gateway;
|
||||||
|
|
||||||
|
use App\Context\TMDB\MovieCreditsContext;
|
||||||
use App\Context\TMDB\MovieSearchContext;
|
use App\Context\TMDB\MovieSearchContext;
|
||||||
|
use App\Exception\GatewayException;
|
||||||
use App\Model\TMDB\TMDBMovie;
|
use App\Model\TMDB\TMDBMovie;
|
||||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
use Symfony\Component\Serializer\SerializerInterface;
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
use Symfony\Contracts\Cache\CacheInterface;
|
use Symfony\Contracts\Cache\CacheInterface;
|
||||||
use Symfony\Contracts\Cache\ItemInterface;
|
use Symfony\Contracts\Cache\ItemInterface;
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||||
use function Symfony\Component\String\u;
|
use function Symfony\Component\String\u;
|
||||||
|
|
||||||
class TMDBGateway
|
class TMDBGateway
|
||||||
{
|
{
|
||||||
private const string SEARCH_URI = '/search/movie';
|
private const string SEARCH_URI = '/search/movie';
|
||||||
|
private const string MOVIE_CREDITS_URI = '/movie/{id}/credits';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly HttpClientInterface $client,
|
private readonly HttpClientInterface $client,
|
||||||
@@ -25,24 +29,50 @@ class TMDBGateway
|
|||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws GatewayException
|
||||||
|
*/
|
||||||
public function searchMovie(string $movieName): ?TMDBMovie
|
public function searchMovie(string $movieName): ?TMDBMovie
|
||||||
{
|
{
|
||||||
$url = $this->host.self::SEARCH_URI.'?'.http_build_query(['query' => $movieName]);
|
$url = $this->host.self::SEARCH_URI.'?'.http_build_query(['query' => $movieName]);
|
||||||
try {
|
$searchContext = $this->fetchSerialized('GET', $url, MovieSearchContext::class);
|
||||||
$response = $this->client->request('GET', $url, ['headers' => $this->getHeaders()]);
|
if (empty($searchResult = $searchContext->getResults())) {
|
||||||
$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;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
13
src/Model/TMDB/TMDBActor.php
Normal file
13
src/Model/TMDB/TMDBActor.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Model\TMDB;
|
||||||
|
|
||||||
|
class TMDBActor
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public int $id { get => $this->id; },
|
||||||
|
public string $name { get => $this->name; },
|
||||||
|
public float $popularity { get => $this->popularity; },
|
||||||
|
public string $character { get => $this->character; },
|
||||||
|
) {}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user