refactor: extract ActorSyncer service from SyncActorsCommand
This commit is contained in:
@@ -2,11 +2,9 @@
|
|||||||
|
|
||||||
namespace App\Command;
|
namespace App\Command;
|
||||||
|
|
||||||
use App\Entity\Actor;
|
|
||||||
use App\Entity\Movie;
|
use App\Entity\Movie;
|
||||||
use App\Entity\MovieRole;
|
|
||||||
use App\Exception\GatewayException;
|
use App\Exception\GatewayException;
|
||||||
use App\Gateway\TMDBGateway;
|
use App\Service\ActorSyncer;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Component\Console\Attribute\AsCommand;
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
@@ -15,8 +13,8 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||||||
#[AsCommand('app:sync-actors')]
|
#[AsCommand('app:sync-actors')]
|
||||||
readonly class SyncActorsCommand
|
readonly class SyncActorsCommand
|
||||||
{
|
{
|
||||||
public function __construct (
|
public function __construct(
|
||||||
private TMDBGateway $TMDBGateway,
|
private ActorSyncer $actorSyncer,
|
||||||
private EntityManagerInterface $em,
|
private EntityManagerInterface $em,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@@ -24,39 +22,13 @@ readonly class SyncActorsCommand
|
|||||||
{
|
{
|
||||||
foreach ($this->em->getRepository(Movie::class)->findAll() as $film) {
|
foreach ($this->em->getRepository(Movie::class)->findAll() as $film) {
|
||||||
try {
|
try {
|
||||||
$creditsContext = $this->TMDBGateway->getMovieCredits($film->getTmdbId());
|
$output->writeln('Syncing cast for '.$film->getTitle());
|
||||||
|
$this->actorSyncer->syncActorsForMovie($film);
|
||||||
} catch (GatewayException $e) {
|
} catch (GatewayException $e) {
|
||||||
$output->writeln('/!\ '.$e->getMessage());
|
$output->writeln('/!\ '.$e->getMessage());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($creditsContext->cast)) {
|
|
||||||
$output->writeln('Syncing cast for '.$film->getTitle());
|
|
||||||
}
|
|
||||||
foreach ($creditsContext->cast as $actorModel) {
|
|
||||||
// Get existing or create new
|
|
||||||
$actor = $this->em->getRepository(Actor::class)->findOneBy(['tmdbId' => $actorModel->id]);
|
|
||||||
if (!$actor instanceof Actor) {
|
|
||||||
$output->writeln('* New actor found: '.$actorModel->name);
|
|
||||||
|
|
||||||
$actor = new Actor()
|
|
||||||
->setPopularity($actorModel->popularity)
|
|
||||||
->setName($actorModel->name)
|
|
||||||
->setTmdbId($actorModel->id)
|
|
||||||
;
|
|
||||||
|
|
||||||
$this->em->persist($actor);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get or create the role
|
|
||||||
if (0 < $this->em->getRepository(MovieRole::class)->count(['actor' => $actor, 'movie' => $film])) {
|
|
||||||
$actor->addMovieRole(new MovieRole()
|
|
||||||
->setMovie($film)
|
|
||||||
->setCharacter($actorModel->character)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->em->flush();
|
$this->em->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
52
src/Service/ActorSyncer.php
Normal file
52
src/Service/ActorSyncer.php
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use App\Entity\Actor;
|
||||||
|
use App\Entity\Movie;
|
||||||
|
use App\Entity\MovieRole;
|
||||||
|
use App\Exception\GatewayException;
|
||||||
|
use App\Gateway\TMDBGateway;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
readonly class ActorSyncer
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private TMDBGateway $tmdbGateway,
|
||||||
|
private EntityManagerInterface $em,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch credits from TMDB for the given movie and create missing Actor/MovieRole entries.
|
||||||
|
*
|
||||||
|
* @throws GatewayException
|
||||||
|
*/
|
||||||
|
public function syncActorsForMovie(Movie $movie): void
|
||||||
|
{
|
||||||
|
$creditsContext = $this->tmdbGateway->getMovieCredits($movie->getTmdbId());
|
||||||
|
|
||||||
|
foreach ($creditsContext->cast as $actorModel) {
|
||||||
|
$actor = $this->em->getRepository(Actor::class)->findOneBy(['tmdbId' => $actorModel->id]);
|
||||||
|
if (!$actor instanceof Actor) {
|
||||||
|
$actor = new Actor()
|
||||||
|
->setPopularity($actorModel->popularity)
|
||||||
|
->setName($actorModel->name)
|
||||||
|
->setTmdbId($actorModel->id);
|
||||||
|
|
||||||
|
$this->em->persist($actor);
|
||||||
|
}
|
||||||
|
|
||||||
|
$existingRole = $this->em->getRepository(MovieRole::class)->count(['actor' => $actor, 'movie' => $movie]);
|
||||||
|
if (0 === $existingRole) {
|
||||||
|
$role = new MovieRole()
|
||||||
|
->setMovie($movie)
|
||||||
|
->setActor($actor)
|
||||||
|
->setCharacter($actorModel->character);
|
||||||
|
|
||||||
|
$this->em->persist($role);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user