lthn.io/app/Mod/Names/Actions/CheckAvailability.php

52 lines
1.2 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Mod\Names\Actions;
use Illuminate\Support\Facades\Cache;
use Mod\Chain\Services\DaemonRpc;
/**
* Check if a .lthn name is available for registration.
*
* $result = CheckAvailability::run('mybrand');
* if ($result['available']) { ... }
*/
class CheckAvailability
{
public function __construct(
private readonly DaemonRpc $rpc,
) {}
public function handle(string $name): array
{
$name = strtolower(trim($name));
if (! preg_match('/^[a-z0-9][a-z0-9.\-]{0,254}$/', $name)) {
return [
'name' => $name,
'available' => false,
'reserved' => false,
'reason' => 'Invalid name format.',
'fqdn' => "{$name}.lthn",
];
}
$alias = $this->rpc->getAliasByName($name);
$reserved = Cache::has("name_lock:{$name}");
return [
'name' => $name,
'available' => $alias === null && ! $reserved,
'reserved' => $reserved,
'fqdn' => "{$name}.lthn",
];
}
public static function run(string $name): array
{
return app(static::class)->handle($name);
}
}