Files
wordpress-theme-aksrt/src/AKSRT/Identity/EventRepository.php
T

55 lines
2.1 KiB
PHP

<?php
/**
* Identity verification event persistence.
*
* @package AKSRT
*/
namespace AKSRT\Identity;
defined('ABSPATH') || exit;
final class EventRepository {
private string $table;
public function __construct() {
$this->table = \AKSRT\Core\DBMigrator::table('identity_event');
}
/** @param array<string,mixed> $event */
public function record(array $event): bool {
global $wpdb;
$summary = is_array($event['summary'] ?? null) ? $event['summary'] : [];
$row = [
'application_id' => (int) ($event['application_id'] ?? 0),
'event_uuid' => wp_generate_uuid4(),
'source' => sanitize_key((string) ($event['source'] ?? 'system')),
'event_type' => sanitize_key((string) ($event['event_type'] ?? 'status_changed')),
'external_event_id' => sanitize_text_field((string) ($event['external_event_id'] ?? '')),
'dedupe_key' => (string) ($event['dedupe_key'] ?? hash('sha256', wp_generate_uuid4())),
'from_status' => sanitize_key((string) ($event['from_status'] ?? '')),
'to_status' => sanitize_key((string) ($event['to_status'] ?? '')),
'result_code' => sanitize_text_field((string) ($event['result_code'] ?? '')),
'summary_json' => $summary !== [] ? wp_json_encode($summary) : null,
'actor_user_id' => !empty($event['actor_user_id']) ? (int) $event['actor_user_id'] : null,
'created_at' => current_time('mysql', true),
];
$inserted = $wpdb->insert($this->table, $row);
if ($inserted === 1) {
return true;
}
return str_contains(strtolower((string) $wpdb->last_error), 'duplicate');
}
/** @return array<int,array<string,mixed>> */
public function for_application(int $application_id): array {
global $wpdb;
$rows = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM {$this->table} WHERE application_id = %d ORDER BY id DESC",
$application_id
), ARRAY_A);
return is_array($rows) ? $rows : [];
}
}