<?php
declare(strict_types=1);
namespace App\Bundles\PatientBundle\EventListener;
use App\Bundles\PatientBundle\Event\PatientDeleteEvent;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
#[AsEventListener(event: PatientDeleteEvent::class, method: 'onDelete')]
class PatientSoftDeleteListener
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
) {
}
public function onDelete(PatientDeleteEvent $event): void
{
$patient = $event->getPatient();
if (!$patient->isDeleted()) {
return;
}
foreach ($patient->getDiseaseCases() as $diseaseCase) {
$diseaseCase->setDeletedAt(new DateTime());
if ($outbreak = $diseaseCase->getOutbreak()) {
$outbreak->decreaseTotalCases();
$this->entityManager->persist($outbreak);
}
$this->entityManager->persist($diseaseCase);
}
$this->entityManager->flush();
}
}