src/Services/NotificacionesInternas.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Entity\SecMensaje;
  4. use App\Entity\TerPersona;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. class NotificacionesInternas
  7. {
  8.     private $entityManager;
  9.     public function __construct(EntityManagerInterface $entityManager)
  10.     {
  11.         $this->entityManager $entityManager;
  12.     }
  13.     # enviar notificaciones internas
  14.     public function notificacionInterna(TerPersona $persona,$tituloNotificacion "Sin Titulo"$mensajeNotificacion "Sin Mensaje",string $notificacionProgramada null)
  15.     {
  16.         # Closure funcion interna para ejecutar fecha programada
  17.         function calcularFechaProgramada(string $parametro): ?\DateTime
  18.         {
  19.             // Limpiar espacios
  20.             $parametro trim(strtolower($parametro));
  21.             // Validar formato: nĂºmero + letra (d, m, a)
  22.             if (!preg_match('/^(\d+)([dma])$/'$parametro$matches)) {
  23.                 return null;
  24.             }
  25.             $cantidad = (int) $matches[1];
  26.             $unidad $matches[2];
  27.             $fecha = new \DateTime(); // Fecha actual
  28.             switch ($unidad) {
  29.                 case 'd':
  30.                     $fecha->modify("+{$cantidad} days");
  31.                     break;
  32.                 case 'm':
  33.                     $fecha->modify("+{$cantidad} months");
  34.                     break;
  35.                 case 'a':
  36.                     $fecha->modify("+{$cantidad} years");
  37.                     break;
  38.             }
  39.             return $fecha;
  40.         }
  41.         $notiInterna = new SecMensaje();
  42.         $notiInterna->setProceso(
  43.             $this->entityManager->getRepository(\App\Entity\ParProceso::class)->find($persona->getPerfilCargo()->getParProceso())
  44.         );
  45.         $notiInterna->setColaborador($persona);
  46.         $notiInterna->setPerfilCargo($persona->getPerfilCargo());
  47.         $notiInterna->setEstado($this->entityManager->getRepository(\App\Entity\ParEstado::class)->find(1));
  48.         $notiInterna->setTipo(1);
  49.         $notiInterna->setTitulo($tituloNotificacion);
  50.         $notiInterna->setDescripcion($mensajeNotificacion);
  51.         $notiInterna->setLeido(false);
  52.         # Validar si se manda alguna fecha programada
  53.         if($notificacionProgramada){
  54.             $fechaProgramada calcularFechaProgramada($notificacionProgramada);
  55.             if($fechaProgramada){
  56.                 $notiInterna->setFechaProgramada($fechaProgramada); # Guardar la fecha programada
  57.             }
  58.         }
  59.         $this->entityManager->persist($notiInterna);
  60.         $this->entityManager->flush();
  61.     }
  62. }