Files
portal-api/app/Integrations/Whmcs/Support/DnsDomainLinker.php
T
2026-09-09 16:56:22 +07:00

54 lines
1.9 KiB
PHP

<?php
namespace App\Integrations\Whmcs\Support;
use Illuminate\Support\Facades\DB;
/**
* Ensures a WHMCS customer domain also exists in the DNS manager (dns_manager_domain) and is
* linked to the customer (dns_domain_user) — that pivot is what the customer's "Domains" panel
* in the admin actually reads (fetchCustomerDnsDomains), not module_domain.
*/
class DnsDomainLinker
{
private static ?int $canonicalServerId = null;
public function link(string $domainName, int $customerId, ?int $registrarId = null): void
{
$domainId = DB::table('dns_manager_domain')->whereRaw('LOWER(domain_name) = ?', [mb_strtolower($domainName)])->value('id');
if (! $domainId) {
$serverId = $this->canonicalServerId();
if (! $serverId) {
return;
}
$domainId = DB::table('dns_manager_domain')->insertGetId([
'domain_name' => $domainName,
'dns_manager_server_id' => $serverId,
// not a real provisioned DNS zone, just a billing placeholder created from WHMCS
'is_created' => 0,
'registrar_id' => $registrarId,
'created_at' => now(),
'updated_at' => now(),
]);
} elseif ($registrarId !== null) {
// don't clobber an existing registrar with an unresolved (null) one from a later re-sync
DB::table('dns_manager_domain')->where('id', $domainId)->update(['registrar_id' => $registrarId]);
}
DB::table('dns_domain_user')->updateOrInsert(
['domain_id' => $domainId, 'user_id' => $customerId],
['created_at' => now(), 'updated_at' => now()]
);
}
private function canonicalServerId(): ?int
{
return self::$canonicalServerId ??= DB::table('dns_manager_servers')
->orderByRaw('CASE WHEN id = 1 THEN 0 ELSE 1 END')
->orderBy('id')
->value('id');
}
}