53 lines
1.5 KiB
PHP
53 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service;
|
|
|
|
use App\Entity\Actor;
|
|
use App\Entity\Movie;
|
|
use App\Entity\MovieRole;
|
|
use App\Exception\GatewayException;
|
|
use App\Gateway\TMDBGateway;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
readonly class ActorSyncer
|
|
{
|
|
public function __construct(
|
|
private TMDBGateway $tmdbGateway,
|
|
private EntityManagerInterface $em,
|
|
) {}
|
|
|
|
/**
|
|
* Fetch credits from TMDB for the given movie and create missing Actor/MovieRole entries.
|
|
*
|
|
* @throws GatewayException
|
|
*/
|
|
public function syncActorsForMovie(Movie $movie): void
|
|
{
|
|
$creditsContext = $this->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);
|
|
}
|
|
}
|
|
}
|
|
}
|