Config Variables - Improve config variable system and initialize variables during setup.

This commit is contained in:
2024-12-12 15:38:28 +01:00
parent 28afb11ac4
commit dcd0b5f064
10 changed files with 175 additions and 58 deletions
+14 -12
View File
@@ -2,10 +2,11 @@
namespace App\Service;
use App\Consts\ConfigVariableConsts;
use App\DTO\AppSetupConfig;
use App\Entity\ConfigVariable;
use App\Entity\User;
use App\Enum\ConfigSectionType;
use App\Enum\ConfigVariableSectionEnum;
use App\Enum\UserRoleEnum;
use App\Repository\ConfigVariableRepository;
@@ -23,7 +24,7 @@ class AppSetupService
public function __construct(
private LoggerInterface $logger,
private EntityManagerInterface $entityManager,
private ConfigVariableRepository $repoVariables,
private ConfigVariableService $configVars,
private UserPasswordHasherInterface $passwordHasher,
private Security $security
) {}
@@ -37,15 +38,18 @@ class AppSetupService
//Create system variables.
$this->logger->info("SomniaRooms Setup - Creating system variables...");
$var_setup = $this->repoVariables->find("SETUP_STATUS");
if (!$var_setup) {
$var_setup = new ConfigVariable();
$var_setup->setKey("SETUP_STATUS");
}
$var_setup->setValue("1");
$var_setup->setSection(ConfigSectionType::Internal);
$this->entityManager->persist($var_setup);
//Not neccesary anymore. CONFIG_INTERNAL_SETUP will be initialized with all vars.
// $var_setup_key = ConfigVariableConsts::getKeyFromConstant(ConfigVariableConsts::CONFIG_INTERNAL_SETUP);
// $var_setup = $this->configVars->getVariable($var_setup_key);
// if (!$var_setup) {
// $var_setup = new ConfigVariable();
// $var_setup->setKey($var_setup_key);
// $this->entityManager->persist($var_setup);
// }
// $var_setup->setValue("1");
// $var_setup->setSection(ConfigVariableSectionEnum::Internal);
$this->configVars->InitVariablesOnSetup();
//Create user Admin.
$this->logger->info("SomniaRooms Setup - Adding super-admin user...");
@@ -53,8 +57,6 @@ class AppSetupService
$user->setRoles([UserRoleEnum::ROLE_USER->value, UserRoleEnum::ROLE_ADMIN->value, UserRoleEnum::ROLE_SUPERADMIN->value]);
$plaintextPassword = $configData->getAdminPass();
// hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $this->passwordHasher->hashPassword(
$user,
+38 -15
View File
@@ -2,10 +2,14 @@
namespace App\Service;
use App\Consts\ConfigVariableConsts;
use App\Entity\ConfigVariable;
use App\Enum\ConfigVariableSectionEnum;
use App\Repository\ConfigVariableRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
/**
* Service to manage config variables.
@@ -14,33 +18,52 @@ class ConfigVariableService
{
public function __construct(
private ConfigVariableRepository $repoVariables
) {}
private EntityManagerInterface $entityManager,
private ConfigVariableRepository $repoVariables,
private LoggerInterface $logger,
) {}
public function getVariable(string $key) {
$var = $this->repoVariables->findVariableByKey($key);
public function getVariable(string $key): ConfigVariable|null
{
return $this->repoVariables->findVariableByKey($key);
}
/**
* Create and init all variables.
* To be used during Setup process.
*/
public function InitVariablesOnSetup(): void {
public function InitVariablesOnSetup(): void
{
//Get a list of variables to check if those already exists.
//$this->entityManager->beginTransaction();
$varListDb = $this->repoVariables->getVariableSet();
$varListPHP = ConfigVariableConsts::GetAllDefinedConfigVariablesList();
$entityManager = $this->repoVariables->getEntityManager();
$entityManager->beginTransaction();
foreach ($varListPHP as $varKey => $varValue) {
if ($varListDb->contains($varKey)) {
$this->logger->info("Var $varKey already created. Skipped!", [$varKey, $varValue]);
continue;
}
$this->AddConfigVariable($varKey, $varValue[1], $varValue[0]);
}
try {
}
catch (\Throwable $th) {
$entityManager->rollback();
} catch (\Throwable $th) {
$this->entityManager->rollback();
throw $th;
}
}
private function AddConfigVariable(string $key, string $value, string $section): void {
}
public function AddConfigVariable(string $key, string $value, ConfigVariableSectionEnum $section): void
{
$var = new ConfigVariable();
$var->setKey($key);
$var->setValue($value);
$var->setSection($section);
}
$this->entityManager->persist($var);
$this->logger->info("Variable added to DB", ["key" => $key, "value" => $value, "section" => $section]);
}
}
-1
View File
@@ -9,7 +9,6 @@ use App\Entity\FareTable;
use App\Entity\RoomCategory;
use App\Entity\RoomFare;
use App\Entity\User;
use App\Enum\ConfigSectionType;
use App\Enum\UserRoleEnum;
use App\Repository\ConfigVariableRepository;
use App\Repository\FareTableRepository;