major update

This commit is contained in:
2026-03-04 15:56:58 +01:00
parent 16759a2732
commit 8f4dd5840d
478 changed files with 24313 additions and 8201 deletions

View File

@@ -2,7 +2,7 @@
namespace MintyPHP\Service\User;
use MintyPHP\DB;
use MintyPHP\Repository\Support\DatabaseSessionRepository;
use MintyPHP\Repository\Support\RepoQuery;
use MintyPHP\Repository\User\UserReadRepository;
use MintyPHP\Repository\User\UserWriteRepository;
@@ -13,7 +13,8 @@ class UserLifecycleRestoreService
public function __construct(
private readonly UserReadRepository $userReadRepository,
private readonly UserWriteRepository $userWriteRepository,
private readonly UserLifecycleAuditService $userLifecycleAuditService
private readonly UserLifecycleAuditService $userLifecycleAuditService,
private readonly DatabaseSessionRepository $databaseSessionRepository
) {
}
@@ -23,38 +24,40 @@ class UserLifecycleRestoreService
return ['ok' => false, 'error' => 'invalid_request'];
}
$db = DB::handle();
$db->begin_transaction();
$transactionStarted = false;
try {
$this->databaseSessionRepository->beginTransaction();
$transactionStarted = true;
$event = $this->userLifecycleAuditService->findDeleteEventForRestore($auditId, true);
if (!$event) {
$db->rollback();
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'audit_event_not_found'];
}
if (!empty($event['restored_at'])) {
$db->rollback();
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'audit_event_already_restored'];
}
$snapshot = $this->userLifecycleAuditService->decryptSnapshot($event);
if (!$snapshot) {
$db->rollback();
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'snapshot_unavailable'];
}
$uuid = trim((string) ($snapshot['uuid'] ?? ''));
$email = trim((string) ($snapshot['email'] ?? ''));
if ($uuid === '' || $email === '') {
$db->rollback();
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'snapshot_invalid'];
}
if ($this->userReadRepository->findByUuid($uuid)) {
$db->rollback();
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'restore_uuid_exists'];
}
if ($this->userReadRepository->findByEmail($email)) {
$db->rollback();
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'restore_email_exists'];
}
@@ -86,7 +89,7 @@ class UserLifecycleRestoreService
'active_changed_by' => $actorUserId,
]);
if (!$createdId) {
$db->rollback();
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'restore_create_failed'];
}
$restoredUserId = (int) $createdId;
@@ -97,7 +100,7 @@ class UserLifecycleRestoreService
$restoredUserId
);
if (!$marked) {
$db->rollback();
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'restore_mark_failed'];
}
@@ -118,7 +121,8 @@ class UserLifecycleRestoreService
null
);
$db->commit();
$this->databaseSessionRepository->commitTransaction();
$transactionStarted = false;
return [
'ok' => true,
'restored_user_id' => $restoredUserId,
@@ -126,15 +130,22 @@ class UserLifecycleRestoreService
'audit_id' => $auditId,
];
} catch (\Throwable $exception) {
if ($db->errno === 0) {
$db->rollback();
} else {
$db->rollback();
if ($transactionStarted) {
$this->rollbackQuietly();
}
return ['ok' => false, 'error' => 'restore_unexpected_error'];
}
}
private function rollbackQuietly(): void
{
try {
$this->databaseSessionRepository->rollbackTransaction();
} catch (\Throwable $exception) {
// no-op
}
}
private function nullableText(mixed $value): ?string
{
$value = trim((string) $value);