refactor: extract FilmImporter service from SyncFilmsCommands

This commit is contained in:
thibaud-leclere
2026-03-29 10:17:17 +02:00
parent 1bf8afd88e
commit bbbfb895af
2 changed files with 104 additions and 72 deletions

View File

@@ -1,72 +1,57 @@
<?php <?php
namespace App\Command; namespace App\Command;
use App\Entity\Movie; use App\Exception\GatewayException;
use App\Exception\GatewayException; use App\Gateway\LtbxdGateway;
use App\Gateway\LtbxdGateway; use App\Service\FilmImporter;
use App\Gateway\TMDBGateway; 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; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand('app:sync-films')]
#[AsCommand('app:sync-films')] readonly class SyncFilmsCommands
readonly class SyncFilmsCommands {
{ public function __construct(
public function __construct( private LtbxdGateway $ltbxdGateway,
private LtbxdGateway $ltbxdGateway, private FilmImporter $filmImporter,
private TMDBGateway $TMDBGateway, private EntityManagerInterface $em,
private EntityManagerInterface $em, ) {}
) {}
public function __invoke(OutputInterface $output): int
public function __invoke(OutputInterface $output): int {
{ try {
try { $ltbxdMovies = $this->ltbxdGateway->parseFile();
$ltbxdMovies = $this->ltbxdGateway->parseFile(); } catch (GatewayException $e) {
} catch (GatewayException $e) { $output->writeln('/!\ '.$e->getMessage());
$output->writeln('/!\ '.$e->getMessage());
return Command::FAILURE;
return Command::FAILURE; }
}
$i = 0;
$i = 0; foreach ($ltbxdMovies as $ltbxdMovie) {
foreach ($ltbxdMovies as $ltbxdMovie) { try {
// If the movie already exists, skip $movie = $this->filmImporter->importFromLtbxdMovie($ltbxdMovie);
if (0 < $this->em->getRepository(Movie::class)->count(['ltbxdRef' => $ltbxdMovie->getLtbxdRef()])) { if ($movie) {
continue; $output->writeln('* Found '.$ltbxdMovie->getName());
} }
} catch (GatewayException $e) {
// Search movie on TMDB $output->writeln('/!\ '.$e->getMessage());
try {
$film = $this->TMDBGateway->searchMovie($ltbxdMovie->getName()); return Command::FAILURE;
} catch (GatewayException $e) { }
$output->writeln('/!\ '.$e->getMessage());
++$i;
return Command::FAILURE; if (0 === $i % 50) {
} $this->em->flush();
}
if ($film) { }
$output->writeln('* Found '.$ltbxdMovie->getName());
$this->em->flush();
$filmEntity = new Movie()
->setLtbxdRef($ltbxdMovie->getLtbxdRef()) $output->writeln('Films synced');
->setTitle($ltbxdMovie->getName())
->setTmdbId($film->getId()) return Command::SUCCESS;
; }
$this->em->persist($filmEntity); }
}
++$i;
if (0 === $i % 50) {
$this->em->flush();
}
}
$this->em->flush();
$output->writeln('Films synced');
return Command::SUCCESS;
}
}

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Entity\Movie;
use App\Exception\GatewayException;
use App\Gateway\TMDBGateway;
use App\Model\Ltbxd\LtbxdMovie;
use Doctrine\ORM\EntityManagerInterface;
readonly class FilmImporter
{
public function __construct(
private TMDBGateway $tmdbGateway,
private EntityManagerInterface $em,
) {}
/**
* Find an existing Movie by ltbxdRef or create a new one via TMDB.
* Returns null if the movie is not found on TMDB.
*
* @throws GatewayException
*/
public function importFromLtbxdMovie(LtbxdMovie $ltbxdMovie): ?Movie
{
$existing = $this->em->getRepository(Movie::class)->findOneBy(['ltbxdRef' => $ltbxdMovie->getLtbxdRef()]);
if ($existing) {
return $existing;
}
$tmdbMovie = $this->tmdbGateway->searchMovie($ltbxdMovie->getName());
if (!$tmdbMovie) {
return null;
}
$movie = new Movie()
->setLtbxdRef($ltbxdMovie->getLtbxdRef())
->setTitle($ltbxdMovie->getName())
->setTmdbId($tmdbMovie->getId());
$this->em->persist($movie);
return $movie;
}
}