63 lines
2.0 KiB
PHP
63 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Integrations\Whmcs\Transformers;
|
|
|
|
use App\Integrations\Whmcs\DTOs\ServiceData;
|
|
use App\Integrations\Whmcs\Support\CustomerIdResolver;
|
|
use App\Integrations\Whmcs\Support\OrderItemIdResolver;
|
|
use App\Integrations\Whmcs\Support\ProductIdResolver;
|
|
|
|
class ServiceTransformer
|
|
{
|
|
public function __construct(
|
|
private CustomerIdResolver $customers,
|
|
private ProductIdResolver $products,
|
|
private OrderItemIdResolver $orderItems,
|
|
) {
|
|
}
|
|
|
|
public function transform(array|object $row): ServiceData
|
|
{
|
|
$row = (array) $row;
|
|
$legacyId = (int) $row['id'];
|
|
|
|
return new ServiceData(
|
|
legacyId: $legacyId,
|
|
customerId: $this->customers->resolveFor('service', $row['domain'] ?: $row['id'], (int) $row['userid']),
|
|
productId: $this->products->resolve((int) $row['packageid']),
|
|
legacyProductId: (int) $row['packageid'],
|
|
orderItemId: $this->orderItems->resolve($legacyId),
|
|
serverName: $row['domain'] ?: null,
|
|
status: $this->mapStatus($row['domainstatus']),
|
|
autoRenew: $row['billingcycle'] !== 'One Time',
|
|
startedAt: $this->nullableDate($row['regdate'] ?? null),
|
|
expiresAt: $this->nullableDate($row['nextduedate'] ?? null),
|
|
cancelledAt: $this->nullableDate($row['termination_date'] ?? null),
|
|
note: $row['notes'] ?: null,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* services.status: pending, active, suspended, expired, cancelled, terminated.
|
|
*/
|
|
private function mapStatus(string $whmcsStatus): string
|
|
{
|
|
return match ($whmcsStatus) {
|
|
'Active', 'Completed' => 'active',
|
|
'Suspended' => 'suspended',
|
|
'Terminated' => 'terminated',
|
|
'Cancelled', 'Fraud' => 'cancelled',
|
|
default => 'pending',
|
|
};
|
|
}
|
|
|
|
private function nullableDate(?string $value): ?string
|
|
{
|
|
if (! $value || (int) substr($value, 0, 4) < 1970) {
|
|
return null;
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
}
|