Files

66 lines
2.2 KiB
PHP

<?php
namespace App\Integrations\Whmcs\Transformers;
use App\Integrations\Whmcs\DTOs\CustomerDomainData;
use App\Integrations\Whmcs\Support\CustomerIdResolver;
use App\Integrations\Whmcs\Support\OrderIdResolver;
use App\Integrations\Whmcs\Support\RegistrarIdResolver;
use Illuminate\Support\Facades\DB;
class CustomerDomainTransformer
{
public function __construct(
private CustomerIdResolver $customers,
private OrderIdResolver $orders,
private RegistrarIdResolver $registrars,
) {
}
public function transform(array|object $row): CustomerDomainData
{
$row = (array) $row;
$name = trim($row['domain']);
$notes = [];
$notes[] = "WHMCS status: {$row['status']}";
$liveInDns = DB::table('dns_manager_domain')->whereRaw('LOWER(domain_name) = ?', [mb_strtolower($name)])->exists();
$status = $liveInDns ? 1 : 2;
if (! $liveInDns) {
$notes[] = 'Không tìm thấy domain này trong DNS manager (dns_manager_domain).';
}
if ($row['registrar'] && ! $this->registrars->resolve($row['registrar'])) {
$notes[] = "Registrar WHMCS chưa map: {$row['registrar']}";
}
if ($row['additionalnotes']) {
$notes[] = $row['additionalnotes'];
}
return new CustomerDomainData(
legacyId: (int) $row['id'],
customerId: $this->customers->resolveDomainOwner($name, (int) $row['userid']),
orderId: $this->orders->resolve((int) $row['orderid']) ?? 0,
name: $name,
firstPayment: (int) round((float) $row['firstpaymentamount']),
nextPayment: (int) round((float) $row['recurringamount']),
nextDueDate: $this->toTimestamp($row['nextduedate'] ?? null),
nextInvoiceDate: $this->toTimestamp($row['nextinvoicedate'] ?? null),
registrarId: $row['registrar'] ? $this->registrars->resolve($row['registrar']) : null,
status: $status,
note: implode(' | ', $notes),
);
}
private function toTimestamp(?string $date): ?int
{
if (! $date || str_starts_with($date, '0000-00-00')) {
return null;
}
return strtotime($date) ?: null;
}
}