58 lines
1.5 KiB
PHP
58 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Command;
|
|
|
|
use App\Exception\GatewayException;
|
|
use App\Gateway\LtbxdGateway;
|
|
use App\Service\FilmImporter;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
#[AsCommand('app:sync-films')]
|
|
readonly class SyncFilmsCommands
|
|
{
|
|
public function __construct(
|
|
private LtbxdGateway $ltbxdGateway,
|
|
private FilmImporter $filmImporter,
|
|
private EntityManagerInterface $em,
|
|
) {}
|
|
|
|
public function __invoke(OutputInterface $output): int
|
|
{
|
|
try {
|
|
$ltbxdMovies = $this->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;
|
|
}
|
|
}
|