<?php
namespace App\Tools;
use App\Entity\ParametreSociete;
use App\Repository\ParametreSocieteRepository;
use Exception;
use Symfony\Component\HttpFoundation\RequestStack;
class ConfigApp
{
private string $_hostname;
private string $_subdomain = '';
public ?ParametreSociete $parametre;
public function __construct(string $hostname, ParametreSocieteRepository $parametreSocieteRepository, RequestStack $requestStack)
{
$this->_hostname = $hostname;
if($this->isValideDomaine()){
try {
$this->parametre = $parametreSocieteRepository->findOneBySubdomain($this->_subdomain);
if(null!==$this->parametre){
$this->setParametreSociete();
$requestStack->getSession()->set('societe', $this->parametre);
}
} catch (Exception $e) {
dd($e);
}
}
}
/**
* @return bool
*/
public function isValideDomaine(): bool
{
// valide si environnement de dev local
if('localhost' === $this->_hostname){
$this->_subdomain = 'dev';
return true;
}
// controle si la chaine de caractère passée est bien une URL valide
if (!filter_var($this->_hostname, FILTER_VALIDATE_URL)) return false;
// init du sous-domaine qui correspond à la config qui va être utilisée
$a = explode('.', $this->_hostname);
if (3 === count($a)) {
$this->_subdomain = str_replace(array('http://','https://'), '', $a[0]);
return true;
}
return false;
}
/**
* Sélection des champs pouvant être enregistrés en session
* @return void
*/
private function setParametreSociete(): void
{
$parametre = new ParametreSociete();
$parametre->setIsActive( $this->parametre->getIsActive() );
$parametre->setBase( $this->parametre->getBase() );
$parametre->setPrefix( $this->parametre->getPrefix() );
$parametre->setLibSociete( $this->parametre->getLibSociete() );
$parametre->setLogoPath( $this->parametre->getLogoPath() );
$parametre->setBackgroundPath( $this->parametre->getBackgroundPath() );
$parametre->setThemeColor( $this->parametre->getThemeColor() );
$parametre->setData( $this->parametre->getData() );
$this->parametre = $parametre;
}
}