src/Controller/Componentes/NotaCorteController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Componentes;
  3. use App\Controller\Customs\Custom2IMController;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. /**
  10.  * @Route("/nota-corte")
  11.  */
  12. class NotaCorteController extends Custom2IMController
  13. {
  14.     protected const TABELA_EVS 'evs';
  15.     protected const CAMPO_EVS 'valor';
  16.     protected const DEFAULT_EVS 3.32;
  17.     protected const TABELA_GPS 'gps';
  18.     protected const DEFAULT_GPS 70;
  19.     protected const CAMPO_GPS 'performance';
  20.     private function __processaWhere(
  21.         float $notaCorte,
  22.         string $direcao
  23.     ): string
  24.     {
  25.         $campoPerformance $this->__getCampo();
  26.         return " AND {$campoPerformance} {$direcao} {$notaCorte}";
  27.     }
  28.     /**
  29.      * @Route("/", name="nota-corte_componente")
  30.      */
  31.     public function index(): Response
  32.     {
  33.         if (!$this->getSession()->has('nota_corte')) {
  34.             $this->setOnSession($this->__getMediaDefault());
  35.         }
  36.         return $this->render('componentes/nota_corte/componente.html.twig', [
  37.             'evs' => $this->getSession()->get('modelagem')->getPrograma()->getEvs(),
  38.             'nota_corte' => $this->getSession()->get('nota_corte')['valor']
  39.         ]);
  40.     }
  41.     /**
  42.      * @Route("/processa", name="nota-corte_processa")
  43.      */
  44.     public function processa(Request $request): JsonResponse
  45.     {
  46.         $notaCorte = (float) $request->request->get('nota_corte'$this->__getMediaDefault());
  47.         try {
  48.             $this->setOnSession($notaCorte);
  49.             $return = [
  50.                 'sucesso' => true,
  51.                 'mensagem' => 'Nota de Corte configurada!'
  52.             ];
  53.         } catch (\Throwable $th) {
  54.             $return = [
  55.                 'sucesso' => false,
  56.                 'mensagem' => 'Erro no sistema de Nota de Corte!'
  57.             ];
  58.         }
  59.         return new JsonResponse($return);
  60.     }
  61.     private function setOnSession(float $notaCorte)
  62.     {
  63.         $this->getSession()->set('nota_corte', [
  64.             'valor' => $notaCorte,
  65.             'where' => [
  66.                 'abaixo' => $this->__processaWhere($notaCorte'<'),
  67.                 'acima' => $this->__processaWhere($notaCorte'>=')
  68.             ],
  69.             'tabela' => $this->__getTabela(),
  70.             'campo' =>$this->__getCampo()
  71.         ]);
  72.     }
  73.     private function __getTabela()
  74.     {
  75.         return $this->getSession()->get('modelagem')->getPrograma()->getEvs() ? self::TABELA_EVS self::TABELA_GPS;
  76.     }
  77.     private function __getCampo()
  78.     {
  79.         return $this->getSession()->get('modelagem')->getPrograma()->getEvs() ? self::CAMPO_EVS self::CAMPO_GPS;
  80.     }
  81.     private function __getMediaDefault()
  82.     {
  83.         return $this->getSession()->get('modelagem')->getPrograma()->getEvs() ? self::DEFAULT_EVS self::DEFAULT_GPS;
  84.     }
  85. }