42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domovoy\Repositories;
|
|
|
|
use PDO;
|
|
|
|
class AuditLogRepository
|
|
{
|
|
private PDO $pdo;
|
|
|
|
public function __construct(PDO $pdo)
|
|
{
|
|
$this->pdo = $pdo;
|
|
}
|
|
|
|
public function log(string $action, ?string $entityType = null, ?string $entityId = null, ?array $details = null, ?string $userId = null): void
|
|
{
|
|
$stmt = $this->pdo->prepare(
|
|
'INSERT INTO audit_log (user_id, action, entity_type, entity_id, details_json, created_at)
|
|
VALUES (:user_id, :action, :entity_type, :entity_id, :details_json, :created_at)'
|
|
);
|
|
$stmt->execute([
|
|
'user_id' => $userId,
|
|
'action' => $action,
|
|
'entity_type' => $entityType,
|
|
'entity_id' => $entityId,
|
|
'details_json' => $details !== null ? json_encode($details) : null,
|
|
'created_at' => (new \DateTimeImmutable())->format('Y-m-d H:i:s'),
|
|
]);
|
|
}
|
|
|
|
public function findRecent(int $limit = 20): array
|
|
{
|
|
$stmt = $this->pdo->prepare('SELECT * FROM audit_log ORDER BY created_at DESC LIMIT :limit');
|
|
$stmt->bindValue('limit', $limit, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
return $stmt->fetchAll();
|
|
}
|
|
}
|