refactor: extract FilmImporter service from SyncFilmsCommands
This commit is contained in:
@@ -2,10 +2,9 @@
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\Movie;
|
||||
use App\Exception\GatewayException;
|
||||
use App\Gateway\LtbxdGateway;
|
||||
use App\Gateway\TMDBGateway;
|
||||
use App\Service\FilmImporter;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
@@ -16,7 +15,7 @@ readonly class SyncFilmsCommands
|
||||
{
|
||||
public function __construct(
|
||||
private LtbxdGateway $ltbxdGateway,
|
||||
private TMDBGateway $TMDBGateway,
|
||||
private FilmImporter $filmImporter,
|
||||
private EntityManagerInterface $em,
|
||||
) {}
|
||||
|
||||
@@ -32,31 +31,17 @@ readonly class SyncFilmsCommands
|
||||
|
||||
$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());
|
||||
$movie = $this->filmImporter->importFromLtbxdMovie($ltbxdMovie);
|
||||
if ($movie) {
|
||||
$output->writeln('* Found '.$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();
|
||||
|
||||
47
src/Service/FilmImporter.php
Normal file
47
src/Service/FilmImporter.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user