Files
portal-api/app/Services/VMHelperService.php
T
2025-07-29 10:24:14 +07:00

117 lines
3.0 KiB
PHP

<?php
namespace App\Services;
use Carbon\Carbon;
use Illuminate\Support\Str;
class VMHelperService
{
/**
* Get VM status display information
*/
public static function getVmStatusInfo($vmInfo): array
{
$status = $vmInfo['status'] ?? null;
return [
'status' => $status,
'isPoweredOn' => $status === 'POWERED_ON',
'isPoweredOff' => $status === 'POWERED_OFF',
'displayStatus' => self::getDisplayStatus($status),
'statusClass' => self::getStatusClass($status)
];
}
/**
* Get display status text
*/
private static function getDisplayStatus(?string $status): string
{
return match($status) {
'POWERED_ON' => __('RUNNING'),
'POWERED_OFF' => __('STOPPED'),
default => __('UNKNOWN')
};
}
/**
* Get status CSS class
*/
private static function getStatusClass(?string $status): string
{
return match($status) {
'POWERED_ON' => 'bg-green-500 text-white',
'POWERED_OFF' => 'bg-red-500 text-white',
default => 'bg-gray-500 text-white'
};
}
/**
* Get VM connection information
*/
public static function getVmConnectionInfo($vmInfo, $invoice): array
{
$isWindows = self::isWindowsOS($vmInfo, $invoice);
return [
'ip' => $vmInfo['ip'] ?? '103.42.56.177',
'port' => $isWindows ? '3389' : '22',
'user' => $isWindows ? 'administrator' : 'root',
'isWindows' => $isWindows
];
}
/**
* Check if OS is Windows
*/
private static function isWindowsOS($vmInfo, $invoice): bool
{
$vmOs = Str::lower($vmInfo['os'] ?? '');
$invoiceOs = Str::lower($invoice->invoiceItem->first()->os->osGroup->name ?? '');
return Str::contains($vmOs, 'windows') || Str::contains($invoiceOs, 'windows');
}
/**
* Get VM specifications
*/
public static function getVmSpecs($vmInfo): array
{
return [
'cpu' => $vmInfo['cpu'] ?? '3',
'ram' => $vmInfo['ram'] ?? '1 GB',
'disk' => $vmInfo['disk'] ?? '25 GB'
];
}
/**
* Format date for display
*/
public static function formatDate($date): string
{
return $date ? Carbon::parse($date)->format('d/m/Y') : 'N/A';
}
/**
* Format currency amount
*/
public static function formatCurrency($amount): string
{
return number_format($amount ?? 0, 0, ',', '.') . ' đ';
}
/**
* Get service status information
*/
public static function getServiceStatus($invoice): array
{
$isExpired = $invoice->status == 1;
return [
'isExpired' => $isExpired,
'statusText' => $isExpired ? __('Hết hạn') : __('Hoạt động'),
'statusClass' => $isExpired ? 'bg-red-100 text-red-800' : 'bg-green-100 text-green-800'
];
}
}