giftcon_dev/app/Services/Admin/Log/AdminAuditLogService.php

107 lines
3.3 KiB
PHP

<?php
namespace App\Services\Admin\Log;
use App\Repositories\Admin\Log\AdminAuditLogRepository;
final class AdminAuditLogService
{
public function __construct(
private readonly AdminAuditLogRepository $repo,
) {}
public function indexData(array $query = []): array
{
$filters = [
'date_from' => $this->safeDate($query['date_from'] ?? ''),
'date_to' => $this->safeDate($query['date_to'] ?? ''),
'actor_q' => $this->safeStr($query['actor_q'] ?? '', 80),
'action' => $this->safeStr($query['action'] ?? '', 60),
'target_type' => $this->safeStr($query['target_type'] ?? '', 80),
'ip' => $this->safeStr($query['ip'] ?? '', 45),
];
// ✅ 기간 역전 방지
if ($filters['date_from'] && $filters['date_to']) {
if (strcmp($filters['date_from'], $filters['date_to']) > 0) {
[$filters['date_from'], $filters['date_to']] = [$filters['date_to'], $filters['date_from']];
}
}
$page = $this->repo->paginate($filters, 30);
return [
'page' => $page,
'items' => $page->items(),
'filters' => $filters,
];
}
public function getItem(int $id): ?array
{
$row = $this->repo->findOne($id);
if (!$row) return null;
$beforeRaw = $row['before_json'] ?? null;
$afterRaw = $row['after_json'] ?? null;
return [
'id' => (int)($row['id'] ?? 0),
'actor_admin_user_id' => (int)($row['actor_admin_user_id'] ?? 0),
'actor_email' => (string)($row['actor_email'] ?? ''),
'actor_name' => (string)($row['actor_name'] ?? ''),
'action' => (string)($row['action'] ?? ''),
'target_type' => (string)($row['target_type'] ?? ''),
'target_id' => (int)($row['target_id'] ?? 0),
'ip' => (string)($row['ip'] ?? ''),
'created_at' => (string)($row['created_at'] ?? ''),
'user_agent' => (string)($row['user_agent'] ?? ''),
// ✅ pretty 출력 (모달에서 바로 textContent로 넣기 좋게)
'before_pretty' => $this->prettyJson($beforeRaw),
'after_pretty' => $this->prettyJson($afterRaw),
// 원문도 필요하면 남겨둠
'before_raw' => $beforeRaw,
'after_raw' => $afterRaw,
];
}
private function safeStr(mixed $v, int $max): string
{
$s = trim((string)$v);
if ($s === '') return '';
if (mb_strlen($s) > $max) $s = mb_substr($s, 0, $max);
return $s;
}
private function safeDate(mixed $v): ?string
{
$s = trim((string)$v);
if ($s === '') return null;
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $s)) return null;
return $s;
}
private function prettyJson(?string $raw): string
{
$raw = $raw !== null ? trim((string)$raw) : '';
if ($raw === '') return '-';
$decoded = json_decode($raw, true);
if (!is_array($decoded) && !is_object($decoded)) {
// 깨진 JSON이면 원문 출력
return $raw;
}
return json_encode(
$decoded,
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
) ?: $raw;
}
}