refactor(directory) : harden report document upload (iri guard, orphan cleanup)

This commit is contained in:
Matthieu
2026-06-22 11:56:21 +02:00
parent b9538454a9
commit 33ba90a00d
@@ -16,6 +16,7 @@ use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Uid\Uuid;
use Throwable;
use function in_array;
@@ -88,8 +89,18 @@ final readonly class ReportDocumentProcessor implements ProcessorInterface
$document->setCreatedAt(new DateTimeImmutable());
$document->setUploadedBy($this->security->getUser());
$this->entityManager->persist($document);
$this->entityManager->flush();
try {
$this->entityManager->persist($document);
$this->entityManager->flush();
} catch (Throwable $e) {
$filePath = $this->uploadDir.'/'.$document->getFileName();
if (file_exists($filePath)) {
@unlink($filePath);
}
throw $e;
}
return $document;
}
@@ -137,11 +148,13 @@ final readonly class ReportDocumentProcessor implements ProcessorInterface
private function resolveReport(string $iri): CommercialReport
{
if ('' === $iri) {
throw new BadRequestHttpException('A commercialReport IRI is required.');
$idString = basename($iri);
if ('' === $iri || !ctype_digit($idString)) {
throw new BadRequestHttpException('A valid commercialReport IRI is required.');
}
$report = $this->entityManager->getRepository(CommercialReport::class)->find((int) basename($iri));
$report = $this->entityManager->getRepository(CommercialReport::class)->find((int) $idString);
if (null === $report) {
throw new BadRequestHttpException('Commercial report not found.');