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

@@ -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;
}
}