instances added god may help
This commit is contained in:
@@ -3,16 +3,14 @@
|
||||
namespace MintyPHP\Service\Import;
|
||||
|
||||
use MintyPHP\Service\Access\PermissionService;
|
||||
use MintyPHP\Service\Access\PermissionGateway;
|
||||
use MintyPHP\Service\Audit\ImportAuditService;
|
||||
use MintyPHP\Service\Import\Profile\DepartmentImportProfile;
|
||||
use MintyPHP\Service\Import\Profile\ImportProfileInterface;
|
||||
use MintyPHP\Service\Import\Profile\UserImportProfile;
|
||||
use MintyPHP\Service\Settings\SettingService;
|
||||
use MintyPHP\Service\Tenant\TenantScopeService;
|
||||
use MintyPHP\Service\Settings\SettingGateway;
|
||||
use MintyPHP\Service\User\UserScopeGateway;
|
||||
|
||||
class ImportService
|
||||
{
|
||||
private const SESSION_KEY = 'admin_imports_v1';
|
||||
private const MAX_ROWS = 20000;
|
||||
private const MAX_ERROR_ROWS = 500;
|
||||
private const PROFILE_PERMISSION_MAP = [
|
||||
@@ -21,42 +19,44 @@ class ImportService
|
||||
];
|
||||
|
||||
/**
|
||||
* @return array<string, ImportProfileInterface>
|
||||
* @param array<string, ImportProfileInterface> $profiles
|
||||
*/
|
||||
public static function profiles(): array
|
||||
{
|
||||
static $profiles = null;
|
||||
if (is_array($profiles)) {
|
||||
return $profiles;
|
||||
}
|
||||
|
||||
$users = new UserImportProfile();
|
||||
$departments = new DepartmentImportProfile();
|
||||
$profiles = [
|
||||
$users->key() => $users,
|
||||
$departments->key() => $departments,
|
||||
];
|
||||
|
||||
return $profiles;
|
||||
public function __construct(
|
||||
private readonly CsvReaderService $csvReaderService,
|
||||
private readonly ImportTempFileService $importTempFileService,
|
||||
private readonly ImportAuditService $importAuditService,
|
||||
private readonly ImportStateStoreService $importStateStoreService,
|
||||
private readonly array $profiles,
|
||||
private readonly PermissionGateway $permissionGateway,
|
||||
private readonly SettingGateway $settingGateway,
|
||||
private readonly UserScopeGateway $userScopeGateway
|
||||
) {
|
||||
}
|
||||
|
||||
public static function profile(string $type): ?ImportProfileInterface
|
||||
/**
|
||||
* @return array<string, ImportProfileInterface>
|
||||
*/
|
||||
public function profiles(): array
|
||||
{
|
||||
return $this->profiles;
|
||||
}
|
||||
|
||||
public function profile(string $type): ?ImportProfileInterface
|
||||
{
|
||||
$type = trim($type);
|
||||
if ($type === '') {
|
||||
return null;
|
||||
}
|
||||
$profiles = self::profiles();
|
||||
return $profiles[$type] ?? null;
|
||||
return $this->profiles[$type] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{key:string,label:string}>
|
||||
*/
|
||||
public static function listProfileOptions(): array
|
||||
public function listProfileOptions(): array
|
||||
{
|
||||
$options = [];
|
||||
foreach (self::profiles() as $profile) {
|
||||
foreach ($this->profiles() as $profile) {
|
||||
$options[] = [
|
||||
'key' => $profile->key(),
|
||||
'label' => $profile->label(),
|
||||
@@ -65,7 +65,7 @@ class ImportService
|
||||
return $options;
|
||||
}
|
||||
|
||||
public static function requiredPermissionForType(string $type): string
|
||||
public function requiredPermissionForType(string $type): string
|
||||
{
|
||||
$type = trim($type);
|
||||
if ($type === '') {
|
||||
@@ -78,23 +78,23 @@ class ImportService
|
||||
* @param array<string, mixed> $upload
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function analyzeUpload(array $upload, string $type, int $currentUserId): array
|
||||
public function analyzeUpload(array $upload, string $type, int $currentUserId): array
|
||||
{
|
||||
$profile = self::profile($type);
|
||||
$profile = $this->profile($type);
|
||||
if (!$profile) {
|
||||
return ['ok' => false, 'code' => 'invalid_file_type'];
|
||||
}
|
||||
|
||||
$stored = ImportTempFileService::storeUploadedFile($upload);
|
||||
$stored = $this->importTempFileService->storeUploadedFile($upload);
|
||||
if (!$stored['ok']) {
|
||||
$code = isset($stored['code']) ? (string) $stored['code'] : 'upload_missing';
|
||||
return ['ok' => false, 'code' => $code];
|
||||
}
|
||||
|
||||
$path = (string) ($stored['path'] ?? '');
|
||||
$analysis = CsvReaderService::analyze($path, self::MAX_ROWS);
|
||||
$analysis = $this->csvReaderService->analyze($path, self::MAX_ROWS);
|
||||
if (!$analysis['ok']) {
|
||||
ImportTempFileService::delete($path);
|
||||
$this->importTempFileService->delete($path);
|
||||
$code = isset($analysis['code']) ? (string) $analysis['code'] : 'invalid_csv_header';
|
||||
return ['ok' => false, 'code' => $code];
|
||||
}
|
||||
@@ -103,8 +103,8 @@ class ImportService
|
||||
$delimiter = (string) ($analysis['delimiter'] ?? ',');
|
||||
$rowsTotal = (int) ($analysis['rows_total'] ?? 0);
|
||||
|
||||
$token = self::newToken();
|
||||
self::setState($token, [
|
||||
$token = $this->newToken();
|
||||
$this->importStateStoreService->setState($token, [
|
||||
'token' => $token,
|
||||
'type' => $profile->key(),
|
||||
'path' => $path,
|
||||
@@ -125,7 +125,7 @@ class ImportService
|
||||
'delimiter' => $delimiter,
|
||||
'allowed_targets' => $profile->allowedTargets(),
|
||||
'required_targets' => $profile->requiredTargets(),
|
||||
'auto_mapping' => self::buildAutoMapping($headers, $profile),
|
||||
'auto_mapping' => $this->buildAutoMapping($headers, $profile),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -133,9 +133,9 @@ class ImportService
|
||||
* @param array<string, mixed> $mappingInput
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function preview(string $token, array $mappingInput, int $currentUserId): array
|
||||
public function preview(string $token, array $mappingInput, int $currentUserId): array
|
||||
{
|
||||
$state = self::state($token);
|
||||
$state = $this->state($token);
|
||||
if (!$state) {
|
||||
return ['ok' => false, 'code' => 'invalid_file_type'];
|
||||
}
|
||||
@@ -143,12 +143,12 @@ class ImportService
|
||||
return ['ok' => false, 'code' => 'invalid_file_type'];
|
||||
}
|
||||
|
||||
$profile = self::profile((string) ($state['type'] ?? ''));
|
||||
$profile = $this->profile((string) ($state['type'] ?? ''));
|
||||
if (!$profile) {
|
||||
return ['ok' => false, 'code' => 'invalid_file_type'];
|
||||
}
|
||||
|
||||
$mappingResult = self::normalizeMapping($mappingInput, (array) ($state['headers'] ?? []), $profile);
|
||||
$mappingResult = $this->normalizeMapping($mappingInput, (array) ($state['headers'] ?? []), $profile);
|
||||
if (!($mappingResult['ok'] ?? false)) {
|
||||
return ['ok' => false, 'code' => (string) ($mappingResult['code'] ?? 'mapping_required_missing')];
|
||||
}
|
||||
@@ -156,22 +156,22 @@ class ImportService
|
||||
$mappedTargets = $mappingResult['mapped_targets'] ?? [];
|
||||
if (
|
||||
$profile->requiresAssignmentPermission()
|
||||
&& self::usesAssignmentTargets($mappedTargets)
|
||||
&& !self::canImportAssignments($currentUserId)
|
||||
&& $this->usesAssignmentTargets($mappedTargets)
|
||||
&& !$this->canImportAssignments($currentUserId)
|
||||
) {
|
||||
return ['ok' => false, 'code' => 'assignment_permission_required'];
|
||||
}
|
||||
|
||||
return self::process($state, $mappingResult['mapping'], $profile, $currentUserId, false);
|
||||
return $this->process($state, $mappingResult['mapping'], $profile, $currentUserId, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $mappingInput
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function commit(string $token, array $mappingInput, int $currentUserId): array
|
||||
public function commit(string $token, array $mappingInput, int $currentUserId): array
|
||||
{
|
||||
$state = self::state($token);
|
||||
$state = $this->state($token);
|
||||
if (!$state) {
|
||||
return ['ok' => false, 'code' => 'invalid_file_type'];
|
||||
}
|
||||
@@ -179,12 +179,12 @@ class ImportService
|
||||
return ['ok' => false, 'code' => 'invalid_file_type'];
|
||||
}
|
||||
|
||||
$profile = self::profile((string) ($state['type'] ?? ''));
|
||||
$profile = $this->profile((string) ($state['type'] ?? ''));
|
||||
if (!$profile) {
|
||||
return ['ok' => false, 'code' => 'invalid_file_type'];
|
||||
}
|
||||
|
||||
$mappingResult = self::normalizeMapping($mappingInput, (array) ($state['headers'] ?? []), $profile);
|
||||
$mappingResult = $this->normalizeMapping($mappingInput, (array) ($state['headers'] ?? []), $profile);
|
||||
if (!($mappingResult['ok'] ?? false)) {
|
||||
return ['ok' => false, 'code' => (string) ($mappingResult['code'] ?? 'mapping_required_missing')];
|
||||
}
|
||||
@@ -192,13 +192,13 @@ class ImportService
|
||||
$mappedTargets = $mappingResult['mapped_targets'] ?? [];
|
||||
if (
|
||||
$profile->requiresAssignmentPermission()
|
||||
&& self::usesAssignmentTargets($mappedTargets)
|
||||
&& !self::canImportAssignments($currentUserId)
|
||||
&& $this->usesAssignmentTargets($mappedTargets)
|
||||
&& !$this->canImportAssignments($currentUserId)
|
||||
) {
|
||||
return ['ok' => false, 'code' => 'assignment_permission_required'];
|
||||
}
|
||||
|
||||
$auditRunId = ImportAuditService::startRun(
|
||||
$auditRunId = $this->importAuditService->startRun(
|
||||
(string) ($state['type'] ?? $profile->key()),
|
||||
$mappingResult['mapped_targets'] ?? [],
|
||||
isset($state['source_filename']) ? (string) $state['source_filename'] : null,
|
||||
@@ -206,51 +206,29 @@ class ImportService
|
||||
isset($_SESSION['current_tenant']['id']) ? (int) $_SESSION['current_tenant']['id'] : null
|
||||
);
|
||||
|
||||
$result = null;
|
||||
$result = ['ok' => false, 'code' => 'unexpected_error', 'processed' => 0, 'created' => 0, 'skipped' => 0, 'failed' => 1];
|
||||
$forcedAuditStatus = null;
|
||||
try {
|
||||
$result = self::process($state, $mappingResult['mapping'], $profile, $currentUserId, true);
|
||||
$result = $this->process($state, $mappingResult['mapping'], $profile, $currentUserId, true);
|
||||
} catch (\Throwable $exception) {
|
||||
$forcedAuditStatus = 'failed';
|
||||
$result = ['ok' => false, 'code' => 'unexpected_error', 'processed' => 0, 'created' => 0, 'skipped' => 0, 'failed' => 1];
|
||||
} finally {
|
||||
ImportAuditService::finishRun($auditRunId, is_array($result) ? $result : [], $forcedAuditStatus);
|
||||
$this->importAuditService->finishRun($auditRunId, $result, $forcedAuditStatus);
|
||||
}
|
||||
|
||||
ImportTempFileService::delete((string) ($state['path'] ?? ''));
|
||||
self::clearState($token);
|
||||
$this->importTempFileService->delete((string) ($state['path'] ?? ''));
|
||||
$this->importStateStoreService->clearState($token);
|
||||
|
||||
return is_array($result) ? $result : ['ok' => false, 'code' => 'unexpected_error'];
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
public static function state(string $token): ?array
|
||||
public function state(string $token): ?array
|
||||
{
|
||||
$token = trim($token);
|
||||
if ($token === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$all = $_SESSION[self::SESSION_KEY] ?? [];
|
||||
if (!is_array($all)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$state = $all[$token] ?? null;
|
||||
if (!is_array($state)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$createdAt = (int) ($state['created_at'] ?? 0);
|
||||
if ($createdAt > 0 && (time() - $createdAt) > 7200) {
|
||||
ImportTempFileService::delete((string) ($state['path'] ?? ''));
|
||||
self::clearState($token);
|
||||
return null;
|
||||
}
|
||||
|
||||
return $state;
|
||||
return $this->importStateStoreService->getState($token);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -258,7 +236,7 @@ class ImportService
|
||||
* @param array<string, string> $mapping
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private static function process(
|
||||
private function process(
|
||||
array $state,
|
||||
array $mapping,
|
||||
ImportProfileInterface $profile,
|
||||
@@ -272,7 +250,7 @@ class ImportService
|
||||
return ['ok' => false, 'code' => 'invalid_csv_header'];
|
||||
}
|
||||
|
||||
$context = self::buildContext($currentUserId);
|
||||
$context = $this->buildContext($currentUserId);
|
||||
$seenRowKeys = [];
|
||||
$processed = 0;
|
||||
$created = 0;
|
||||
@@ -282,7 +260,7 @@ class ImportService
|
||||
$errors = [];
|
||||
$errorCounts = [];
|
||||
|
||||
CsvReaderService::streamRows(
|
||||
$this->csvReaderService->streamRows(
|
||||
$path,
|
||||
$delimiter,
|
||||
array_values($headers),
|
||||
@@ -304,13 +282,13 @@ class ImportService
|
||||
$mapped = [];
|
||||
$identifier = '';
|
||||
try {
|
||||
$mapped = self::mapRow($rowAssoc, $mapping);
|
||||
$mapped = $this->mapRow($rowAssoc, $mapping);
|
||||
$validation = $profile->validateMappedRow($mapped, $context);
|
||||
if (!$validation['ok']) {
|
||||
$failed++;
|
||||
foreach ($validation['errors'] as $error) {
|
||||
self::incrementErrorCount($errorCounts, (string) ($error['code'] ?? 'unexpected_error'));
|
||||
self::pushError(
|
||||
$this->incrementErrorCount($errorCounts, (string) $error['code']);
|
||||
$this->pushError(
|
||||
$errors,
|
||||
$lineNumber,
|
||||
$profile->rowIdentifier($mapped),
|
||||
@@ -327,8 +305,8 @@ class ImportService
|
||||
if ($duplicateKey !== null && $duplicateKey !== '') {
|
||||
if (isset($seenRowKeys[$duplicateKey])) {
|
||||
$skipped++;
|
||||
self::incrementErrorCount($errorCounts, 'duplicate_in_file');
|
||||
self::pushError($errors, $lineNumber, $identifier, 'duplicate_in_file', null);
|
||||
$this->incrementErrorCount($errorCounts, 'duplicate_in_file');
|
||||
$this->pushError($errors, $lineNumber, $identifier, 'duplicate_in_file', null);
|
||||
return;
|
||||
}
|
||||
$seenRowKeys[$duplicateKey] = true;
|
||||
@@ -350,18 +328,18 @@ class ImportService
|
||||
if ($status === 'skipped') {
|
||||
$skipped++;
|
||||
$errorCode = $code !== '' ? $code : 'email_exists';
|
||||
self::incrementErrorCount($errorCounts, $errorCode);
|
||||
self::pushError($errors, $lineNumber, $identifier, $errorCode, $message);
|
||||
$this->incrementErrorCount($errorCounts, $errorCode);
|
||||
$this->pushError($errors, $lineNumber, $identifier, $errorCode, $message);
|
||||
return;
|
||||
}
|
||||
|
||||
$failed++;
|
||||
$errorCode = $code !== '' ? $code : 'unexpected_error';
|
||||
self::incrementErrorCount($errorCounts, $errorCode);
|
||||
self::pushError($errors, $lineNumber, $identifier, $errorCode, $message);
|
||||
$this->incrementErrorCount($errorCounts, $errorCode);
|
||||
$this->pushError($errors, $lineNumber, $identifier, $errorCode, $message);
|
||||
} catch (\Throwable $exception) {
|
||||
$failed++;
|
||||
self::incrementErrorCount($errorCounts, 'unexpected_error');
|
||||
$this->incrementErrorCount($errorCounts, 'unexpected_error');
|
||||
if ($identifier === '' && $mapped) {
|
||||
try {
|
||||
$identifier = $profile->rowIdentifier($mapped);
|
||||
@@ -369,7 +347,7 @@ class ImportService
|
||||
$identifier = '';
|
||||
}
|
||||
}
|
||||
self::pushError($errors, $lineNumber, $identifier, 'unexpected_error', null);
|
||||
$this->pushError($errors, $lineNumber, $identifier, 'unexpected_error', null);
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -401,7 +379,7 @@ class ImportService
|
||||
* @param array<int, string> $headers
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private static function normalizeMapping(array $mappingInput, array $headers, ImportProfileInterface $profile): array
|
||||
private function normalizeMapping(array $mappingInput, array $headers, ImportProfileInterface $profile): array
|
||||
{
|
||||
$mappingRaw = $mappingInput['mapping'] ?? $mappingInput;
|
||||
if (!is_array($mappingRaw)) {
|
||||
@@ -445,7 +423,7 @@ class ImportService
|
||||
* @param array<string, string> $rowAssoc
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private static function mapRow(array $rowAssoc, array $mapping): array
|
||||
private function mapRow(array $rowAssoc, array $mapping): array
|
||||
{
|
||||
$mapped = [];
|
||||
foreach ($mapping as $header => $target) {
|
||||
@@ -461,22 +439,22 @@ class ImportService
|
||||
* @param array<int, string> $headers
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private static function buildAutoMapping(array $headers, ImportProfileInterface $profile): array
|
||||
private function buildAutoMapping(array $headers, ImportProfileInterface $profile): array
|
||||
{
|
||||
$mapping = [];
|
||||
$aliases = $profile->autoMapAliases();
|
||||
$normalizedAliasMap = [];
|
||||
|
||||
foreach ($aliases as $target => $targetAliases) {
|
||||
$normalizedAliasMap[self::normalizeHeader($target)] = $target;
|
||||
$normalizedAliasMap[$this->normalizeHeader($target)] = $target;
|
||||
foreach ($targetAliases as $alias) {
|
||||
$normalizedAliasMap[self::normalizeHeader($alias)] = $target;
|
||||
$normalizedAliasMap[$this->normalizeHeader($alias)] = $target;
|
||||
}
|
||||
}
|
||||
|
||||
$assignedTargets = [];
|
||||
foreach ($headers as $header) {
|
||||
$normalized = self::normalizeHeader($header);
|
||||
$normalized = $this->normalizeHeader($header);
|
||||
if ($normalized === '') {
|
||||
continue;
|
||||
}
|
||||
@@ -494,7 +472,7 @@ class ImportService
|
||||
/**
|
||||
* @param array<int, string> $mappedTargets
|
||||
*/
|
||||
private static function usesAssignmentTargets(array $mappedTargets): bool
|
||||
private function usesAssignmentTargets(array $mappedTargets): bool
|
||||
{
|
||||
foreach ($mappedTargets as $target) {
|
||||
if (in_array($target, ['tenant', 'role', 'department'], true)) {
|
||||
@@ -504,29 +482,29 @@ class ImportService
|
||||
return false;
|
||||
}
|
||||
|
||||
private static function canImportAssignments(int $userId): bool
|
||||
private function canImportAssignments(int $userId): bool
|
||||
{
|
||||
return PermissionService::userHas($userId, PermissionService::USERS_IMPORT_ASSIGNMENTS);
|
||||
return $this->permissionGateway->userHas($userId, PermissionService::USERS_IMPORT_ASSIGNMENTS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private static function buildContext(int $currentUserId): array
|
||||
private function buildContext(int $currentUserId): array
|
||||
{
|
||||
$isGlobalAdmin = TenantScopeService::hasGlobalAccess($currentUserId);
|
||||
$isGlobalAdmin = $this->userScopeGateway->hasGlobalAccess($currentUserId);
|
||||
return [
|
||||
'current_user_id' => $currentUserId,
|
||||
'is_global_admin' => $isGlobalAdmin,
|
||||
'allowed_tenant_ids' => $isGlobalAdmin ? [] : TenantScopeService::getUserTenantIds($currentUserId),
|
||||
'default_tenant_id' => SettingService::getDefaultTenantId(),
|
||||
'default_role_id' => SettingService::getDefaultRoleId(),
|
||||
'default_department_id' => SettingService::getDefaultDepartmentId(),
|
||||
'can_import_assignments' => self::canImportAssignments($currentUserId),
|
||||
'allowed_tenant_ids' => $isGlobalAdmin ? [] : $this->userScopeGateway->getUserTenantIds($currentUserId),
|
||||
'default_tenant_id' => $this->settingGateway->getDefaultTenantId(),
|
||||
'default_role_id' => $this->settingGateway->getDefaultRoleId(),
|
||||
'default_department_id' => $this->settingGateway->getDefaultDepartmentId(),
|
||||
'can_import_assignments' => $this->canImportAssignments($currentUserId),
|
||||
];
|
||||
}
|
||||
|
||||
private static function newToken(): string
|
||||
private function newToken(): string
|
||||
{
|
||||
try {
|
||||
return bin2hex(random_bytes(24));
|
||||
@@ -535,33 +513,14 @@ class ImportService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $state
|
||||
*/
|
||||
private static function setState(string $token, array $state): void
|
||||
{
|
||||
if (!isset($_SESSION[self::SESSION_KEY]) || !is_array($_SESSION[self::SESSION_KEY])) {
|
||||
$_SESSION[self::SESSION_KEY] = [];
|
||||
}
|
||||
$_SESSION[self::SESSION_KEY][$token] = $state;
|
||||
}
|
||||
|
||||
private static function clearState(string $token): void
|
||||
{
|
||||
if (!isset($_SESSION[self::SESSION_KEY]) || !is_array($_SESSION[self::SESSION_KEY])) {
|
||||
return;
|
||||
}
|
||||
unset($_SESSION[self::SESSION_KEY][$token]);
|
||||
}
|
||||
|
||||
private static function normalizeHeader(string $value): string
|
||||
private function normalizeHeader(string $value): string
|
||||
{
|
||||
$value = trim($value);
|
||||
if ($value === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$value = self::lower($value);
|
||||
$value = $this->lower($value);
|
||||
$value = str_replace(['-', '.', ' '], '_', $value);
|
||||
$value = preg_replace('/[^a-z0-9_]/', '', $value) ?? '';
|
||||
$value = preg_replace('/_+/', '_', $value) ?? '';
|
||||
@@ -571,7 +530,7 @@ class ImportService
|
||||
/**
|
||||
* @param array<int, array{row_number:int,identifier:string,code:string,message:string}> $errors
|
||||
*/
|
||||
private static function pushError(array &$errors, int $lineNumber, string $identifier, string $code, ?string $message): void
|
||||
private function pushError(array &$errors, int $lineNumber, string $identifier, string $code, ?string $message): void
|
||||
{
|
||||
if (count($errors) >= self::MAX_ERROR_ROWS) {
|
||||
return;
|
||||
@@ -580,14 +539,14 @@ class ImportService
|
||||
'row_number' => $lineNumber,
|
||||
'identifier' => $identifier,
|
||||
'code' => $code,
|
||||
'message' => $message !== null && $message !== '' ? $message : self::defaultMessageForCode($code),
|
||||
'message' => $message !== null && $message !== '' ? $message : $this->defaultMessageForCode($code),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, int> $errorCounts
|
||||
*/
|
||||
private static function incrementErrorCount(array &$errorCounts, string $code): void
|
||||
private function incrementErrorCount(array &$errorCounts, string $code): void
|
||||
{
|
||||
$code = trim($code);
|
||||
if ($code === '') {
|
||||
@@ -596,47 +555,59 @@ class ImportService
|
||||
$errorCounts[$code] = (int) ($errorCounts[$code] ?? 0) + 1;
|
||||
}
|
||||
|
||||
private static function defaultMessageForCode(string $code): string
|
||||
private function defaultMessageForCode(string $code): string
|
||||
{
|
||||
$map = [
|
||||
'upload_missing' => t('No upload file provided'),
|
||||
'upload_too_large' => t('Upload exceeds the maximum size'),
|
||||
'invalid_file_type' => t('Invalid file type'),
|
||||
'invalid_csv_header' => t('CSV header is invalid'),
|
||||
'empty_csv' => t('CSV file has no data rows'),
|
||||
'max_rows_exceeded' => t('CSV row limit exceeded'),
|
||||
'mapping_required_missing' => t('Required mapping is missing'),
|
||||
'assignment_permission_required' => t('Assignment import permission is required'),
|
||||
'invalid_email' => t('Email is invalid'),
|
||||
'duplicate_in_file' => t('Duplicate entry in CSV file'),
|
||||
'email_exists' => t('Email already exists'),
|
||||
'invalid_locale' => t('Locale is invalid'),
|
||||
'invalid_active' => t('Active value is invalid'),
|
||||
'invalid_hire_date' => t('Hire date must be YYYY-MM-DD'),
|
||||
'invalid_tenant_uuid' => t('Tenant must be a valid UUID'),
|
||||
'tenant_not_found' => t('Tenant is missing, inactive or invalid'),
|
||||
'role_not_found' => t('Role is missing, inactive or invalid'),
|
||||
'department_not_found' => t('Department is missing, inactive or invalid'),
|
||||
'department_tenant_mismatch' => t('Department does not belong to tenant'),
|
||||
'assignment_out_of_scope' => t('Tenant assignment is out of your scope'),
|
||||
'code_exists' => t('Code already exists'),
|
||||
'department_exists' => t('Department already exists for this tenant'),
|
||||
'create_failed' => t('Entry could not be created'),
|
||||
'unexpected_error' => t('Unexpected error during import'),
|
||||
'upload_missing' => $this->translate('No upload file provided'),
|
||||
'upload_too_large' => $this->translate('Upload exceeds the maximum size'),
|
||||
'invalid_file_type' => $this->translate('Invalid file type'),
|
||||
'invalid_csv_header' => $this->translate('CSV header is invalid'),
|
||||
'empty_csv' => $this->translate('CSV file has no data rows'),
|
||||
'max_rows_exceeded' => $this->translate('CSV row limit exceeded'),
|
||||
'mapping_required_missing' => $this->translate('Required mapping is missing'),
|
||||
'assignment_permission_required' => $this->translate('Assignment import permission is required'),
|
||||
'invalid_email' => $this->translate('Email is invalid'),
|
||||
'duplicate_in_file' => $this->translate('Duplicate entry in CSV file'),
|
||||
'email_exists' => $this->translate('Email already exists'),
|
||||
'invalid_locale' => $this->translate('Locale is invalid'),
|
||||
'invalid_active' => $this->translate('Active value is invalid'),
|
||||
'invalid_hire_date' => $this->translate('Hire date must be YYYY-MM-DD'),
|
||||
'invalid_tenant_uuid' => $this->translate('Tenant must be a valid UUID'),
|
||||
'tenant_not_found' => $this->translate('Tenant is missing, inactive or invalid'),
|
||||
'role_not_found' => $this->translate('Role is missing, inactive or invalid'),
|
||||
'department_not_found' => $this->translate('Department is missing, inactive or invalid'),
|
||||
'department_tenant_mismatch' => $this->translate('Department does not belong to tenant'),
|
||||
'assignment_out_of_scope' => $this->translate('Tenant assignment is out of your scope'),
|
||||
'code_exists' => $this->translate('Code already exists'),
|
||||
'department_exists' => $this->translate('Department already exists for this tenant'),
|
||||
'create_failed' => $this->translate('Entry could not be created'),
|
||||
'unexpected_error' => $this->translate('Unexpected error during import'),
|
||||
];
|
||||
return $map[$code] ?? t('Unexpected error during import');
|
||||
return $map[$code] ?? $this->translate('Unexpected error during import');
|
||||
}
|
||||
|
||||
public static function messageForCode(string $code): string
|
||||
public function messageForCode(string $code): string
|
||||
{
|
||||
return self::defaultMessageForCode(trim($code));
|
||||
return $this->defaultMessageForCode(trim($code));
|
||||
}
|
||||
|
||||
private static function lower(string $value): string
|
||||
private function lower(string $value): string
|
||||
{
|
||||
if (function_exists('mb_strtolower')) {
|
||||
return mb_strtolower($value, 'UTF-8');
|
||||
}
|
||||
return strtolower($value);
|
||||
}
|
||||
|
||||
private function translate(string $text, mixed ...$args): string
|
||||
{
|
||||
if (function_exists('t')) {
|
||||
return t($text, ...$args);
|
||||
}
|
||||
if ($args === []) {
|
||||
return $text;
|
||||
}
|
||||
return vsprintf($text, array_map(static fn ($arg) => (string) $arg, $args));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user