instances added god may help
This commit is contained in:
@@ -4,42 +4,50 @@ namespace MintyPHP\Service\Tenant;
|
||||
|
||||
use MintyPHP\Repository\Org\DepartmentRepository;
|
||||
use MintyPHP\Repository\Tenant\TenantRepository;
|
||||
use MintyPHP\Service\Settings\SettingService;
|
||||
use MintyPHP\Service\Directory\DirectorySettingsGateway;
|
||||
use MintyPHP\Service\Settings\SettingServicesFactory;
|
||||
|
||||
class TenantService
|
||||
{
|
||||
public static function list(): array
|
||||
{
|
||||
return TenantRepository::list();
|
||||
public function __construct(
|
||||
private readonly ?TenantRepository $tenantRepository = null,
|
||||
private readonly ?DepartmentRepository $departmentRepository = null,
|
||||
private readonly ?DirectorySettingsGateway $settingsGateway = null
|
||||
) {
|
||||
}
|
||||
|
||||
public static function listPaged(array $options): array
|
||||
public function list(): array
|
||||
{
|
||||
return TenantRepository::listPaged($options);
|
||||
return $this->tenantRepository()->list();
|
||||
}
|
||||
|
||||
public static function findByUuid(string $uuid): ?array
|
||||
public function listPaged(array $options): array
|
||||
{
|
||||
return TenantRepository::findByUuid($uuid);
|
||||
return $this->tenantRepository()->listPaged($options);
|
||||
}
|
||||
|
||||
public static function findById(int $id): ?array
|
||||
public function findByUuid(string $uuid): ?array
|
||||
{
|
||||
return TenantRepository::find($id);
|
||||
return $this->tenantRepository()->findByUuid($uuid);
|
||||
}
|
||||
|
||||
public static function createFromAdmin(array $input, int $currentUserId = 0): array
|
||||
public function findById(int $id): ?array
|
||||
{
|
||||
$form = self::sanitizeBase($input);
|
||||
$form['status'] = self::normalizeStatus($form['status'] ?? '');
|
||||
$errors = self::validateBase($form);
|
||||
return $this->tenantRepository()->find($id);
|
||||
}
|
||||
|
||||
public function createFromAdmin(array $input, int $currentUserId = 0): array
|
||||
{
|
||||
$form = $this->sanitizeBase($input);
|
||||
$form['status'] = $this->normalizeStatus($form['status'] ?? '');
|
||||
$errors = $this->validateBase($form);
|
||||
|
||||
if ($errors) {
|
||||
return ['ok' => false, 'errors' => $errors, 'form' => $form];
|
||||
}
|
||||
|
||||
$statusChangedAt = gmdate('Y-m-d H:i:s');
|
||||
$createdId = TenantRepository::create([
|
||||
$createdId = $this->tenantRepository()->create([
|
||||
'description' => $form['description'],
|
||||
'address' => $form['address'],
|
||||
'postal_code' => $form['postal_code'],
|
||||
@@ -72,25 +80,25 @@ class TenantService
|
||||
return ['ok' => false, 'errors' => [t('Tenant can not be created')], 'form' => $form];
|
||||
}
|
||||
|
||||
$createdTenant = TenantRepository::find((int) $createdId);
|
||||
$createdTenant = $this->tenantRepository()->find((int) $createdId);
|
||||
$uuid = $createdTenant['uuid'] ?? null;
|
||||
if (!empty($input['is_default'])) {
|
||||
SettingService::setDefaultTenantId((int) $createdId);
|
||||
$this->settingsGateway()->setDefaultTenantId((int) $createdId);
|
||||
}
|
||||
return ['ok' => true, 'form' => $form, 'uuid' => $uuid, 'id' => (int) $createdId];
|
||||
}
|
||||
|
||||
public static function updateFromAdmin(int $tenantId, array $input, int $currentUserId = 0): array
|
||||
public function updateFromAdmin(int $tenantId, array $input, int $currentUserId = 0): array
|
||||
{
|
||||
$form = self::sanitizeBase($input);
|
||||
$form['status'] = self::normalizeStatus($form['status'] ?? '');
|
||||
$errors = self::validateBase($form);
|
||||
$form = $this->sanitizeBase($input);
|
||||
$form['status'] = $this->normalizeStatus($form['status'] ?? '');
|
||||
$errors = $this->validateBase($form);
|
||||
|
||||
if ($errors) {
|
||||
return ['ok' => false, 'errors' => $errors, 'form' => $form];
|
||||
}
|
||||
|
||||
$existing = TenantRepository::find($tenantId);
|
||||
$existing = $this->tenantRepository()->find($tenantId);
|
||||
$statusChanged = false;
|
||||
if ($existing && isset($existing['status'])) {
|
||||
$statusChanged = (string) $existing['status'] !== $form['status'];
|
||||
@@ -127,7 +135,7 @@ class TenantService
|
||||
$updateData['status_changed_by'] = $currentUserId > 0 ? $currentUserId : null;
|
||||
}
|
||||
|
||||
$updated = TenantRepository::update($tenantId, $updateData);
|
||||
$updated = $this->tenantRepository()->update($tenantId, $updateData);
|
||||
|
||||
if (!$updated) {
|
||||
return ['ok' => false, 'errors' => [t('Tenant can not be updated')], 'form' => $form];
|
||||
@@ -136,24 +144,24 @@ class TenantService
|
||||
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'];
|
||||
}
|
||||
|
||||
$tenant = TenantRepository::findByUuid($uuid);
|
||||
$tenant = $this->tenantRepository()->findByUuid($uuid);
|
||||
if (!$tenant || !isset($tenant['id'])) {
|
||||
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
|
||||
}
|
||||
|
||||
$tenantId = (int) $tenant['id'];
|
||||
if (DepartmentRepository::countByTenantId($tenantId) > 0) {
|
||||
if ($this->departmentRepository()->countByTenantId($tenantId) > 0) {
|
||||
return ['ok' => false, 'status' => 409, 'error' => 'tenant_has_departments'];
|
||||
}
|
||||
|
||||
$deleted = TenantRepository::delete($tenantId);
|
||||
$deleted = $this->tenantRepository()->delete($tenantId);
|
||||
if (!$deleted) {
|
||||
return ['ok' => false, 'status' => 500, 'error' => 'delete_failed'];
|
||||
}
|
||||
@@ -161,7 +169,7 @@ class TenantService
|
||||
return ['ok' => true, 'tenant' => $tenant];
|
||||
}
|
||||
|
||||
private static function sanitizeBase(array $input): array
|
||||
private function sanitizeBase(array $input): array
|
||||
{
|
||||
return [
|
||||
'description' => trim((string) ($input['description'] ?? '')),
|
||||
@@ -189,7 +197,7 @@ class TenantService
|
||||
];
|
||||
}
|
||||
|
||||
private static function validateBase(array $form): array
|
||||
private function validateBase(array $form): array
|
||||
{
|
||||
$errors = [];
|
||||
if ($form['description'] === '') {
|
||||
@@ -215,7 +223,7 @@ class TenantService
|
||||
return $errors;
|
||||
}
|
||||
|
||||
private static function normalizeStatus(string $status): string
|
||||
private function normalizeStatus(string $status): string
|
||||
{
|
||||
$status = strtolower(trim($status));
|
||||
if (!in_array($status, ['active', 'inactive'], true)) {
|
||||
@@ -223,4 +231,25 @@ class TenantService
|
||||
}
|
||||
return $status;
|
||||
}
|
||||
|
||||
private function tenantRepository(): TenantRepository
|
||||
{
|
||||
return $this->tenantRepository ?? new TenantRepository();
|
||||
}
|
||||
|
||||
private function departmentRepository(): DepartmentRepository
|
||||
{
|
||||
return $this->departmentRepository ?? new DepartmentRepository();
|
||||
}
|
||||
|
||||
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