You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.7 KiB
PHP
53 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudHost;
|
|
use App\Models\CloudHostResourcePoolMembership;
|
|
|
|
/** Administrator host DTOs backed by the authoritative pool membership. */
|
|
final class CloudAdminHostProjectionService
|
|
{
|
|
private const SCHEDULER_STATES = ['active', 'draining', 'disabled'];
|
|
|
|
public static function all(): array
|
|
{
|
|
$hosts = CloudHost::orderBy('id')->get();
|
|
if ($hosts->isEmpty()) {
|
|
return [];
|
|
}
|
|
|
|
$memberships = CloudHostResourcePoolMembership::whereIn(
|
|
'host_id', $hosts->pluck('id')->all()
|
|
)->orderBy('host_id')->orderBy('resource_pool_id')->orderBy('id')->get();
|
|
$byHost = [];
|
|
foreach ($memberships as $membership) {
|
|
$byHost[(int) $membership->host_id][] = $membership;
|
|
}
|
|
|
|
return $hosts->map(static fn (CloudHost $host): array => self::dto(
|
|
$host, $byHost[(int) $host->id] ?? []
|
|
))->values()->all();
|
|
}
|
|
|
|
public static function one(CloudHost $host): array
|
|
{
|
|
$memberships = CloudHostResourcePoolMembership::where(
|
|
'host_id', (int) $host->id
|
|
)->orderBy('resource_pool_id')->orderBy('id')->get();
|
|
return self::dto($host, $memberships);
|
|
}
|
|
|
|
private static function dto(CloudHost $host, iterable $memberships): array
|
|
{
|
|
$selected = CloudHostPoolMembershipService::selected($memberships);
|
|
$state = (string) ($selected?->scheduler_state ?? 'disabled');
|
|
$dto = $host->toArray();
|
|
$dto['resource_pool_id'] = $selected === null
|
|
? null : (int) $selected->resource_pool_id;
|
|
$dto['scheduler_state'] = in_array($state, self::SCHEDULER_STATES, true)
|
|
? $state : 'disabled';
|
|
return $dto;
|
|
}
|
|
}
|