src/EventSubscriber/CuestionarioEventSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Aviso;
  4. use App\Enum\TipoAvisoEnum;
  5. use App\Interfaces\AvisoRepositoryInterface;
  6. use App\Interfaces\ParticipanteGrupoDetalleRepositoryInterface;
  7. use App\Services\AsymmetricEncryptionService;
  8. use App\Services\DiplomaGeneratorService;
  9. use App\Services\EventDomain\CuestionarioChangedEvent;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. class CuestionarioEventSubscriber implements EventSubscriberInterface
  15. {
  16. public function __construct(
  17. private AsymmetricEncryptionService $asymmetricEncryptionService,
  18. private AvisoRepositoryInterface $avisoRepository,
  19. private TranslatorInterface $translator,
  20. private UrlGeneratorInterface $urlGenerator,
  21. private DiplomaGeneratorService $diplomaGeneratorService,
  22. private ParticipanteGrupoDetalleRepositoryInterface $participanteGrupoDetalleRepository,
  23. private LoggerInterface $logger,
  24. )
  25. {
  26. }
  27. public static function getSubscribedEvents()
  28. {
  29. return [
  30. CuestionarioChangedEvent::class => 'onChangedCuestionario',
  31. ];
  32. }
  33. public function onChangedCuestionario(CuestionarioChangedEvent $event): void
  34. {
  35. $cuestionario = $event->getCuestionario();
  36. $aviso = new Aviso();
  37. $aviso->setTipo(TipoAvisoEnum::AVISO_CUESTIONARIO);
  38. $aviso->setFecha($cuestionario->getCreatedAt());
  39. $aviso->setAsunto($this->translator->trans('new.ads.cuestionario', [], 'ads'));
  40. $aviso->setPropietarioContenido($cuestionario->getParticipantesGrupo()->getParticipanteGrupoDetalle()->getParticipante());
  41. $aviso->setObject($cuestionario);
  42. $aviso->setDeletedAt(null);
  43. $token = $this->asymmetricEncryptionService->hybridEncrypt([
  44. 'tipoAviso' => TipoAvisoEnum::AVISO_CUESTIONARIO,
  45. 'propietarioId' => $aviso->getPropietarioContenido()?->getId(),
  46. 'url' => $this->urlGenerator->generate('cuestionarios_pendientes', [], UrlGeneratorInterface::ABSOLUTE_URL),
  47. ]);
  48. $aviso->setTokenHash($token);
  49. $this->avisoRepository->save($aviso);
  50. // Generar certificado automáticamente tras completar el cuestionario
  51. try {
  52. // 1. Obtener el participante asociado al cuestionario.
  53. $participanteGrupoDetalle = $cuestionario->getParticipantesGrupo()?->getParticipanteGrupoDetalle();
  54. if (!$participanteGrupoDetalle) {
  55. // Log de advertencia si no se encuentra el participante y salir.
  56. $this->logger->warning('No se encontró un ParticipanteGrupoDetalle para el Cuestionario ID: ' . $cuestionario->getId());
  57. return;
  58. }
  59. // 2. Actualizar el estado del participante a "Finalizado".
  60. // (Se ha confirmado que completar el cuestionario es suficiente para aprobar).
  61. $participanteGrupoDetalle->setEstadoFinalizacion('F');
  62. $participanteGrupoDetalle->setCertificar(true);
  63. // 3. Guardar los cambios en la base de datos.
  64. $this->participanteGrupoDetalleRepository->save($participanteGrupoDetalle);
  65. // 4. Invocar el servicio para generar el diploma.
  66. $this->diplomaGeneratorService->generateDiplomaForParticipante($participanteGrupoDetalle);
  67. } catch (\Exception $e) {
  68. // Log del error para poder depurarlo sin detener el flujo.
  69. $this->logger->error('Error al generar el diploma tras completar el cuestionario: ' . $e->getMessage(), [
  70. 'cuestionarioId' => $cuestionario->getId(),
  71. 'exception' => $e
  72. ]);
  73. }
  74. }
  75. }