src/EventSubscriber/UserNotificacionSubscriber.php line 29
<?phpnamespace App\EventSubscriber;use App\Entity\User;use App\Entity\Evento;use App\Entity\Notificacion;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpKernel\KernelEvents;use Symfony\Component\HttpKernel\Event\ViewEvent;use ApiPlatform\Symfony\EventListener\EventPriorities;use Symfony\Bundle\SecurityBundle\Security;use Symfony\Component\EventDispatcher\EventSubscriberInterface;final class UserNotificacionSubscriber implements EventSubscriberInterface{public function __construct(private readonly EntityManagerInterface $entityManager, private readonly Security $security){}public static function getSubscribedEvents(){return [KernelEvents::VIEW => ['createNotificacion', EventPriorities::POST_WRITE],];}public function createNotificacion(ViewEvent $event): void{$user = $this->security->getUser();if (!$user instanceof User) {return;}$newUser = $event->getControllerResult();$method = $event->getRequest()->getMethod();if (!$newUser instanceof User || Request::METHOD_POST !== $method) {return;}$admins = $this->entityManager->getRepository(User::class)->findBy(['empresa' => $user->getEmpresa(), 'tipo' => User::TIPO_ADMIN]);foreach ($admins as $admin) {$notificacion = new Notificacion();$notificacion->setEmpresa($newUser->getEmpresa());$notificacion->setUser($admin);$notificacion->setTitulo('Nuevo ' . $newUser->getTipo() . ' creado');$notificacion->setIsNew(true);$notificacion->setModulo(User::MODULO);$notificacion->setModuloRef($newUser->getUuid());$notificacion->setHint($newUser->getUsername());$notificacion->setCreatedAt(new \DateTimeImmutable());$this->entityManager->persist($notificacion);}$this->entityManager->flush();}}