src/EventSubscriber/ItemDocumentDeleteEventSubscriber.php line 39
<?phpdeclare(strict_types=1);namespace App\EventSubscriber;use ApiPlatform\Core\EventListener\EventPriorities;use App\Entity\ItemDocument;use App\Entity\ItemDocumentIndicator;use App\Entity\ItemDocumentReport;use App\Enum\ReportStatusEnum;use App\Repository\ItemDocumentIndicatorRepository;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\Filesystem\Filesystem;use Symfony\Component\HttpFoundation\JsonResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\HttpKernel\Event\ViewEvent;use Symfony\Component\HttpKernel\KernelEvents;use Symfony\Contracts\Translation\TranslatorInterface;final class ItemDocumentDeleteEventSubscriber implements EventSubscriberInterface{public function __construct(private readonly ItemDocumentIndicatorRepository $documentIndicatorRepository,private readonly TranslatorInterface $translator,private readonly Filesystem $filesystem){}public static function getSubscribedEvents(): array{return [KernelEvents::VIEW => ['unlinkDocument', EventPriorities::PRE_WRITE]];}public function unlinkDocument(ViewEvent $event): void{$entity = $event->getControllerResult();if (!$entity instanceof ItemDocument||Request::METHOD_DELETE !== $event->getRequest()->getMethod()) {return;}$itemDocumentIndicators = $this->documentIndicatorRepository->findBy(['itemDocument' => $entity]);/** @var ItemDocumentIndicator $itemDocumentsIndicator */foreach ($itemDocumentIndicators as $itemDocumentIndicator) {if (null !== $itemDocumentIndicator->getValue()) {$event->setResponse(new JsonResponse(['message' => $this->translator->trans('programs.project.itemDocument.indicatorValueDeleteError')],Response::HTTP_UNPROCESSABLE_ENTITY));}}$this->filesystem->remove($entity->getFullDocumentPath());}}