50 lines
1.7 KiB
PHP
50 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Integrations\Domain\Writers;
|
|
|
|
use App\Integrations\Domain\DTOs\DnsDomainData;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DnsDomainWriter
|
|
{
|
|
/**
|
|
* Skips domains whose server couldn't be resolved — dns_manager_domain.dns_manager_server_id is NOT NULL.
|
|
*/
|
|
public function write(DnsDomainData $data): bool
|
|
{
|
|
if ($data->dnsManagerServerId === null) {
|
|
return false;
|
|
}
|
|
|
|
$attributes = [
|
|
'legacy_id' => $data->legacyId,
|
|
'domain_name' => $data->domainName,
|
|
'dns_manager_server_id' => $data->dnsManagerServerId,
|
|
'is_created' => $data->isCreated,
|
|
'plesk_id' => $data->pleskId,
|
|
'created_at' => $data->createdAt,
|
|
'updated_at' => $data->updatedAt,
|
|
];
|
|
|
|
$existingId = DB::table('dns_manager_domain')->where('legacy_id', $data->legacyId)->value('id');
|
|
|
|
// A WHMCS-created placeholder (DnsDomainLinker) has legacy_id NULL and the same
|
|
// domain_name — adopt it instead of inserting a duplicate row that would strand
|
|
// dns_domain_user pointing at the empty placeholder while NS records attach here.
|
|
if (! $existingId && $data->domainName !== null) {
|
|
$existingId = DB::table('dns_manager_domain')
|
|
->whereNull('legacy_id')
|
|
->whereRaw('LOWER(domain_name) = ?', [mb_strtolower($data->domainName)])
|
|
->value('id');
|
|
}
|
|
|
|
if ($existingId) {
|
|
DB::table('dns_manager_domain')->where('id', $existingId)->update($attributes);
|
|
} else {
|
|
DB::table('dns_manager_domain')->insert($attributes);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|