Use a VALUES clause to fetch awards for all actors of a film in a single SPARQL request, reducing Wikidata API calls from ~20 per film to 1 and avoiding idle timeout errors from rate limiting. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
99 lines
3.4 KiB
PHP
99 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\MessageHandler;
|
|
|
|
use App\Entity\Import;
|
|
use App\Entity\UserMovie;
|
|
use App\Message\ImportFilmsBatchMessage;
|
|
use App\Model\Ltbxd\LtbxdMovie;
|
|
use App\Repository\ImportRepository;
|
|
use App\Import\ActorSyncer;
|
|
use App\Import\AwardImporter;
|
|
use App\Import\FilmImporter;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
|
|
|
#[AsMessageHandler]
|
|
readonly class ImportFilmsBatchMessageHandler
|
|
{
|
|
public function __construct(
|
|
private EntityManagerInterface $em,
|
|
private FilmImporter $filmImporter,
|
|
private ActorSyncer $actorSyncer,
|
|
private ImportRepository $importRepository,
|
|
private AwardImporter $awardImporter,
|
|
private LoggerInterface $logger,
|
|
) {}
|
|
|
|
public function __invoke(ImportFilmsBatchMessage $message): void
|
|
{
|
|
$import = $this->em->getRepository(Import::class)->find($message->importId);
|
|
if (!$import) {
|
|
$this->logger->error('Import not found', ['importId' => $message->importId]);
|
|
|
|
return;
|
|
}
|
|
|
|
$batch = array_map(
|
|
fn (array $film) => new LtbxdMovie(
|
|
date: new \DateTime($film['date']),
|
|
name: $film['name'],
|
|
year: $film['year'],
|
|
ltbxdUri: $film['ltbxdUri'],
|
|
),
|
|
$message->films,
|
|
);
|
|
$userId = $import->getUser()->getId();
|
|
$importId = $import->getId();
|
|
|
|
foreach ($batch as $ltbxdMovie) {
|
|
try {
|
|
$movie = $this->filmImporter->importFromLtbxdMovie($ltbxdMovie);
|
|
if (!$movie) {
|
|
$this->importRepository->incrementFailedFilms($import);
|
|
} else {
|
|
$this->actorSyncer->syncActorsForMovie($movie);
|
|
|
|
$actors = array_map(fn ($role) => $role->getActor(), $movie->getActors()->toArray());
|
|
$this->awardImporter->importForActors($actors);
|
|
|
|
$user = $this->em->getReference(\App\Entity\User::class, $userId);
|
|
$existingLink = $this->em->getRepository(UserMovie::class)->findOneBy([
|
|
'user' => $user,
|
|
'movie' => $movie,
|
|
]);
|
|
if (!$existingLink) {
|
|
$userMovie = new UserMovie();
|
|
$userMovie->setUser($user);
|
|
$userMovie->setMovie($movie);
|
|
$this->em->persist($userMovie);
|
|
}
|
|
|
|
$this->em->flush();
|
|
}
|
|
} catch (\Throwable $e) {
|
|
$this->logger->warning('Failed to import film', [
|
|
'film' => $ltbxdMovie->getName(),
|
|
'importId' => $importId,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
$this->importRepository->incrementFailedFilms($import);
|
|
}
|
|
|
|
$processedFilms = $this->importRepository->incrementProcessedFilms($import);
|
|
|
|
$this->em->clear();
|
|
$import = $this->em->getRepository(Import::class)->find($importId);
|
|
|
|
if ($processedFilms >= $import->getTotalFilms()) {
|
|
$import->setStatus(Import::STATUS_COMPLETED);
|
|
$import->setCompletedAt(new \DateTimeImmutable());
|
|
$this->em->flush();
|
|
}
|
|
}
|
|
}
|
|
}
|