refactor(arch): enforce gateway compliance and remove service-wrapping gateways
This commit is contained in:
@@ -3,12 +3,11 @@
|
||||
namespace MintyPHP\Service\Import;
|
||||
|
||||
use MintyPHP\Http\SessionStoreInterface;
|
||||
use MintyPHP\Service\Access\PermissionGateway;
|
||||
use MintyPHP\Service\Access\PermissionService;
|
||||
use MintyPHP\Service\Audit\ImportAuditService;
|
||||
use MintyPHP\Service\Import\Profile\ImportProfileInterface;
|
||||
use MintyPHP\Service\Settings\SettingsDefaultsGateway;
|
||||
use MintyPHP\Service\User\UserScopeGateway;
|
||||
use MintyPHP\Service\Tenant\TenantScopeService;
|
||||
|
||||
class ImportService
|
||||
{
|
||||
@@ -28,9 +27,9 @@ class ImportService
|
||||
private readonly ImportAuditService $importAuditService,
|
||||
private readonly ImportStateStoreService $importStateStoreService,
|
||||
private readonly array $profiles,
|
||||
private readonly PermissionGateway $permissionGateway,
|
||||
private readonly PermissionService $permissionService,
|
||||
private readonly SettingsDefaultsGateway $settingsDefaultsGateway,
|
||||
private readonly UserScopeGateway $userScopeGateway,
|
||||
private readonly TenantScopeService $userScopeGateway,
|
||||
private readonly SessionStoreInterface $sessionStore
|
||||
) {
|
||||
}
|
||||
@@ -490,7 +489,7 @@ class ImportService
|
||||
|
||||
private function canImportAssignments(int $userId): bool
|
||||
{
|
||||
return $this->permissionGateway->userHas($userId, PermissionService::USERS_IMPORT_ASSIGNMENTS);
|
||||
return $this->permissionService->userHas($userId, PermissionService::USERS_IMPORT_ASSIGNMENTS);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace MintyPHP\Service\Import;
|
||||
use MintyPHP\Http\SessionStoreInterface;
|
||||
use MintyPHP\Repository\Audit\ImportAuditRunRepositoryInterface;
|
||||
use MintyPHP\Service\Access\AccessServicesFactory;
|
||||
use MintyPHP\Service\Access\PermissionGateway;
|
||||
use MintyPHP\Service\Access\PermissionService;
|
||||
use MintyPHP\Service\Audit\ImportAuditService;
|
||||
use MintyPHP\Service\Directory\DirectoryServicesFactory;
|
||||
use MintyPHP\Service\Import\Profile\DepartmentImportGateway;
|
||||
@@ -14,6 +14,7 @@ use MintyPHP\Service\Import\Profile\UserImportGateway;
|
||||
use MintyPHP\Service\Import\Profile\UserImportProfile;
|
||||
use MintyPHP\Service\Settings\SettingsDefaultsGateway;
|
||||
use MintyPHP\Service\Settings\SettingServicesFactory;
|
||||
use MintyPHP\Service\Tenant\TenantScopeService;
|
||||
use MintyPHP\Service\User\UserServicesFactory;
|
||||
|
||||
class ImportServicesFactory
|
||||
@@ -22,7 +23,7 @@ class ImportServicesFactory
|
||||
private ?ImportTempFileService $importTempFileService = null;
|
||||
private ?ImportStateStoreService $importStateStoreService = null;
|
||||
private ?ImportAuditService $importAuditService = null;
|
||||
private ?PermissionGateway $permissionGateway = null;
|
||||
private ?PermissionService $permissionService = null;
|
||||
private ?SettingsDefaultsGateway $settingsDefaultsGateway = null;
|
||||
private ?ImportService $importService = null;
|
||||
|
||||
@@ -32,6 +33,7 @@ class ImportServicesFactory
|
||||
private readonly SettingServicesFactory $settingServicesFactory,
|
||||
private readonly DirectoryServicesFactory $directoryServicesFactory,
|
||||
private readonly ImportRepositoryFactory $importRepositoryFactory,
|
||||
private readonly TenantScopeService $tenantScopeService,
|
||||
private readonly SessionStoreInterface $sessionStore
|
||||
) {
|
||||
}
|
||||
@@ -43,18 +45,22 @@ class ImportServicesFactory
|
||||
}
|
||||
|
||||
$profiles = [
|
||||
'users' => new UserImportProfile(new UserImportGateway(
|
||||
$this->userServicesFactory->createUserReadRepository(),
|
||||
$this->userServicesFactory->createUserAccountService(),
|
||||
$this->directoryServicesFactory->createTenantRepository(),
|
||||
$this->directoryServicesFactory->createRoleRepository(),
|
||||
$this->directoryServicesFactory->createDepartmentRepository(),
|
||||
)),
|
||||
'departments' => new DepartmentImportProfile(new DepartmentImportGateway(
|
||||
$this->directoryServicesFactory->createDepartmentRepository(),
|
||||
$this->directoryServicesFactory->createTenantRepository(),
|
||||
'users' => new UserImportProfile(
|
||||
new UserImportGateway(
|
||||
$this->userServicesFactory->createUserReadRepository(),
|
||||
$this->directoryServicesFactory->createTenantRepository(),
|
||||
$this->directoryServicesFactory->createRoleRepository(),
|
||||
$this->directoryServicesFactory->createDepartmentRepository(),
|
||||
),
|
||||
$this->userServicesFactory->createUserAccountService()
|
||||
),
|
||||
'departments' => new DepartmentImportProfile(
|
||||
new DepartmentImportGateway(
|
||||
$this->directoryServicesFactory->createDepartmentRepository(),
|
||||
$this->directoryServicesFactory->createTenantRepository()
|
||||
),
|
||||
$this->directoryServicesFactory->createDepartmentService()
|
||||
)),
|
||||
),
|
||||
];
|
||||
|
||||
return $this->importService = new ImportService(
|
||||
@@ -63,9 +69,9 @@ class ImportServicesFactory
|
||||
$this->createImportAuditService(),
|
||||
$this->createImportStateStoreService(),
|
||||
$profiles,
|
||||
$this->createPermissionGateway(),
|
||||
$this->createPermissionService(),
|
||||
$this->createSettingsDefaultsGateway(),
|
||||
$this->userServicesFactory->createUserScopeGateway(),
|
||||
$this->tenantScopeService,
|
||||
$this->sessionStore
|
||||
);
|
||||
}
|
||||
@@ -98,9 +104,9 @@ class ImportServicesFactory
|
||||
return $this->importRepositoryFactory->createImportAuditRunRepository();
|
||||
}
|
||||
|
||||
private function createPermissionGateway(): PermissionGateway
|
||||
private function createPermissionService(): PermissionService
|
||||
{
|
||||
return $this->permissionGateway ??= $this->accessServicesFactory->createPermissionGateway();
|
||||
return $this->permissionService ??= $this->accessServicesFactory->createPermissionService();
|
||||
}
|
||||
|
||||
private function createSettingsDefaultsGateway(): SettingsDefaultsGateway
|
||||
|
||||
@@ -4,58 +4,32 @@ namespace MintyPHP\Service\Import\Profile;
|
||||
|
||||
use MintyPHP\Repository\Org\DepartmentRepositoryInterface;
|
||||
use MintyPHP\Repository\Tenant\TenantRepositoryInterface;
|
||||
use MintyPHP\Service\Org\DepartmentService;
|
||||
|
||||
class DepartmentImportGateway
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DepartmentRepositoryInterface $departmentRepository,
|
||||
private readonly TenantRepositoryInterface $tenantRepository,
|
||||
private readonly DepartmentService $departmentService
|
||||
private readonly TenantRepositoryInterface $tenantRepository
|
||||
) {
|
||||
}
|
||||
|
||||
public function findTenantByUuid(string $uuid): ?array
|
||||
{
|
||||
return $this->tenantRepository()->findByUuid($uuid);
|
||||
return $this->tenantRepository->findByUuid($uuid);
|
||||
}
|
||||
|
||||
public function findTenantById(int $id): ?array
|
||||
{
|
||||
return $this->tenantRepository()->find($id);
|
||||
return $this->tenantRepository->find($id);
|
||||
}
|
||||
|
||||
public function existsDepartmentCode(string $code): bool
|
||||
{
|
||||
return $this->departmentRepository()->existsByCode($code);
|
||||
return $this->departmentRepository->existsByCode($code);
|
||||
}
|
||||
|
||||
public function existsDepartmentByTenantAndDescription(int $tenantId, string $description): bool
|
||||
{
|
||||
return $this->departmentRepository()->existsByTenantAndDescription($tenantId, $description);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function createDepartmentFromAdmin(array $input, int $currentUserId): array
|
||||
{
|
||||
return $this->departmentService()->createFromAdmin($input, $currentUserId);
|
||||
}
|
||||
|
||||
private function departmentRepository(): DepartmentRepositoryInterface
|
||||
{
|
||||
return $this->departmentRepository;
|
||||
}
|
||||
|
||||
private function tenantRepository(): TenantRepositoryInterface
|
||||
{
|
||||
return $this->tenantRepository;
|
||||
}
|
||||
|
||||
private function departmentService(): DepartmentService
|
||||
{
|
||||
return $this->departmentService;
|
||||
return $this->departmentRepository->existsByTenantAndDescription($tenantId, $description);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,14 @@
|
||||
|
||||
namespace MintyPHP\Service\Import\Profile;
|
||||
|
||||
use MintyPHP\Service\Org\DepartmentService;
|
||||
|
||||
class DepartmentImportProfile implements ImportProfileInterface
|
||||
{
|
||||
public function __construct(private readonly DepartmentImportGateway $gateway)
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DepartmentImportGateway $gateway,
|
||||
private readonly DepartmentService $departmentService
|
||||
) {
|
||||
}
|
||||
|
||||
public function key(): string
|
||||
@@ -139,7 +143,7 @@ class DepartmentImportProfile implements ImportProfileInterface
|
||||
return ['status' => 'skipped', 'code' => 'department_exists'];
|
||||
}
|
||||
|
||||
$result = $this->gateway->createDepartmentFromAdmin([
|
||||
$result = $this->departmentService->createFromAdmin([
|
||||
'description' => (string) ($normalized['description'] ?? ''),
|
||||
'tenant_id' => (int) ($normalized['tenant_id'] ?? 0),
|
||||
'code' => (string) ($normalized['code'] ?? ''),
|
||||
|
||||
@@ -6,13 +6,11 @@ use MintyPHP\Repository\Access\RoleRepositoryInterface;
|
||||
use MintyPHP\Repository\Org\DepartmentRepositoryInterface;
|
||||
use MintyPHP\Repository\Tenant\TenantRepositoryInterface;
|
||||
use MintyPHP\Repository\User\UserReadRepositoryInterface;
|
||||
use MintyPHP\Service\User\UserAccountService;
|
||||
|
||||
class UserImportGateway
|
||||
{
|
||||
public function __construct(
|
||||
private readonly UserReadRepositoryInterface $userReadRepository,
|
||||
private readonly UserAccountService $userAccountService,
|
||||
private readonly TenantRepositoryInterface $tenantRepository,
|
||||
private readonly RoleRepositoryInterface $roleRepository,
|
||||
private readonly DepartmentRepositoryInterface $departmentRepository,
|
||||
@@ -53,13 +51,4 @@ class UserImportGateway
|
||||
{
|
||||
return $this->departmentRepository->find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function createUserFromAdmin(array $input, int $currentUserId): array
|
||||
{
|
||||
return $this->userAccountService->createFromAdmin($input, $currentUserId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace MintyPHP\Service\Import\Profile;
|
||||
|
||||
use MintyPHP\Service\User\UserAccountService;
|
||||
|
||||
class UserImportProfile implements ImportProfileInterface
|
||||
{
|
||||
/**
|
||||
@@ -9,8 +11,10 @@ class UserImportProfile implements ImportProfileInterface
|
||||
*/
|
||||
private ?array $allowedLocales = null;
|
||||
|
||||
public function __construct(private readonly UserImportGateway $gateway)
|
||||
{
|
||||
public function __construct(
|
||||
private readonly UserImportGateway $gateway,
|
||||
private readonly UserAccountService $userAccountService
|
||||
) {
|
||||
}
|
||||
|
||||
public function key(): string
|
||||
@@ -203,7 +207,7 @@ class UserImportProfile implements ImportProfileInterface
|
||||
$input['active'] = null;
|
||||
}
|
||||
|
||||
$result = $this->gateway->createUserFromAdmin($input, (int) ($context['current_user_id'] ?? 0));
|
||||
$result = $this->userAccountService->createFromAdmin($input, (int) ($context['current_user_id'] ?? 0));
|
||||
if (!($result['ok'] ?? false)) {
|
||||
$errors = $result['errors'] ?? [];
|
||||
$message = is_array($errors) && isset($errors[0]) ? (string) $errors[0] : t('User can not be registered');
|
||||
|
||||
Reference in New Issue
Block a user