<?php
declare(strict_types=1);
namespace App\Bundles\DiseaseCaseBundle\EventListener;
use App\Bundles\DiagnosisBundle\Service\Field\FieldProvider;
use App\Bundles\DiseaseCaseBundle\Event\DiseaseCaseCreatedEvent;
use App\Bundles\DiseaseCaseBundle\Service\DiseaseCaseFieldValueCreator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
#[AsEventListener(event: DiseaseCaseCreatedEvent::class, method: 'onDiseaseCaseCreated')]
class DiseaseCaseCreatedEventListener
{
public const CLASSIFICATION_NAME = 'Класифікація випадку';
public function __construct(
private readonly DiseaseCaseFieldValueCreator $diseaseCaseFieldValueCreator,
private readonly EntityManagerInterface $entityManager,
private readonly FieldProvider $fieldProvider,
) {
}
public function onDiseaseCaseCreated(DiseaseCaseCreatedEvent $event): void
{
$diseaseCase = $event->getDiseaseCase();
$classification = $event->getClassification();
$diagnosis = $diseaseCase->getDiagnosis();
$classificationCatalog = $classification->getCatalog();
$field = $this->fieldProvider->provide(catalog: $classificationCatalog, diagnosis: $diagnosis);
if (is_null($field)) {
return;
}
$fieldValue = $this->diseaseCaseFieldValueCreator->create($diseaseCase, $field, $classification);
$this->entityManager->persist($fieldValue);
$this->entityManager->flush();
}
}