This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\DTO\AppSetupConfig;
|
||||
use App\Entity\ConfigVariable;
|
||||
use App\Entity\User;
|
||||
use App\Enum\ConfigSectionType;
|
||||
use App\Enum\UserRoleEnum;
|
||||
use App\Repository\ConfigVariableRepository;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
|
||||
/**
|
||||
* Service for setup proccess.
|
||||
*/
|
||||
class AppSetupService
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
private LoggerInterface $logger,
|
||||
private EntityManagerInterface $entityManager,
|
||||
private ConfigVariableRepository $repoVariables,
|
||||
private UserPasswordHasherInterface $passwordHasher,
|
||||
private Security $security
|
||||
) {}
|
||||
|
||||
public function InitialSetup(AppSetupConfig $configData): bool
|
||||
{
|
||||
$this->entityManager->beginTransaction();
|
||||
$user = new User();
|
||||
|
||||
try {
|
||||
//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);
|
||||
|
||||
|
||||
//Create user Admin.
|
||||
$this->logger->info("SomniaRooms Setup - Adding super-admin user...");
|
||||
$user->setUsername($configData->getAdminUser());
|
||||
$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,
|
||||
$plaintextPassword
|
||||
);
|
||||
$user->setPassword($hashedPassword);
|
||||
|
||||
$this->entityManager->persist($user);
|
||||
$this->entityManager->flush();
|
||||
$this->entityManager->commit();
|
||||
} catch (\Throwable $th) {
|
||||
$this->entityManager->rollback();
|
||||
throw $th;
|
||||
}
|
||||
|
||||
//If init was sucessfull, log admin user.
|
||||
$this->security->login($user);
|
||||
|
||||
$this->logger->info("SomniaRooms Setup completed!");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\DTO\AppSetupConfig;
|
||||
use App\DTO\ExampleDataConfig;
|
||||
use App\Entity\ConfigVariable;
|
||||
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;
|
||||
use App\Repository\RoomAvailabilityRepository;
|
||||
use App\Repository\RoomCategoryRepository;
|
||||
use App\Repository\RoomFareRepository;
|
||||
use DateTime;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Service for setup proccess.
|
||||
*/
|
||||
class ExampleDataService
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
private LoggerInterface $logger,
|
||||
private EntityManagerInterface $entityManager,
|
||||
private RoomCategoryRepository $roomCategoryRepository,
|
||||
private RoomAvailabilityRepository $roomAvailabilityRepository,
|
||||
private FareTableRepository $fareTableRepository,
|
||||
private RoomFareRepository $roomFareRepository,
|
||||
) {}
|
||||
|
||||
public function AddExampleData(ExampleDataConfig $config): string
|
||||
{
|
||||
$genMode = $config->getGenMode();
|
||||
if ($genMode == ExampleDataConfig::GENMODE_EXAMPLE) {
|
||||
return "'Example' generation mode is not supported yet!";
|
||||
}
|
||||
|
||||
$this->entityManager->beginTransaction();
|
||||
|
||||
try {
|
||||
if ($config->getGenRoomCategories()) {
|
||||
$this->GenerateRoomCategories();
|
||||
}
|
||||
if ($config->getGenAvailability()) {
|
||||
$this->GenerateAvailability($config);
|
||||
}
|
||||
if ($config->getGenFaretables()) {
|
||||
$this->GenerateFares($config);
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
$this->entityManager->commit();
|
||||
} catch (\Throwable $th) {
|
||||
$this->entityManager->rollback();
|
||||
throw $th;
|
||||
}
|
||||
|
||||
return "OK";
|
||||
}
|
||||
|
||||
private function GenerateRoomCategories(): bool
|
||||
{
|
||||
//TODOME.
|
||||
return true;
|
||||
}
|
||||
|
||||
private function GenerateAvailability(ExampleDataConfig $config): bool
|
||||
{
|
||||
$roomCategoryList = $this->roomCategoryRepository->findAll();
|
||||
foreach ($roomCategoryList as $room) {
|
||||
//TODOME: Make amounts configurable.
|
||||
$amount = random_int(0, 7);
|
||||
$this->roomAvailabilityRepository->bulkEditRoomAvailabilityForCategoryOnPeriod(
|
||||
$room,
|
||||
$config->getStartDate(),
|
||||
$config->getEndDate(),
|
||||
$amount,
|
||||
useTransactions: false
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function GenerateFares(ExampleDataConfig $config): bool
|
||||
{
|
||||
$generationTime = new DateTime();
|
||||
$strGenTime = $generationTime->format('d-m-Y H:i:s');
|
||||
|
||||
$roomCategoryList = $this->roomCategoryRepository->findAll();
|
||||
foreach ($roomCategoryList as $room) {
|
||||
$list = $this->fareTableRepository->findFareTable($room->getId(), $config->getStartDate(), $config->getEndDate());
|
||||
if (count($list) > 0) {
|
||||
//TODOME: Log info.
|
||||
continue;
|
||||
}
|
||||
|
||||
$fareTable = new FareTable();
|
||||
$fareTable->setStartDate($config->getStartDate());
|
||||
$fareTable->setEndDate($config->getEndDate());
|
||||
$fareTable->setRoomCategory($room);
|
||||
$fareTable->setComment("Autogenerated " . $strGenTime);
|
||||
|
||||
$this->entityManager->persist($fareTable);
|
||||
|
||||
for ($i = 1; $i < $room->getMaxGuestNum(); $i++) {
|
||||
$roomFare = new RoomFare();
|
||||
$roomFare->setGuestNumber($i);
|
||||
$roomFare->setFareTable($fareTable);
|
||||
|
||||
//TODOME: Make amounts configurable.
|
||||
// Random price per person.
|
||||
$amount = random_int(10, 25) * $i * 100;
|
||||
$roomFare->setFareAmount($amount);
|
||||
|
||||
$this->entityManager->persist($roomFare);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user