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

@@ -12,9 +12,13 @@ class ImportAuditService
/**
* @var array<int, float>
*/
private static array $startedAtByRunId = [];
private array $startedAtByRunId = [];
public static function startRun(
public function __construct(private readonly ImportAuditRunRepository $importAuditRunRepository)
{
}
public function startRun(
string $profileKey,
array $mappedTargets,
?string $sourceFilename,
@@ -27,12 +31,12 @@ class ImportAuditService
}
try {
$runId = ImportAuditRunRepository::createRunning([
$runId = $this->importAuditRunRepository->createRunning([
'run_uuid' => RepoQuery::uuidV4(),
'profile_key' => $profileKey,
'status' => 'running',
'source_filename' => self::normalizeSourceFilename($sourceFilename),
'mapped_targets_csv' => self::normalizeMappedTargetsCsv($mappedTargets),
'source_filename' => $this->normalizeSourceFilename($sourceFilename),
'mapped_targets_csv' => $this->normalizeMappedTargetsCsv($mappedTargets),
'user_id' => $userId > 0 ? $userId : null,
'current_tenant_id' => ($currentTenantId ?? 0) > 0 ? (int) $currentTenantId : null,
]);
@@ -44,11 +48,11 @@ class ImportAuditService
return null;
}
self::$startedAtByRunId[(int) $runId] = microtime(true);
$this->startedAtByRunId[(int) $runId] = microtime(true);
return (int) $runId;
}
public static function finishRun(?int $runId, array $result, ?string $forcedStatus = null): void
public function finishRun(?int $runId, array $result, ?string $forcedStatus = null): void
{
$runId = (int) ($runId ?? 0);
if ($runId <= 0) {
@@ -60,7 +64,7 @@ class ImportAuditService
$skippedCount = max(0, (int) ($result['skipped'] ?? 0));
$failedCount = max(0, (int) ($result['failed'] ?? 0));
$status = self::normalizeStatus($forcedStatus);
$status = $this->normalizeStatus($forcedStatus);
if ($status === null) {
if (array_key_exists('ok', $result) && !($result['ok'] ?? false)) {
$status = 'failed';
@@ -74,18 +78,18 @@ class ImportAuditService
}
$durationMs = null;
if (isset(self::$startedAtByRunId[$runId])) {
$durationMs = (int) round((microtime(true) - self::$startedAtByRunId[$runId]) * 1000);
if (isset($this->startedAtByRunId[$runId])) {
$durationMs = (int) round((microtime(true) - $this->startedAtByRunId[$runId]) * 1000);
if ($durationMs < 0) {
$durationMs = 0;
}
unset(self::$startedAtByRunId[$runId]);
unset($this->startedAtByRunId[$runId]);
}
$errorCodesJson = self::encodeErrorCounts($result);
$errorCodesJson = $this->encodeErrorCounts($result);
try {
ImportAuditRunRepository::finishById($runId, [
$this->importAuditRunRepository->finishById($runId, [
'status' => $status,
'rows_total' => $rowsTotal,
'created_count' => $createdCount,
@@ -99,25 +103,25 @@ class ImportAuditService
}
}
public static function listPaged(array $filters): array
public function listPaged(array $filters): array
{
return ImportAuditRunRepository::listPaged($filters);
return $this->importAuditRunRepository->listPaged($filters);
}
public static function find(int $id): ?array
public function find(int $id): ?array
{
return ImportAuditRunRepository::find($id);
return $this->importAuditRunRepository->find($id);
}
public static function purgeExpired(): int
public function purgeExpired(): int
{
return ImportAuditRunRepository::purgeOlderThanDays(self::RETENTION_DAYS);
return $this->importAuditRunRepository->purgeOlderThanDays(self::RETENTION_DAYS);
}
/**
* @param array<int, string> $mappedTargets
*/
private static function normalizeMappedTargetsCsv(array $mappedTargets): ?string
private function normalizeMappedTargetsCsv(array $mappedTargets): ?string
{
$normalized = [];
foreach ($mappedTargets as $target) {
@@ -135,7 +139,7 @@ class ImportAuditService
return implode(',', $list);
}
private static function normalizeSourceFilename(?string $sourceFilename): ?string
private function normalizeSourceFilename(?string $sourceFilename): ?string
{
$sourceFilename = trim((string) ($sourceFilename ?? ''));
if ($sourceFilename === '') {
@@ -150,7 +154,7 @@ class ImportAuditService
return strlen($base) > 255 ? substr($base, 0, 255) : $base;
}
private static function normalizeStatus(?string $status): ?string
private function normalizeStatus(?string $status): ?string
{
$status = strtolower(trim((string) ($status ?? '')));
if ($status === '') {
@@ -159,7 +163,7 @@ class ImportAuditService
return in_array($status, ['running', 'success', 'partial', 'failed'], true) ? $status : null;
}
private static function encodeErrorCounts(array $result): ?string
private function encodeErrorCounts(array $result): ?string
{
$counts = [];
@@ -200,3 +204,4 @@ class ImportAuditService
return is_string($encoded) ? $encoded : null;
}
}