src/EventSubscriber/MensajeNotificacionSubscriber.php line 27
<?phpnamespace App\EventSubscriber;use ApiPlatform\Symfony\EventListener\EventPriorities;use App\Entity\Mensaje;use App\Entity\Notificacion;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpKernel\Event\ViewEvent;use Symfony\Component\HttpKernel\KernelEvents;final class MensajeNotificacionSubscriber implements EventSubscriberInterface{public function __construct(private readonly EntityManagerInterface $entityManager){}public static function getSubscribedEvents(){return [KernelEvents::VIEW => ['createNotificacion', EventPriorities::POST_WRITE],];}public function createNotificacion(ViewEvent $event): void{$mensaje = $event->getControllerResult();$method = $event->getRequest()->getMethod();if (!$mensaje instanceof Mensaje || Request::METHOD_POST !== $method) {return;}foreach($mensaje->getToUsers() as $user){$notificaicon = new Notificacion();$notificaicon->setEmpresa($mensaje->getCreatedBy()->getEmpresa());$notificaicon->setUser($user);$notificaicon->setTitulo($mensaje->getAsunto());$notificaicon->setIsNew(true);$notificaicon->setModulo(Mensaje::MODULO);$notificaicon->setModuloRef($mensaje->getUuid());$notificaicon->setHint($mensaje->getCuerpo());$this->entityManager->persist($notificaicon);}$this->entityManager->flush();}}