KEA Setup
This commit is contained in:
170
src/Repository/KeaHostMetadataRepository.php
Normal file
170
src/Repository/KeaHostMetadataRepository.php
Normal file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use PDO;
|
||||
|
||||
final class KeaHostMetadataRepository
|
||||
{
|
||||
public function __construct(private PDO $pdo) {}
|
||||
|
||||
public function ensureSchema(): void
|
||||
{
|
||||
$driver = (string)$this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
|
||||
|
||||
if ($driver === 'pgsql') {
|
||||
$this->pdo->exec(
|
||||
"CREATE TABLE IF NOT EXISTS nexus_dhcp_host_meta (
|
||||
host_id BIGINT PRIMARY KEY,
|
||||
hardware_address TEXT,
|
||||
ip_address TEXT,
|
||||
real_name TEXT,
|
||||
device_name TEXT,
|
||||
owner TEXT,
|
||||
location TEXT,
|
||||
device_type TEXT,
|
||||
notes TEXT,
|
||||
tags_json JSONB,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
)"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($driver === 'sqlite') {
|
||||
$this->pdo->exec(
|
||||
"CREATE TABLE IF NOT EXISTS nexus_dhcp_host_meta (
|
||||
host_id INTEGER PRIMARY KEY,
|
||||
hardware_address TEXT,
|
||||
ip_address TEXT,
|
||||
real_name TEXT,
|
||||
device_name TEXT,
|
||||
owner TEXT,
|
||||
location TEXT,
|
||||
device_type TEXT,
|
||||
notes TEXT,
|
||||
tags_json TEXT,
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
)"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->pdo->exec(
|
||||
"CREATE TABLE IF NOT EXISTS nexus_dhcp_host_meta (
|
||||
host_id BIGINT PRIMARY KEY,
|
||||
hardware_address TEXT,
|
||||
ip_address TEXT,
|
||||
real_name TEXT,
|
||||
device_name TEXT,
|
||||
owner TEXT,
|
||||
location TEXT,
|
||||
device_type TEXT,
|
||||
notes TEXT,
|
||||
tags_json TEXT,
|
||||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)"
|
||||
);
|
||||
}
|
||||
|
||||
public function findByHostIds(array $hostIds): array
|
||||
{
|
||||
$hostIds = array_values(array_unique(array_filter(array_map('intval', $hostIds))));
|
||||
if ($hostIds === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$params = [];
|
||||
$placeholders = [];
|
||||
foreach ($hostIds as $idx => $hostId) {
|
||||
$key = ':host_id_' . $idx;
|
||||
$placeholders[] = $key;
|
||||
$params[$key] = $hostId;
|
||||
}
|
||||
|
||||
$stmt = $this->pdo->prepare(
|
||||
'SELECT host_id, hardware_address, ip_address, real_name, device_name, owner, location, device_type, notes, tags_json, updated_at
|
||||
FROM nexus_dhcp_host_meta
|
||||
WHERE host_id IN (' . implode(', ', $placeholders) . ')'
|
||||
);
|
||||
foreach ($params as $key => $value) {
|
||||
$stmt->bindValue($key, $value, PDO::PARAM_INT);
|
||||
}
|
||||
$stmt->execute();
|
||||
|
||||
$items = [];
|
||||
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
|
||||
$items[(int)$row['host_id']] = $row;
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
public function saveForHost(int $hostId, string $hardwareAddress, string $ipAddress, array $metadata): void
|
||||
{
|
||||
$metadata = array_merge([
|
||||
'real_name' => null,
|
||||
'device_name' => null,
|
||||
'owner' => null,
|
||||
'location' => null,
|
||||
'device_type' => null,
|
||||
'notes' => null,
|
||||
'tags' => [],
|
||||
], $metadata);
|
||||
|
||||
$tagsJson = json_encode($metadata['tags'], JSON_UNESCAPED_UNICODE);
|
||||
if ($tagsJson === false) {
|
||||
$tagsJson = '[]';
|
||||
}
|
||||
|
||||
$driver = (string)$this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
|
||||
if ($driver === 'pgsql') {
|
||||
$stmt = $this->pdo->prepare(
|
||||
"INSERT INTO nexus_dhcp_host_meta (
|
||||
host_id, hardware_address, ip_address, real_name, device_name, owner, location, device_type, notes, tags_json, updated_at
|
||||
) VALUES (
|
||||
:host_id, :hardware_address, :ip_address, :real_name, :device_name, :owner, :location, :device_type, :notes, CAST(:tags_json AS jsonb), NOW()
|
||||
)
|
||||
ON CONFLICT (host_id) DO UPDATE SET
|
||||
hardware_address = EXCLUDED.hardware_address,
|
||||
ip_address = EXCLUDED.ip_address,
|
||||
real_name = EXCLUDED.real_name,
|
||||
device_name = EXCLUDED.device_name,
|
||||
owner = EXCLUDED.owner,
|
||||
location = EXCLUDED.location,
|
||||
device_type = EXCLUDED.device_type,
|
||||
notes = EXCLUDED.notes,
|
||||
tags_json = EXCLUDED.tags_json,
|
||||
updated_at = NOW()"
|
||||
);
|
||||
} else {
|
||||
$stmt = $this->pdo->prepare(
|
||||
"REPLACE INTO nexus_dhcp_host_meta (
|
||||
host_id, hardware_address, ip_address, real_name, device_name, owner, location, device_type, notes, tags_json, updated_at
|
||||
) VALUES (
|
||||
:host_id, :hardware_address, :ip_address, :real_name, :device_name, :owner, :location, :device_type, :notes, :tags_json, CURRENT_TIMESTAMP
|
||||
)"
|
||||
);
|
||||
}
|
||||
|
||||
$stmt->execute([
|
||||
'host_id' => $hostId,
|
||||
'hardware_address' => $hardwareAddress,
|
||||
'ip_address' => $ipAddress,
|
||||
'real_name' => $this->nullableString($metadata['real_name']),
|
||||
'device_name' => $this->nullableString($metadata['device_name']),
|
||||
'owner' => $this->nullableString($metadata['owner']),
|
||||
'location' => $this->nullableString($metadata['location']),
|
||||
'device_type' => $this->nullableString($metadata['device_type']),
|
||||
'notes' => $this->nullableString($metadata['notes']),
|
||||
'tags_json' => $tagsJson,
|
||||
]);
|
||||
}
|
||||
|
||||
private function nullableString(mixed $value): ?string
|
||||
{
|
||||
$value = trim((string)$value);
|
||||
return $value !== '' ? $value : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user