Files
portal-api/app/Http/Resources/Api/Client/Service/ServiceClientDetailResource.php

87 lines
3.2 KiB
PHP

<?php
namespace App\Http\Resources\Api\Client\Service;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ServiceClientDetailResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
private function resolveSpecs($vm, $product): ?array
{
if ($vm && ($vm->cpu !== null || $vm->ram_mb !== null || $vm->disk_gb !== null)) {
return [
'cpu' => (int) ($vm->cpu ?? 0),
'ram' => (int) ($vm->ram_mb ?? 0),
'disk' => (int) ($vm->disk_gb ?? 0),
'bandwidth' => (int) ($product->bandwidth ?? 0),
];
}
$addons = $this->addons ?? collect();
if ($addons->isNotEmpty()) {
return [
'cpu' => (int) $addons->where('addon_type', 'cpu')->sum('addon_value'),
'ram' => (int) $addons->where('addon_type', 'ram')->sum('addon_value'),
'disk' => (int) $addons->where('addon_type', 'disk')->sum('addon_value'),
'bandwidth' => (int) $addons->where('addon_type', 'bandwidth')->sum('addon_value'),
];
}
return $product ? [
'cpu' => (int) ($product->cpu ?? 0),
'ram' => (int) ($product->ram ?? 0),
'disk' => (int) ($product->disk ?? 0),
'bandwidth' => (int) ($product->bandwidth ?? 0),
] : null;
}
public function toArray(Request $request): array
{
if (!$this->resource) {
return [];
}
$orderItem = $this->orderItem;
$order = $orderItem?->order;
$vm = $this->resource->vmware;
$product = $orderItem?->product;
return [
'id' => $this->id,
'uuid' => $this->uuid,
'server_name' => $this->server_name,
'status' => $this->status,
'auto_renew' => $this->auto_renew,
'started_at' => $this->started_at?->toISOString(),
'expires_at' => $this->expires_at?->toISOString(),
'os' => [
'version' => $orderItem?->os?->version ?? $vm?->os?->version,
'group' => $orderItem?->os?->osGroup?->name ?? $vm?->os?->osGroup?->name,
],
'specs' => $this->resolveSpecs($vm, $product),
'order' => $order ? [
'total' => (float) $order->total,
'months' => (int) $orderItem->months,
'billing_label' => $orderItem->billing_label,
] : null,
'vm' => $vm ? [
'id' => $vm->id,
'vmware_id' => $vm->vmware_id,
'ip_address' => $vm->ipAddress?->ip ?? $vm->ip,
'hostname' => $vm->hostname,
'username' => $vm->user,
'password' => $vm->plain_password ?? null,
'port' => $vm->port,
'status' => $vm->status,
'ip' => $vm->ip ?? null,
'dns' => $vm->dns ?? null,
] : null,
];
}
}