diff --git a/src/Command/SyncActorsCommand.php b/src/Command/SyncActorsCommand.php index c2f33c0..15f9b5d 100644 --- a/src/Command/SyncActorsCommand.php +++ b/src/Command/SyncActorsCommand.php @@ -1,65 +1,37 @@ -em->getRepository(Movie::class)->findAll() as $film) { - try { - $creditsContext = $this->TMDBGateway->getMovieCredits($film->getTmdbId()); - } catch (GatewayException $e) { - $output->writeln('/!\ '.$e->getMessage()); - continue; - } - - if (!empty($creditsContext->cast)) { - $output->writeln('Syncing cast for '.$film->getTitle()); - } - foreach ($creditsContext->cast as $actorModel) { - // Get existing or create new - $actor = $this->em->getRepository(Actor::class)->findOneBy(['tmdbId' => $actorModel->id]); - if (!$actor instanceof Actor) { - $output->writeln('* New actor found: '.$actorModel->name); - - $actor = new Actor() - ->setPopularity($actorModel->popularity) - ->setName($actorModel->name) - ->setTmdbId($actorModel->id) - ; - - $this->em->persist($actor); - } - - // Get or create the role - if (0 < $this->em->getRepository(MovieRole::class)->count(['actor' => $actor, 'movie' => $film])) { - $actor->addMovieRole(new MovieRole() - ->setMovie($film) - ->setCharacter($actorModel->character) - ); - } - } - - $this->em->flush(); - } - - return Command::SUCCESS; - } -} +em->getRepository(Movie::class)->findAll() as $film) { + try { + $output->writeln('Syncing cast for '.$film->getTitle()); + $this->actorSyncer->syncActorsForMovie($film); + } catch (GatewayException $e) { + $output->writeln('/!\ '.$e->getMessage()); + continue; + } + + $this->em->flush(); + } + + return Command::SUCCESS; + } +} diff --git a/src/Service/ActorSyncer.php b/src/Service/ActorSyncer.php new file mode 100644 index 0000000..70ddc13 --- /dev/null +++ b/src/Service/ActorSyncer.php @@ -0,0 +1,52 @@ +tmdbGateway->getMovieCredits($movie->getTmdbId()); + + foreach ($creditsContext->cast as $actorModel) { + $actor = $this->em->getRepository(Actor::class)->findOneBy(['tmdbId' => $actorModel->id]); + if (!$actor instanceof Actor) { + $actor = new Actor() + ->setPopularity($actorModel->popularity) + ->setName($actorModel->name) + ->setTmdbId($actorModel->id); + + $this->em->persist($actor); + } + + $existingRole = $this->em->getRepository(MovieRole::class)->count(['actor' => $actor, 'movie' => $movie]); + if (0 === $existingRole) { + $role = new MovieRole() + ->setMovie($movie) + ->setActor($actor) + ->setCharacter($actorModel->character); + + $this->em->persist($role); + } + } + } +}