instances added god may help
This commit is contained in:
@@ -3,45 +3,52 @@
|
||||
namespace MintyPHP\Service\Access;
|
||||
|
||||
use MintyPHP\Repository\Access\RoleRepository;
|
||||
use MintyPHP\Service\Settings\SettingService;
|
||||
use MintyPHP\Service\Directory\DirectorySettingsGateway;
|
||||
use MintyPHP\Service\Settings\SettingServicesFactory;
|
||||
|
||||
class RoleService
|
||||
{
|
||||
public static function list(): array
|
||||
{
|
||||
return RoleRepository::list();
|
||||
public function __construct(
|
||||
private readonly ?RoleRepository $roleRepository = null,
|
||||
private readonly ?DirectorySettingsGateway $settingsGateway = null
|
||||
) {
|
||||
}
|
||||
|
||||
public static function listActive(): array
|
||||
public function list(): array
|
||||
{
|
||||
return RoleRepository::listActive();
|
||||
return $this->roleRepository()->list();
|
||||
}
|
||||
|
||||
public static function listPaged(array $options): array
|
||||
public function listActive(): array
|
||||
{
|
||||
return RoleRepository::listPaged($options);
|
||||
return $this->roleRepository()->listActive();
|
||||
}
|
||||
|
||||
public static function findByUuid(string $uuid): ?array
|
||||
public function listPaged(array $options): array
|
||||
{
|
||||
return RoleRepository::findByUuid($uuid);
|
||||
return $this->roleRepository()->listPaged($options);
|
||||
}
|
||||
|
||||
public static function findById(int $id): ?array
|
||||
public function findByUuid(string $uuid): ?array
|
||||
{
|
||||
return RoleRepository::find($id);
|
||||
return $this->roleRepository()->findByUuid($uuid);
|
||||
}
|
||||
|
||||
public static function createFromAdmin(array $input, int $currentUserId = 0): array
|
||||
public function findById(int $id): ?array
|
||||
{
|
||||
$form = self::sanitizeBase($input);
|
||||
$errors = self::validateBase($form, 0);
|
||||
return $this->roleRepository()->find($id);
|
||||
}
|
||||
|
||||
public function createFromAdmin(array $input, int $currentUserId = 0): array
|
||||
{
|
||||
$form = $this->sanitizeBase($input);
|
||||
$errors = $this->validateBase($form, 0);
|
||||
|
||||
if ($errors) {
|
||||
return ['ok' => false, 'errors' => $errors, 'form' => $form];
|
||||
}
|
||||
|
||||
$createdId = RoleRepository::create([
|
||||
$createdId = $this->roleRepository()->create([
|
||||
'description' => $form['description'],
|
||||
'code' => $form['code'] ?: null,
|
||||
'active' => $form['active'],
|
||||
@@ -52,24 +59,24 @@ class RoleService
|
||||
return ['ok' => false, 'errors' => [t('Role can not be created')], 'form' => $form];
|
||||
}
|
||||
|
||||
$createdRole = RoleRepository::find((int) $createdId);
|
||||
$createdRole = $this->roleRepository()->find((int) $createdId);
|
||||
$uuid = $createdRole['uuid'] ?? null;
|
||||
if (!empty($input['is_default'])) {
|
||||
SettingService::setDefaultRoleId((int) $createdId);
|
||||
$this->settingsGateway()->setDefaultRoleId((int) $createdId);
|
||||
}
|
||||
return ['ok' => true, 'form' => $form, 'uuid' => $uuid, 'id' => (int) $createdId];
|
||||
}
|
||||
|
||||
public static function updateFromAdmin(int $roleId, array $input, int $currentUserId = 0): array
|
||||
public function updateFromAdmin(int $roleId, array $input, int $currentUserId = 0): array
|
||||
{
|
||||
$form = self::sanitizeBase($input);
|
||||
$errors = self::validateBase($form, $roleId);
|
||||
$form = $this->sanitizeBase($input);
|
||||
$errors = $this->validateBase($form, $roleId);
|
||||
|
||||
if ($errors) {
|
||||
return ['ok' => false, 'errors' => $errors, 'form' => $form];
|
||||
}
|
||||
|
||||
$updated = RoleRepository::update($roleId, [
|
||||
$updated = $this->roleRepository()->update($roleId, [
|
||||
'description' => $form['description'],
|
||||
'code' => $form['code'] ?: null,
|
||||
'active' => $form['active'],
|
||||
@@ -83,14 +90,14 @@ class RoleService
|
||||
return ['ok' => true, 'form' => $form];
|
||||
}
|
||||
|
||||
public static function deleteByUuid(string $uuid): array
|
||||
public function deleteByUuid(string $uuid): array
|
||||
{
|
||||
$uuid = trim($uuid);
|
||||
if ($uuid === '') {
|
||||
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
|
||||
}
|
||||
|
||||
$role = RoleRepository::findByUuid($uuid);
|
||||
$role = $this->roleRepository()->findByUuid($uuid);
|
||||
if (!$role || !isset($role['id'])) {
|
||||
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
|
||||
}
|
||||
@@ -99,7 +106,7 @@ class RoleService
|
||||
return ['ok' => false, 'status' => 403, 'error' => 'admin_role_protected'];
|
||||
}
|
||||
|
||||
$deleted = RoleRepository::delete((int) $role['id']);
|
||||
$deleted = $this->roleRepository()->delete((int) $role['id']);
|
||||
if (!$deleted) {
|
||||
return ['ok' => false, 'status' => 500, 'error' => 'delete_failed'];
|
||||
}
|
||||
@@ -107,29 +114,29 @@ class RoleService
|
||||
return ['ok' => true, 'role' => $role];
|
||||
}
|
||||
|
||||
private static function sanitizeBase(array $input): array
|
||||
private function sanitizeBase(array $input): array
|
||||
{
|
||||
return [
|
||||
'description' => trim((string) ($input['description'] ?? '')),
|
||||
'code' => trim((string) ($input['code'] ?? '')),
|
||||
'active' => self::normalizeActive($input['active'] ?? 1),
|
||||
'active' => $this->normalizeActive($input['active'] ?? 1),
|
||||
];
|
||||
}
|
||||
|
||||
private static function validateBase(array $form, int $excludeId): array
|
||||
private function validateBase(array $form, int $excludeId): array
|
||||
{
|
||||
$errors = [];
|
||||
if ($form['description'] === '') {
|
||||
$errors[] = t('Description cannot be empty');
|
||||
}
|
||||
$code = $form['code'] ?? '';
|
||||
if ($code !== '' && RoleRepository::existsByCode($code, $excludeId)) {
|
||||
if ($code !== '' && $this->roleRepository()->existsByCode($code, $excludeId)) {
|
||||
$errors[] = t('Role code already exists');
|
||||
}
|
||||
return $errors;
|
||||
}
|
||||
|
||||
private static function normalizeActive($value): int
|
||||
private function normalizeActive($value): int
|
||||
{
|
||||
$value = strtolower(trim((string) $value));
|
||||
if (in_array($value, ['0', 'false', 'inactive'], true)) {
|
||||
@@ -137,4 +144,20 @@ class RoleService
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
private function roleRepository(): RoleRepository
|
||||
{
|
||||
return $this->roleRepository ?? new RoleRepository();
|
||||
}
|
||||
|
||||
private function settingsGateway(): DirectorySettingsGateway
|
||||
{
|
||||
if ($this->settingsGateway instanceof DirectorySettingsGateway) {
|
||||
return $this->settingsGateway;
|
||||
}
|
||||
|
||||
return new DirectorySettingsGateway(
|
||||
(new SettingServicesFactory())->createSettingGateway()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user