<?php
namespace App\Controller;
use App\Entity\CargosSistema;
use App\Entity\FeeMinimo;
use App\Entity\Fees;
use App\Entity\Mantenimiento;
use App\Entity\Mwamin;
use App\Entity\Porcentajes;
use App\Entity\PrecioCoste;
use App\Entity\PrecioPotencia;
use App\Repository\CargosSistemaRepository;
use App\Repository\FeeMinimoRepository;
use App\Repository\FeesRepository;
use App\Repository\MantenimientoRepository;
use App\Repository\MwaminRepository;
use App\Repository\PorcentajesRepository;
use App\Repository\PrecioCosteRepository;
use App\Repository\PrecioPotenciaRepository;
use App\Service\MainService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class BaseController extends AbstractController
{
// Tipo Factura
public const TIPO_HOGAR = 'tipohogar';
public const TIPO_EMPRESA = 'tipoempresa';
// Basic parameters
const BASIC_PARAMS = ['id', 'idClienteAplicacion', 'empDato', 'user', 'password'];
// Repositories
/** @var CargosSistemaRepository */
protected $cargosSistemaRepo;
/** @var FeeMinimoRepository */
protected $feeMinimoRepo;
/** @var FeesRepository */
protected $feesRepo;
/** @var MantenimientoRepository */
protected $mantenimientoRepo;
/** @var MwaminRepository */
protected $mwaminRepo;
/** @var PorcentajesRepository */
protected $porcentajesRepo;
/** @var PrecioCosteRepository */
protected $precioCosteRepo;
/** @var PrecioPotenciaRepository */
protected $precioPotenciaRepo;
/** @var SessionInterface */
protected $session;
/** @var MainService */
protected $service;
/** @var EntityManagerInterface */
protected $em;
/** @var array */
private $data;
/**
* @throws SessionNotFoundException
*/
public function __construct(RequestStack $stack, MainService $service, EntityManagerInterface $em)
{
$this->session = $stack->getSession();
$this->service = $service;
$this->em = $em;
$this->data = [];
$this->session->start();
$this->cargosSistemaRepo = $this->em->getRepository(CargosSistema::class);
$this->feeMinimoRepo = $this->em->getRepository(FeeMinimo::class);
$this->feesRepo = $this->em->getRepository(Fees::class);
$this->mantenimientoRepo = $this->em->getRepository(Mantenimiento::class);
$this->mwaminRepo = $this->em->getRepository(Mwamin::class);
$this->porcentajesRepo = $this->em->getRepository(Porcentajes::class);
$this->precioCosteRepo = $this->em->getRepository(PrecioCoste::class);
$this->precioPotenciaRepo = $this->em->getRepository(PrecioPotencia::class);
}
/**
* Adds parameter to session and data array
* @param string $key
* @param $value
* @return void
*/
protected function addData(string $key, $value): void
{
$this->session->set($key, $value);
$this->data[$key] = $value;
}
/**
* Adds request data to session and data array
* @param Request $request
* @return void
*/
protected function addRequestData(Request $request): void
{
$data = array_merge($request->request->all(), $request->query->all());
foreach ($data as $key => $value) {
$this->session->set($key, $value);
$this->data[$key] = $value;
}
}
/**
* Returns session and data array, or custom param
* @param string|null $param
* @return mixed
*/
protected function getData(string $param = null)
{
$data = array_merge(
$this->session->all(),
$this->data
);
if ($param) {
if (array_key_exists($param, $data)) {
return $data[$param];
}
return null;
}
return $data;
}
/**
* Resets session keeping basic and custom parameters
* @param array $customExclude
* @return void
*/
protected function resetSession(array $customExclude = []): void
{
foreach ($this->session->all() as $key => $value) {
if (!in_array($key, self::BASIC_PARAMS)
&& !in_array($key, $customExclude)) {
$this->session->remove($key);
}
}
}
/**
* Returns if user is logged in or not
* @return bool
*/
protected function checkUser(): bool
{
return $this->session->get('user')
&& $this->session->get('password');
}
/**
* Check if basic data is defined
* @param $data
* @return bool
*/
protected function checkBasics($data): bool
{
return array_key_exists('tipoFactura', $data);
}
}