src/EventSubscriber/DocumentoNotificacionSubscriber.php line 29
<?phpnamespace App\EventSubscriber;use App\Entity\User;use App\Entity\Documento;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\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;final class DocumentoNotificacionSubscriber implements EventSubscriberInterface{public function __construct(private readonly EntityManagerInterface $entityManager, private readonly TokenStorageInterface $tokenStorage){}public static function getSubscribedEvents(){return [KernelEvents::VIEW => ['createNotificacion', EventPriorities::POST_WRITE],];}public function createNotificacion(ViewEvent $event): void{$token = $this->tokenStorage->getToken();if (!$token instanceof \Symfony\Component\Security\Core\Authentication\Token\TokenInterface) {return;}$user = $token->getUser();if (!$user instanceof User) {return;}$documento = $event->getControllerResult();$method = $event->getRequest()->getMethod();if (!$documento instanceof Documento || 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($user->getEmpresa());$notificacion->setUser($admin);$notificacion->setTitulo('Se ha subido un nuevo documento');$notificacion->setIsNew(true);$notificacion->setModulo(Documento::MODULO);$notificacion->setModuloRef($documento->getUuid());$notificacion->setHint($documento->getNombreArchivo());$this->entityManager->persist($notificacion);}$this->entityManager->flush();}}