enhance sync films

This commit is contained in:
thibaud-leclere
2026-01-19 23:22:04 +01:00
parent 5e715a40c6
commit b764116552
5 changed files with 116 additions and 8 deletions

View File

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