lthn.io/app/Mod/Names/Actions/CheckAvailability.php
Claude ca11c4ccee
refactor: extract Actions for CheckAvailability, SubmitClaim, RegisterName
CorePHP Actions pattern — single-purpose classes with static ::run().
Controller methods now delegate to Actions. Each Action validates,
executes, and returns typed results. Enables reuse from commands,
jobs, and tests without going through HTTP.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 11:24:27 +01:00

51 lines
1.2 KiB
PHP

<?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);
}
}