From bbbfb895af89da1a8c595576efc87da0d9163e05 Mon Sep 17 00:00:00 2001 From: thibaud-leclere Date: Sun, 29 Mar 2026 10:17:17 +0200 Subject: [PATCH] refactor: extract FilmImporter service from SyncFilmsCommands --- src/Command/SyncFilmsCommands.php | 129 +++++++++++++----------------- src/Service/FilmImporter.php | 47 +++++++++++ 2 files changed, 104 insertions(+), 72 deletions(-) create mode 100644 src/Service/FilmImporter.php diff --git a/src/Command/SyncFilmsCommands.php b/src/Command/SyncFilmsCommands.php index 4b5b250..5fd8159 100644 --- a/src/Command/SyncFilmsCommands.php +++ b/src/Command/SyncFilmsCommands.php @@ -1,72 +1,57 @@ -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; - } -} +ltbxdGateway->parseFile(); + } catch (GatewayException $e) { + $output->writeln('/!\ '.$e->getMessage()); + + return Command::FAILURE; + } + + $i = 0; + foreach ($ltbxdMovies as $ltbxdMovie) { + try { + $movie = $this->filmImporter->importFromLtbxdMovie($ltbxdMovie); + if ($movie) { + $output->writeln('* Found '.$ltbxdMovie->getName()); + } + } catch (GatewayException $e) { + $output->writeln('/!\ '.$e->getMessage()); + + return Command::FAILURE; + } + + ++$i; + if (0 === $i % 50) { + $this->em->flush(); + } + } + + $this->em->flush(); + + $output->writeln('Films synced'); + + return Command::SUCCESS; + } +} diff --git a/src/Service/FilmImporter.php b/src/Service/FilmImporter.php new file mode 100644 index 0000000..1a034e4 --- /dev/null +++ b/src/Service/FilmImporter.php @@ -0,0 +1,47 @@ +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; + } +}