Files
HexaHost-GameCloud/integrations/whmcs/modules/servers/hexagamecloud/lib/ServiceMetadata.php

62 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
use WHMCS\Database\Capsule;
final class HexaGameCloudServiceMetadata
{
private const OPERATION_PREFIX = 'service:admin-meta:';
public static function load(string $serviceId): array
{
try {
$row = Capsule::table('mod_hexagamecloud_operations')
->where('operation', self::OPERATION_PREFIX . $serviceId)
->orderBy('id', 'desc')
->first();
if ($row === null || empty($row->metadata)) {
return [];
}
$decoded = json_decode((string) $row->metadata, true);
return is_array($decoded) ? $decoded : [];
} catch (Throwable) {
return [];
}
}
public static function save(string $serviceId, array $data): void
{
$payload = json_encode($data, JSON_THROW_ON_ERROR);
try {
$existing = Capsule::table('mod_hexagamecloud_operations')
->where('operation', self::OPERATION_PREFIX . $serviceId)
->orderBy('id', 'desc')
->first();
if ($existing !== null) {
Capsule::table('mod_hexagamecloud_operations')
->where('id', $existing->id)
->update([
'metadata' => $payload,
'status' => 'saved',
]);
return;
}
Capsule::table('mod_hexagamecloud_operations')->insert([
'operation' => self::OPERATION_PREFIX . $serviceId,
'status' => 'saved',
'metadata' => $payload,
'created_at' => gmdate('Y-m-d H:i:s'),
]);
} catch (Throwable $exception) {
logActivity('HexaGameCloud admin metadata save failed: ' . $exception->getMessage());
}
}
}