domovoy/app/Repositories/DeviceRepository.php

126 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Domovoy\Repositories;
use Domovoy\Models\Device;
use PDO;
class DeviceRepository
{
private PDO $pdo;
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
public function findById(int $id): ?Device
{
$stmt = $this->pdo->prepare('SELECT * FROM devices WHERE id = :id');
$stmt->execute(['id' => $id]);
$row = $stmt->fetch();
return $row ? Device::fromArray($row) : null;
}
public function findAll(string $sortBy = 'name'): array
{
$allowed = ['name', 'type', 'status', 'primary_ip', 'created_at'];
$sort = in_array($sortBy, $allowed, true) ? $sortBy : 'name';
$stmt = $this->pdo->query("SELECT * FROM devices ORDER BY {$sort} ASC");
$results = [];
while ($row = $stmt->fetch()) {
$results[] = Device::fromArray($row);
}
return $results;
}
public function findByMac(string $macAddress): ?Device
{
$stmt = $this->pdo->prepare('SELECT * FROM devices WHERE mac_address = :mac');
$stmt->execute(['mac' => strtolower($macAddress)]);
$row = $stmt->fetch();
return $row ? Device::fromArray($row) : null;
}
public function findByName(string $name): ?Device
{
$stmt = $this->pdo->prepare('SELECT * FROM devices WHERE name = :name');
$stmt->execute(['name' => $name]);
$row = $stmt->fetch();
return $row ? Device::fromArray($row) : null;
}
public function getCount(): int
{
return (int)$this->pdo->query('SELECT COUNT(*) FROM devices')->fetchColumn();
}
public function getNewDiscoveriesCount(): int
{
$stmt = $this->pdo->query("SELECT COUNT(*) FROM discovered_hosts WHERE status = 'new'");
return (int)$stmt->fetchColumn();
}
public function save(Device $device): void
{
$now = (new \DateTimeImmutable())->format('Y-m-d H:i:s');
if ($device->id === null) {
$stmt = $this->pdo->prepare(
'INSERT INTO devices (name, type, description, primary_ip, mac_address, hostname,
vendor, os_name, os_version, location, importance, status, created_at, updated_at)
VALUES (:name, :type, :description, :primary_ip, :mac_address, :hostname,
:vendor, :os_name, :os_version, :location, :importance, :status, :created_at, :updated_at)'
);
$stmt->execute([
'name' => $device->name,
'type' => $device->type,
'description' => $device->description,
'primary_ip' => $device->primaryIp,
'mac_address' => $device->macAddress !== null ? strtolower($device->macAddress) : null,
'hostname' => $device->hostname,
'vendor' => $device->vendor,
'os_name' => $device->osName,
'os_version' => $device->osVersion,
'location' => $device->location,
'importance' => $device->importance,
'status' => $device->status,
'created_at' => $now,
'updated_at' => $now,
]);
$device->id = (int)$this->pdo->lastInsertId();
} else {
$stmt = $this->pdo->prepare(
'UPDATE devices SET name = :name, type = :type, description = :description,
primary_ip = :primary_ip, mac_address = :mac_address, hostname = :hostname,
vendor = :vendor, os_name = :os_name, os_version = :os_version,
location = :location, importance = :importance, status = :status,
updated_at = :updated_at WHERE id = :id'
);
$stmt->execute([
'id' => $device->id,
'name' => $device->name,
'type' => $device->type,
'description' => $device->description,
'primary_ip' => $device->primaryIp,
'mac_address' => $device->macAddress !== null ? strtolower($device->macAddress) : null,
'hostname' => $device->hostname,
'vendor' => $device->vendor,
'os_name' => $device->osName,
'os_version' => $device->osVersion,
'location' => $device->location,
'importance' => $device->importance,
'status' => $device->status,
'updated_at' => $now,
]);
}
}
public function delete(int $id): void
{
$stmt = $this->pdo->prepare('DELETE FROM devices WHERE id = :id');
$stmt->execute(['id' => $id]);
}
}