refactor: extract ActorSyncer service from SyncActorsCommand

This commit is contained in:
thibaud-leclere
2026-03-29 10:18:00 +02:00
parent bbbfb895af
commit dedc41e237
2 changed files with 89 additions and 65 deletions

View File

@@ -1,65 +1,37 @@
<?php <?php
namespace App\Command; namespace App\Command;
use App\Entity\Actor; use App\Entity\Movie;
use App\Entity\Movie; use App\Exception\GatewayException;
use App\Entity\MovieRole; use App\Service\ActorSyncer;
use App\Exception\GatewayException; use Doctrine\ORM\EntityManagerInterface;
use App\Gateway\TMDBGateway; use Symfony\Component\Console\Attribute\AsCommand;
use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface; #[AsCommand('app:sync-actors')]
readonly class SyncActorsCommand
#[AsCommand('app:sync-actors')] {
readonly class SyncActorsCommand public function __construct(
{ private ActorSyncer $actorSyncer,
public function __construct ( private EntityManagerInterface $em,
private TMDBGateway $TMDBGateway, ) {}
private EntityManagerInterface $em,
) {} public function __invoke(OutputInterface $output): int
{
public function __invoke(OutputInterface $output): int foreach ($this->em->getRepository(Movie::class)->findAll() as $film) {
{ try {
foreach ($this->em->getRepository(Movie::class)->findAll() as $film) { $output->writeln('Syncing cast for '.$film->getTitle());
try { $this->actorSyncer->syncActorsForMovie($film);
$creditsContext = $this->TMDBGateway->getMovieCredits($film->getTmdbId()); } catch (GatewayException $e) {
} catch (GatewayException $e) { $output->writeln('/!\ '.$e->getMessage());
$output->writeln('/!\ '.$e->getMessage()); continue;
continue; }
}
$this->em->flush();
if (!empty($creditsContext->cast)) { }
$output->writeln('Syncing cast for '.$film->getTitle());
} return Command::SUCCESS;
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();
}
return Command::SUCCESS;
}
}

View 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);
}
}
}
}