src/EventSubscriber/MensajeNotificacionSubscriber.php line 27

  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Symfony\EventListener\EventPriorities;
  4. use App\Entity\Mensaje;
  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 MensajeNotificacionSubscriber 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.         $mensaje $event->getControllerResult();
  25.         $method $event->getRequest()->getMethod();
  26.         if (!$mensaje instanceof Mensaje || Request::METHOD_POST !== $method) {
  27.             return;
  28.         }
  29.         foreach($mensaje->getToUsers() as $user){
  30.             $notificaicon = new Notificacion();
  31.             $notificaicon->setEmpresa($mensaje->getCreatedBy()->getEmpresa());
  32.             $notificaicon->setUser($user);
  33.             $notificaicon->setTitulo($mensaje->getAsunto());
  34.             $notificaicon->setIsNew(true);
  35.             $notificaicon->setModulo(Mensaje::MODULO);
  36.             $notificaicon->setModuloRef($mensaje->getUuid());
  37.             $notificaicon->setHint($mensaje->getCuerpo());
  38.             $this->entityManager->persist($notificaicon);
  39.         }
  40.         
  41.         $this->entityManager->flush();
  42.     }
  43. }