src/EventSubscriber/EventoNotificacionSubscriber.php line 27

  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Symfony\EventListener\EventPriorities;
  4. use App\Entity\Evento;
  5. use App\Entity\Notificacion;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Event\ViewEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. final class EventoNotificacionSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(private readonly EntityManagerInterface $entityManager)
  14.     {
  15.     }
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             KernelEvents::VIEW => ['createNotificacion'EventPriorities::POST_WRITE],
  20.         ];
  21.     }
  22.     public function createNotificacion(ViewEvent $event): void
  23.     {
  24.         $evento $event->getControllerResult();
  25.         $method $event->getRequest()->getMethod();
  26.         if (!$evento instanceof Evento || Request::METHOD_POST !== $method) {
  27.             return;
  28.         }
  29.         foreach($evento->getUsers() as $user){
  30.             $notificacion = new Notificacion();
  31.             $notificacion->setEmpresa($evento->getCreatedBy()->getEmpresa());
  32.             $notificacion->setUser($user);
  33.             $notificacion->setTitulo($evento->getTitulo());
  34.             $notificacion->setIsNew(true);
  35.             $notificacion->setModulo(Evento::TIPO_MANUAL);
  36.             $notificacion->setModuloRef($evento->getUuid());
  37.             $notificacion->setHint($evento->getDescripcion());
  38.             $this->entityManager->persist($notificacion);
  39.         }
  40.         
  41.         $this->entityManager->flush();
  42.     }
  43. }