2026-03-29 08:20:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\MessageHandler;
|
|
|
|
|
|
|
|
|
|
use App\Entity\Import;
|
|
|
|
|
use App\Entity\UserMovie;
|
|
|
|
|
use App\Gateway\LtbxdGateway;
|
|
|
|
|
use App\Message\ImportFilmsBatchMessage;
|
|
|
|
|
use App\Repository\ImportRepository;
|
|
|
|
|
use App\Service\ActorSyncer;
|
2026-04-01 12:27:54 +00:00
|
|
|
use App\Service\AwardImporter;
|
2026-03-29 08:20:24 +00:00
|
|
|
use App\Service\FilmImporter;
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
use League\Flysystem\FilesystemOperator;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
|
|
|
|
|
|
|
|
|
#[AsMessageHandler]
|
|
|
|
|
readonly class ImportFilmsBatchMessageHandler
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
private EntityManagerInterface $em,
|
|
|
|
|
private FilesystemOperator $defaultStorage,
|
|
|
|
|
private LtbxdGateway $ltbxdGateway,
|
|
|
|
|
private FilmImporter $filmImporter,
|
|
|
|
|
private ActorSyncer $actorSyncer,
|
|
|
|
|
private ImportRepository $importRepository,
|
2026-04-01 12:44:08 +00:00
|
|
|
private AwardImporter $awardImporter,
|
2026-03-29 08:20:24 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$csvContent = $this->defaultStorage->read($import->getFilePath());
|
|
|
|
|
$tmpFile = tempnam(sys_get_temp_dir(), 'import_');
|
|
|
|
|
file_put_contents($tmpFile, $csvContent);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$ltbxdMovies = $this->ltbxdGateway->parseFileFromPath($tmpFile);
|
|
|
|
|
} finally {
|
|
|
|
|
unlink($tmpFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$batch = array_slice($ltbxdMovies, $message->offset, $message->limit);
|
2026-03-31 19:34:11 +00:00
|
|
|
$userId = $import->getUser()->getId();
|
|
|
|
|
$importId = $import->getId();
|
2026-03-29 08:20:24 +00:00
|
|
|
|
|
|
|
|
foreach ($batch as $ltbxdMovie) {
|
|
|
|
|
try {
|
|
|
|
|
$movie = $this->filmImporter->importFromLtbxdMovie($ltbxdMovie);
|
|
|
|
|
if (!$movie) {
|
|
|
|
|
$this->importRepository->incrementFailedFilms($import);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->actorSyncer->syncActorsForMovie($movie);
|
|
|
|
|
|
2026-04-01 12:27:54 +00:00
|
|
|
// Import awards for actors of this movie
|
|
|
|
|
foreach ($movie->getActors() as $role) {
|
|
|
|
|
$this->awardImporter->importForActor($role->getActor());
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 19:34:11 +00:00
|
|
|
$user = $this->em->getReference(\App\Entity\User::class, $userId);
|
2026-03-29 08:20:24 +00:00
|
|
|
$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(),
|
2026-03-31 19:34:11 +00:00
|
|
|
'importId' => $importId,
|
2026-03-29 08:20:24 +00:00
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
]);
|
|
|
|
|
$this->importRepository->incrementFailedFilms($import);
|
|
|
|
|
}
|
2026-03-31 19:34:11 +00:00
|
|
|
|
|
|
|
|
$this->em->clear();
|
|
|
|
|
$import = $this->em->getRepository(Import::class)->find($importId);
|
2026-03-29 08:20:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$processedBatches = $this->importRepository->incrementProcessedBatches($import);
|
|
|
|
|
|
|
|
|
|
if ($processedBatches >= $import->getTotalBatches()) {
|
|
|
|
|
// Refresh the entity to get updated failedFilms from DB
|
|
|
|
|
$this->em->refresh($import);
|
|
|
|
|
|
|
|
|
|
$import->setStatus(Import::STATUS_COMPLETED);
|
|
|
|
|
$import->setCompletedAt(new \DateTimeImmutable());
|
|
|
|
|
$this->em->flush();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|