src/Controller/BaseController.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CargosSistema;
  4. use App\Entity\FeeMinimo;
  5. use App\Entity\Fees;
  6. use App\Entity\Mantenimiento;
  7. use App\Entity\Mwamin;
  8. use App\Entity\Porcentajes;
  9. use App\Entity\PrecioCoste;
  10. use App\Entity\PrecioPotencia;
  11. use App\Repository\CargosSistemaRepository;
  12. use App\Repository\FeeMinimoRepository;
  13. use App\Repository\FeesRepository;
  14. use App\Repository\MantenimientoRepository;
  15. use App\Repository\MwaminRepository;
  16. use App\Repository\PorcentajesRepository;
  17. use App\Repository\PrecioCosteRepository;
  18. use App\Repository\PrecioPotenciaRepository;
  19. use App\Service\MainService;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\RequestStack;
  25. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  26. class BaseController extends AbstractController
  27. {
  28.     // Tipo Factura
  29.     public const TIPO_HOGAR 'tipohogar';
  30.     public const TIPO_EMPRESA 'tipoempresa';
  31.     // Basic parameters
  32.     const BASIC_PARAMS = ['id''idClienteAplicacion''empDato''user''password'];
  33.     // Repositories
  34.     /** @var CargosSistemaRepository */
  35.     protected $cargosSistemaRepo;
  36.     /** @var FeeMinimoRepository */
  37.     protected $feeMinimoRepo;
  38.     /** @var FeesRepository */
  39.     protected $feesRepo;
  40.     /** @var MantenimientoRepository */
  41.     protected $mantenimientoRepo;
  42.     /** @var MwaminRepository */
  43.     protected $mwaminRepo;
  44.     /** @var PorcentajesRepository */
  45.     protected $porcentajesRepo;
  46.     /** @var PrecioCosteRepository */
  47.     protected $precioCosteRepo;
  48.     /** @var PrecioPotenciaRepository */
  49.     protected $precioPotenciaRepo;
  50.     /** @var SessionInterface */
  51.     protected $session;
  52.     /** @var MainService */
  53.     protected $service;
  54.     /** @var EntityManagerInterface */
  55.     protected $em;
  56.     /** @var array */
  57.     private $data;
  58.     /**
  59.      * @throws SessionNotFoundException
  60.      */
  61.     public function __construct(RequestStack $stackMainService $serviceEntityManagerInterface $em)
  62.     {
  63.         $this->session $stack->getSession();
  64.         $this->service $service;
  65.         $this->em $em;
  66.         $this->data = [];
  67.         $this->session->start();
  68.         $this->cargosSistemaRepo $this->em->getRepository(CargosSistema::class);
  69.         $this->feeMinimoRepo $this->em->getRepository(FeeMinimo::class);
  70.         $this->feesRepo $this->em->getRepository(Fees::class);
  71.         $this->mantenimientoRepo $this->em->getRepository(Mantenimiento::class);
  72.         $this->mwaminRepo $this->em->getRepository(Mwamin::class);
  73.         $this->porcentajesRepo $this->em->getRepository(Porcentajes::class);
  74.         $this->precioCosteRepo $this->em->getRepository(PrecioCoste::class);
  75.         $this->precioPotenciaRepo $this->em->getRepository(PrecioPotencia::class);
  76.     }
  77.     /**
  78.      * Adds parameter to session and data array
  79.      * @param string $key
  80.      * @param $value
  81.      * @return void
  82.      */
  83.     protected function addData(string $key$value): void
  84.     {
  85.         $this->session->set($key$value);
  86.         $this->data[$key] = $value;
  87.     }
  88.     /**
  89.      * Adds request data to session and data array
  90.      * @param Request $request
  91.      * @return void
  92.      */
  93.     protected function addRequestData(Request $request): void
  94.     {
  95.         $data array_merge($request->request->all(), $request->query->all());
  96.         foreach ($data as $key => $value) {
  97.             $this->session->set($key$value);
  98.             $this->data[$key] = $value;
  99.         }
  100.     }
  101.     /**
  102.      * Returns session and data array, or custom param
  103.      * @param string|null $param
  104.      * @return mixed
  105.      */
  106.     protected function getData(string $param null)
  107.     {
  108.         $data array_merge(
  109.             $this->session->all(),
  110.             $this->data
  111.         );
  112.         if ($param) {
  113.             if (array_key_exists($param$data)) {
  114.                 return $data[$param];
  115.             }
  116.             return null;
  117.         }
  118.         return $data;
  119.     }
  120.     /**
  121.      * Resets session keeping basic and custom parameters
  122.      * @param array $customExclude
  123.      * @return void
  124.      */
  125.     protected function resetSession(array $customExclude = []): void
  126.     {
  127.         foreach ($this->session->all() as $key => $value) {
  128.             if (!in_array($keyself::BASIC_PARAMS)
  129.                 && !in_array($key$customExclude)) {
  130.                 $this->session->remove($key);
  131.             }
  132.         }
  133.     }
  134.     /**
  135.      * Returns if user is logged in or not
  136.      * @return bool
  137.      */
  138.     protected function checkUser(): bool
  139.     {
  140.         return $this->session->get('user')
  141.             && $this->session->get('password');
  142.     }
  143.     /**
  144.      * Check if basic data is defined
  145.      * @param $data
  146.      * @return bool
  147.      */
  148.     protected function checkBasics($data): bool
  149.     {
  150.         return array_key_exists('tipoFactura'$data);
  151.     }
  152. }