93 lines
2.7 KiB
PHP
93 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Vito\Plugins\LiittleCookie\VitoTechnitiumDns\Actions;
|
|
|
|
use App\Vito\Plugins\LiittleCookie\VitoTechnitiumDns\Services\TechnitiumClient;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Throwable;
|
|
|
|
class ListZones
|
|
{
|
|
public function __construct(private readonly TechnitiumClient $client) {}
|
|
|
|
/**
|
|
* @param array<int, string> $allowedZones
|
|
* @return array<int, array<string, mixed>>
|
|
*/
|
|
public function list(array $allowedZones = []): array
|
|
{
|
|
try {
|
|
$zones = $this->fetchAll();
|
|
|
|
return collect($zones)
|
|
// Only show Primary zones (authoritative zones the user actually manages)
|
|
->filter(fn (array $zone) => ($zone['type'] ?? '') === 'Primary')
|
|
// Exclude internal zones
|
|
->reject(fn (array $zone) => ($zone['internal'] ?? false) === true)
|
|
// Apply optional zone filter
|
|
->when(count($allowedZones) > 0, fn ($collection) => $collection->filter(
|
|
fn (array $zone) => in_array($zone['name'], $allowedZones)
|
|
))
|
|
->map(fn (array $zone) => self::mapZone($zone))
|
|
->values()
|
|
->toArray();
|
|
} catch (Throwable $e) {
|
|
Log::error('Technitium getDomains exception', ['error' => $e->getMessage()]);
|
|
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function find(string $domainId): array
|
|
{
|
|
try {
|
|
$zones = $this->fetchAll();
|
|
|
|
$zone = collect($zones)->first(
|
|
fn (array $zone) => $zone['name'] === $domainId
|
|
);
|
|
|
|
return $zone ? self::mapZone($zone) : [];
|
|
} catch (Throwable $e) {
|
|
Log::error('Technitium getDomain exception', ['error' => $e->getMessage()]);
|
|
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array<string, mixed>>
|
|
*/
|
|
private function fetchAll(): array
|
|
{
|
|
$response = $this->client->get('zones/list');
|
|
|
|
if (! $this->client->isSuccessful($response)) {
|
|
Log::error('Failed to fetch Technitium zones', ['response' => $response->json()]);
|
|
|
|
return [];
|
|
}
|
|
|
|
return $this->client->responseData($response, 'zones') ?? [];
|
|
}
|
|
|
|
/**
|
|
* Map a Technitium zone to the format Vito expects.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
private static function mapZone(array $zone): array
|
|
{
|
|
return [
|
|
'id' => $zone['name'],
|
|
'name' => $zone['name'],
|
|
'status' => ($zone['disabled'] ?? false) ? 'disabled' : 'active',
|
|
'created_on' => null,
|
|
'modified_on' => null,
|
|
];
|
|
}
|
|
}
|