instances added god may help
This commit is contained in:
61
lib/Service/Import/Profile/DepartmentImportGateway.php
Normal file
61
lib/Service/Import/Profile/DepartmentImportGateway.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Service\Import\Profile;
|
||||
|
||||
use MintyPHP\Repository\Org\DepartmentRepository;
|
||||
use MintyPHP\Repository\Tenant\TenantRepository;
|
||||
use MintyPHP\Service\Org\DepartmentService;
|
||||
|
||||
class DepartmentImportGateway
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ?DepartmentRepository $departmentRepository = null,
|
||||
private readonly ?TenantRepository $tenantRepository = null,
|
||||
private readonly ?DepartmentService $departmentService = null
|
||||
) {
|
||||
}
|
||||
|
||||
public function findTenantByUuid(string $uuid): ?array
|
||||
{
|
||||
return $this->tenantRepository()->findByUuid($uuid);
|
||||
}
|
||||
|
||||
public function findTenantById(int $id): ?array
|
||||
{
|
||||
return $this->tenantRepository()->find($id);
|
||||
}
|
||||
|
||||
public function existsDepartmentCode(string $code): bool
|
||||
{
|
||||
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(): DepartmentRepository
|
||||
{
|
||||
return $this->departmentRepository ?? new DepartmentRepository();
|
||||
}
|
||||
|
||||
private function tenantRepository(): TenantRepository
|
||||
{
|
||||
return $this->tenantRepository ?? new TenantRepository();
|
||||
}
|
||||
|
||||
private function departmentService(): DepartmentService
|
||||
{
|
||||
return $this->departmentService ?? new DepartmentService();
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
namespace MintyPHP\Service\Import\Profile;
|
||||
|
||||
use MintyPHP\Repository\Org\DepartmentRepository;
|
||||
use MintyPHP\Repository\Tenant\TenantRepository;
|
||||
use MintyPHP\Service\Org\DepartmentService;
|
||||
|
||||
class DepartmentImportProfile implements ImportProfileInterface
|
||||
{
|
||||
public function __construct(private readonly DepartmentImportGateway $gateway)
|
||||
{
|
||||
}
|
||||
|
||||
public function key(): string
|
||||
{
|
||||
return 'departments';
|
||||
@@ -81,7 +81,7 @@ class DepartmentImportProfile implements ImportProfileInterface
|
||||
if (!$this->isUuid($tenantRaw)) {
|
||||
$errors[] = ['code' => 'invalid_tenant_uuid'];
|
||||
} else {
|
||||
$tenant = TenantRepository::findByUuid($tenantRaw);
|
||||
$tenant = $this->gateway->findTenantByUuid($tenantRaw);
|
||||
if (!$tenant || (string) ($tenant['status'] ?? '') !== 'active') {
|
||||
$errors[] = ['code' => 'tenant_not_found'];
|
||||
}
|
||||
@@ -91,7 +91,7 @@ class DepartmentImportProfile implements ImportProfileInterface
|
||||
if (!$tenant) {
|
||||
$defaultTenantId = (int) ($context['default_tenant_id'] ?? 0);
|
||||
if ($defaultTenantId > 0) {
|
||||
$defaultTenant = TenantRepository::find($defaultTenantId);
|
||||
$defaultTenant = $this->gateway->findTenantById($defaultTenantId);
|
||||
if ($defaultTenant && (string) ($defaultTenant['status'] ?? '') === 'active') {
|
||||
$tenant = $defaultTenant;
|
||||
}
|
||||
@@ -139,7 +139,7 @@ class DepartmentImportProfile implements ImportProfileInterface
|
||||
return ['status' => 'skipped', 'code' => 'department_exists'];
|
||||
}
|
||||
|
||||
$result = DepartmentService::createFromAdmin([
|
||||
$result = $this->gateway->createDepartmentFromAdmin([
|
||||
'description' => (string) ($normalized['description'] ?? ''),
|
||||
'tenant_id' => (int) ($normalized['tenant_id'] ?? 0),
|
||||
'code' => (string) ($normalized['code'] ?? ''),
|
||||
@@ -190,7 +190,7 @@ class DepartmentImportProfile implements ImportProfileInterface
|
||||
if ($code === '') {
|
||||
return false;
|
||||
}
|
||||
return DepartmentRepository::existsByCode($code);
|
||||
return $this->gateway->existsDepartmentCode($code);
|
||||
}
|
||||
|
||||
private function departmentExists(array $normalized): bool
|
||||
@@ -200,7 +200,7 @@ class DepartmentImportProfile implements ImportProfileInterface
|
||||
if ($tenantId <= 0 || $description === '') {
|
||||
return false;
|
||||
}
|
||||
return DepartmentRepository::existsByTenantAndDescription($tenantId, $description);
|
||||
return $this->gateway->existsDepartmentByTenantAndDescription($tenantId, $description);
|
||||
}
|
||||
|
||||
private function parseActive(string $value): ?int
|
||||
|
||||
62
lib/Service/Import/Profile/UserImportGateway.php
Normal file
62
lib/Service/Import/Profile/UserImportGateway.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Service\Import\Profile;
|
||||
|
||||
use MintyPHP\Repository\Access\RoleRepository;
|
||||
use MintyPHP\Repository\Org\DepartmentRepository;
|
||||
use MintyPHP\Repository\Tenant\TenantRepository;
|
||||
use MintyPHP\Repository\User\UserReadRepository;
|
||||
use MintyPHP\Service\User\UserAccountService;
|
||||
|
||||
class UserImportGateway
|
||||
{
|
||||
public function __construct(
|
||||
private readonly UserReadRepository $userReadRepository,
|
||||
private readonly UserAccountService $userAccountService
|
||||
) {
|
||||
}
|
||||
|
||||
public function findUserByEmail(string $email): ?array
|
||||
{
|
||||
return $this->userReadRepository->findByEmail($email);
|
||||
}
|
||||
|
||||
public function findTenantByUuid(string $uuid): ?array
|
||||
{
|
||||
return (new TenantRepository())->findByUuid($uuid);
|
||||
}
|
||||
|
||||
public function findTenantById(int $id): ?array
|
||||
{
|
||||
return (new TenantRepository())->find($id);
|
||||
}
|
||||
|
||||
public function findRoleByUuid(string $uuid): ?array
|
||||
{
|
||||
return (new RoleRepository())->findByUuid($uuid);
|
||||
}
|
||||
|
||||
public function findRoleById(int $id): ?array
|
||||
{
|
||||
return (new RoleRepository())->find($id);
|
||||
}
|
||||
|
||||
public function findDepartmentByUuid(string $uuid): ?array
|
||||
{
|
||||
return (new DepartmentRepository())->findByUuid($uuid);
|
||||
}
|
||||
|
||||
public function findDepartmentById(int $id): ?array
|
||||
{
|
||||
return (new 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,12 +2,6 @@
|
||||
|
||||
namespace MintyPHP\Service\Import\Profile;
|
||||
|
||||
use MintyPHP\Repository\Access\RoleRepository;
|
||||
use MintyPHP\Repository\Org\DepartmentRepository;
|
||||
use MintyPHP\Repository\Tenant\TenantRepository;
|
||||
use MintyPHP\Repository\User\UserRepository;
|
||||
use MintyPHP\Service\User\UserService;
|
||||
|
||||
class UserImportProfile implements ImportProfileInterface
|
||||
{
|
||||
/**
|
||||
@@ -15,6 +9,10 @@ class UserImportProfile implements ImportProfileInterface
|
||||
*/
|
||||
private ?array $allowedLocales = null;
|
||||
|
||||
public function __construct(private readonly UserImportGateway $gateway)
|
||||
{
|
||||
}
|
||||
|
||||
public function key(): string
|
||||
{
|
||||
return 'users';
|
||||
@@ -159,7 +157,7 @@ class UserImportProfile implements ImportProfileInterface
|
||||
|
||||
public function dryRunRow(array $normalized, array $context): array
|
||||
{
|
||||
$existing = UserRepository::findByEmail((string) ($normalized['email'] ?? ''));
|
||||
$existing = $this->gateway->findUserByEmail((string) ($normalized['email'] ?? ''));
|
||||
if ($existing) {
|
||||
return ['status' => 'skipped', 'code' => 'email_exists'];
|
||||
}
|
||||
@@ -168,7 +166,7 @@ class UserImportProfile implements ImportProfileInterface
|
||||
|
||||
public function commitRow(array $normalized, array $context): array
|
||||
{
|
||||
$existing = UserRepository::findByEmail((string) ($normalized['email'] ?? ''));
|
||||
$existing = $this->gateway->findUserByEmail((string) ($normalized['email'] ?? ''));
|
||||
if ($existing) {
|
||||
return ['status' => 'skipped', 'code' => 'email_exists'];
|
||||
}
|
||||
@@ -201,11 +199,11 @@ class UserImportProfile implements ImportProfileInterface
|
||||
if ((int) ($normalized['active'] ?? 1) === 1) {
|
||||
$input['active'] = 1;
|
||||
} else {
|
||||
// UserService::createFromAdmin expects a present-but-null value for inactive state.
|
||||
// The admin create flow expects a present-but-null value for inactive state.
|
||||
$input['active'] = null;
|
||||
}
|
||||
|
||||
$result = UserService::createFromAdmin($input, (int) ($context['current_user_id'] ?? 0));
|
||||
$result = $this->gateway->createUserFromAdmin($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');
|
||||
@@ -335,9 +333,9 @@ class UserImportProfile implements ImportProfileInterface
|
||||
{
|
||||
$tenant = null;
|
||||
if ($this->isUuid($value)) {
|
||||
$tenant = TenantRepository::findByUuid($value);
|
||||
$tenant = $this->gateway->findTenantByUuid($value);
|
||||
} elseif (ctype_digit($value)) {
|
||||
$tenant = TenantRepository::find((int) $value);
|
||||
$tenant = $this->gateway->findTenantById((int) $value);
|
||||
}
|
||||
|
||||
if (!$tenant) {
|
||||
@@ -350,9 +348,9 @@ class UserImportProfile implements ImportProfileInterface
|
||||
{
|
||||
$role = null;
|
||||
if ($this->isUuid($value)) {
|
||||
$role = RoleRepository::findByUuid($value);
|
||||
$role = $this->gateway->findRoleByUuid($value);
|
||||
} elseif (ctype_digit($value)) {
|
||||
$role = RoleRepository::find((int) $value);
|
||||
$role = $this->gateway->findRoleById((int) $value);
|
||||
}
|
||||
|
||||
if (!$role) {
|
||||
@@ -365,9 +363,9 @@ class UserImportProfile implements ImportProfileInterface
|
||||
{
|
||||
$department = null;
|
||||
if ($this->isUuid($value)) {
|
||||
$department = DepartmentRepository::findByUuid($value);
|
||||
$department = $this->gateway->findDepartmentByUuid($value);
|
||||
} elseif (ctype_digit($value)) {
|
||||
$department = DepartmentRepository::find((int) $value);
|
||||
$department = $this->gateway->findDepartmentById((int) $value);
|
||||
}
|
||||
|
||||
if (!$department) {
|
||||
|
||||
Reference in New Issue
Block a user