ltbxd-actorle/src/Controller/ImportController.php

103 lines
3.3 KiB
PHP
Raw Normal View History

2026-03-29 08:21:02 +00:00
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\Import;
use App\Entity\User;
use App\Message\ProcessImportMessage;
use App\Repository\ImportRepository;
2026-03-29 08:21:02 +00:00
use Doctrine\ORM\EntityManagerInterface;
use League\Flysystem\FilesystemOperator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class ImportController extends AbstractController
{
#[Route('/api/imports/latest', methods: ['GET'])]
#[IsGranted('ROLE_USER')]
public function latest(ImportRepository $importRepository): JsonResponse
{
/** @var User $user */
$user = $this->getUser();
$import = $importRepository->findLatestForUser($user);
if (!$import) {
return $this->json(null);
}
return $this->json([
'id' => $import->getId(),
'status' => $import->getStatus(),
'totalFilms' => $import->getTotalFilms(),
'processedFilms' => $import->getProcessedFilms(),
'failedFilms' => $import->getFailedFilms(),
]);
}
2026-03-29 08:21:02 +00:00
#[Route('/api/imports', methods: ['POST'])]
#[IsGranted('ROLE_USER')]
public function create(
Request $request,
FilesystemOperator $defaultStorage,
EntityManagerInterface $em,
ImportRepository $importRepository,
2026-03-29 08:21:02 +00:00
MessageBusInterface $bus,
): JsonResponse {
/** @var User $user */
$user = $this->getUser();
if ($importRepository->hasActiveImport($user)) {
return $this->json(['error' => 'Un import est déjà en cours.'], Response::HTTP_CONFLICT);
}
2026-03-29 08:21:02 +00:00
$file = $request->files->get('file');
if (!$file) {
return $this->json(['error' => 'No file provided.'], Response::HTTP_UNPROCESSABLE_ENTITY);
}
if ('csv' !== $file->getClientOriginalExtension()) {
return $this->json(['error' => 'Only CSV files are accepted.'], Response::HTTP_UNPROCESSABLE_ENTITY);
}
if ($file->getSize() > 5 * 1024 * 1024) {
return $this->json(['error' => 'File too large (max 5 MB).'], Response::HTTP_UNPROCESSABLE_ENTITY);
}
$import = new Import();
$import->setUser($user);
$import->setFilePath('pending');
2026-03-29 08:21:02 +00:00
$em->persist($import);
$em->flush();
try {
$filePath = sprintf('imports/%d/%d.csv', $user->getId(), $import->getId());
$defaultStorage->write($filePath, file_get_contents($file->getPathname()));
2026-03-29 08:21:02 +00:00
$import->setFilePath($filePath);
$em->flush();
$bus->dispatch(new ProcessImportMessage($import->getId()));
} catch (\Throwable) {
$import->setStatus(Import::STATUS_FAILED);
$em->flush();
2026-03-29 08:21:02 +00:00
return $this->json(['error' => 'An error occurred while starting the import.'], Response::HTTP_INTERNAL_SERVER_ERROR);
}
2026-03-29 08:21:02 +00:00
return $this->json([
'id' => $import->getId(),
'status' => $import->getStatus(),
], Response::HTTP_CREATED);
}
}