instances added god may help

This commit is contained in:
2026-02-23 12:58:19 +01:00
parent 25370a1a55
commit 99db252f60
290 changed files with 9858 additions and 4914 deletions

View File

@@ -3,34 +3,43 @@
namespace MintyPHP\Service\Org;
use MintyPHP\Repository\Org\DepartmentRepository;
use MintyPHP\Repository\Org\UserDepartmentRepository;
use MintyPHP\Service\Settings\SettingService;
use MintyPHP\Service\Tenant\TenantScopeService;
use MintyPHP\Service\Directory\DirectoryScopeGateway;
use MintyPHP\Service\Directory\DirectorySettingsGateway;
use MintyPHP\Service\Settings\SettingServicesFactory;
use MintyPHP\Service\User\UserServicesFactory;
class DepartmentService
{
public static function list(): array
{
return DepartmentRepository::list();
public function __construct(
private readonly ?DepartmentRepository $departmentRepository = null,
private readonly ?UserServicesFactory $userServicesFactory = null,
private readonly ?DirectorySettingsGateway $settingsGateway = null,
private readonly ?DirectoryScopeGateway $scopeGateway = null
) {
}
public static function listPaged(array $options): array
public function list(): array
{
return $this->departmentRepository()->list();
}
public function listPaged(array $options): array
{
if (!empty($options['tenantUserId'])) {
$tenantUserId = (int) $options['tenantUserId'];
if ($tenantUserId > 0 && TenantScopeService::hasGlobalAccess($tenantUserId)) {
if ($tenantUserId > 0 && $this->scopeGateway()->hasGlobalAccess($tenantUserId)) {
unset($options['tenantUserId'], $options['tenantIds']);
}
}
return DepartmentRepository::listPaged($options);
return $this->departmentRepository()->listPaged($options);
}
public static function listByTenantIds(array $tenantIds): array
public function listByTenantIds(array $tenantIds): array
{
return DepartmentRepository::listByTenantIds($tenantIds);
return $this->departmentRepository()->listByTenantIds($tenantIds);
}
public static function groupActiveByTenantIds(array $tenantIds): array
public function groupActiveByTenantIds(array $tenantIds): array
{
$tenantIds = array_values(array_unique(array_map('intval', $tenantIds)));
$tenantIds = array_values(array_filter($tenantIds, static fn ($id) => $id > 0));
@@ -38,7 +47,7 @@ class DepartmentService
return [];
}
$departments = self::listByTenantIds($tenantIds);
$departments = $this->listByTenantIds($tenantIds);
if (!$departments) {
return [];
}
@@ -63,15 +72,15 @@ class DepartmentService
return $grouped;
}
public static function listByIds(array $departmentIds): array
public function listByIds(array $departmentIds): array
{
return DepartmentRepository::listByIds($departmentIds);
return $this->departmentRepository()->listByIds($departmentIds);
}
public static function listForUserAssignments(array $tenantIds, array $selectedDepartmentIds): array
public function listForUserAssignments(array $tenantIds, array $selectedDepartmentIds): array
{
$departments = $tenantIds ? self::listByTenantIds($tenantIds) : self::list();
$extraDepartments = $selectedDepartmentIds ? self::listByIds($selectedDepartmentIds) : [];
$departments = $tenantIds ? $this->listByTenantIds($tenantIds) : $this->list();
$extraDepartments = $selectedDepartmentIds ? $this->listByIds($selectedDepartmentIds) : [];
if (!$extraDepartments) {
return $departments;
}
@@ -89,27 +98,27 @@ class DepartmentService
return array_values($departmentMap);
}
public static function findByUuid(string $uuid): ?array
public function findByUuid(string $uuid): ?array
{
return DepartmentRepository::findByUuid($uuid);
return $this->departmentRepository()->findByUuid($uuid);
}
public static function findById(int $id): ?array
public function findById(int $id): ?array
{
return DepartmentRepository::find($id);
return $this->departmentRepository()->find($id);
}
public static function createFromAdmin(array $input, int $currentUserId = 0): array
public function createFromAdmin(array $input, int $currentUserId = 0): array
{
$form = self::sanitizeBase($input);
$errors = self::validateBase($form);
$warnings = self::warningsForCode($form, 0);
$form = $this->sanitizeBase($input);
$errors = $this->validateBase($form);
$warnings = $this->warningsForCode($form, 0);
if ($errors) {
return ['ok' => false, 'errors' => $errors, 'warnings' => $warnings, 'form' => $form];
}
$createdId = DepartmentRepository::create([
$createdId = $this->departmentRepository()->create([
'description' => $form['description'],
'tenant_id' => $form['tenant_id'],
'code' => $form['code'] ?: null,
@@ -122,25 +131,25 @@ class DepartmentService
return ['ok' => false, 'errors' => [t('Department can not be created')], 'form' => $form];
}
$createdDepartment = DepartmentRepository::find((int) $createdId);
$createdDepartment = $this->departmentRepository()->find((int) $createdId);
$uuid = $createdDepartment['uuid'] ?? null;
if (!empty($input['is_default'])) {
SettingService::setDefaultDepartmentId((int) $createdId);
$this->settingsGateway()->setDefaultDepartmentId((int) $createdId);
}
return ['ok' => true, 'form' => $form, 'warnings' => $warnings, 'uuid' => $uuid, 'id' => (int) $createdId];
}
public static function updateFromAdmin(int $departmentId, array $input, int $currentUserId = 0): array
public function updateFromAdmin(int $departmentId, array $input, int $currentUserId = 0): array
{
$form = self::sanitizeBase($input);
$errors = self::validateBase($form);
$warnings = self::warningsForCode($form, $departmentId);
$form = $this->sanitizeBase($input);
$errors = $this->validateBase($form);
$warnings = $this->warningsForCode($form, $departmentId);
if ($errors) {
return ['ok' => false, 'errors' => $errors, 'warnings' => $warnings, 'form' => $form];
}
$updated = DepartmentRepository::update($departmentId, [
$updated = $this->departmentRepository()->update($departmentId, [
'description' => $form['description'],
'tenant_id' => $form['tenant_id'],
'code' => $form['code'] ?: null,
@@ -156,42 +165,44 @@ class DepartmentService
return ['ok' => true, 'form' => $form, 'warnings' => $warnings];
}
public static function syncTenant(int $departmentId, int $tenantId): int
public function syncTenant(int $departmentId, int $tenantId): int
{
$updated = DepartmentRepository::setTenant($departmentId, $tenantId);
$updated = $this->departmentRepository()->setTenant($departmentId, $tenantId);
if (!$updated) {
return -1;
}
return self::cleanupUserAssignments($departmentId);
return $this->cleanupUserAssignments($departmentId);
}
public static function syncTenants(int $departmentId, array $tenantIds): int
public function syncTenants(int $departmentId, array $tenantIds): int
{
$tenantId = (int) ($tenantIds[0] ?? 0);
if ($tenantId <= 0) {
return -1;
}
return self::syncTenant($departmentId, $tenantId);
return $this->syncTenant($departmentId, $tenantId);
}
public static function cleanupUserAssignments(int $departmentId): int
public function cleanupUserAssignments(int $departmentId): int
{
return UserDepartmentRepository::removeInvalidForDepartment($departmentId);
return $this->userServicesFactory()
->createUserDepartmentRepository()
->removeInvalidForDepartment($departmentId);
}
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'];
}
$department = DepartmentRepository::findByUuid($uuid);
$department = $this->departmentRepository()->findByUuid($uuid);
if (!$department || !isset($department['id'])) {
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
}
$deleted = DepartmentRepository::delete((int) $department['id']);
$deleted = $this->departmentRepository()->delete((int) $department['id']);
if (!$deleted) {
return ['ok' => false, 'status' => 500, 'error' => 'delete_failed'];
}
@@ -199,18 +210,18 @@ class DepartmentService
return ['ok' => true, 'department' => $department];
}
private static function sanitizeBase(array $input): array
private function sanitizeBase(array $input): array
{
return [
'description' => trim((string) ($input['description'] ?? '')),
'tenant_id' => (int) ($input['tenant_id'] ?? 0),
'code' => trim((string) ($input['code'] ?? '')),
'cost_center' => trim((string) ($input['cost_center'] ?? '')),
'active' => self::normalizeActive($input['active'] ?? 1),
'active' => $this->normalizeActive($input['active'] ?? 1),
];
}
private static function validateBase(array $form): array
private function validateBase(array $form): array
{
$errors = [];
if ($form['description'] === '') {
@@ -222,17 +233,17 @@ class DepartmentService
return $errors;
}
private static function warningsForCode(array $form, int $excludeId): array
private function warningsForCode(array $form, int $excludeId): array
{
$warnings = [];
$code = $form['code'] ?? '';
if ($code !== '' && DepartmentRepository::existsByCode($code, $excludeId)) {
if ($code !== '' && $this->departmentRepository()->existsByCode($code, $excludeId)) {
$warnings[] = t('Department code already exists');
}
return $warnings;
}
private static function normalizeActive($value): int
private function normalizeActive($value): int
{
$value = strtolower(trim((string) $value));
if (in_array($value, ['0', 'false', 'inactive'], true)) {
@@ -240,4 +251,30 @@ class DepartmentService
}
return 1;
}
private function departmentRepository(): DepartmentRepository
{
return $this->departmentRepository ?? new DepartmentRepository();
}
private function userServicesFactory(): UserServicesFactory
{
return $this->userServicesFactory ?? new UserServicesFactory();
}
private function settingsGateway(): DirectorySettingsGateway
{
if ($this->settingsGateway instanceof DirectorySettingsGateway) {
return $this->settingsGateway;
}
return new DirectorySettingsGateway(
(new SettingServicesFactory())->createSettingGateway()
);
}
private function scopeGateway(): DirectoryScopeGateway
{
return $this->scopeGateway ?? new DirectoryScopeGateway();
}
}