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.
112 lines
3.7 KiB
PHP
112 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudTemplateImportIntent;
|
|
|
|
/** The one executable state definition for cloud-template import intents. */
|
|
final class CloudTemplateImportStateMachine
|
|
{
|
|
/** @var array<string,list<string>> */
|
|
private const TRANSITIONS = [
|
|
'prepared' => ['uploading', 'fetching', 'cancelled'],
|
|
'uploading' => [
|
|
'source_verifying', 'upload_failed', 'source_unknown',
|
|
'cancel_pending',
|
|
],
|
|
'fetching' => [
|
|
'source_verifying', 'source_failed', 'source_unknown',
|
|
'cancelled',
|
|
],
|
|
'source_verifying' => [
|
|
'source_ready', 'upload_failed', 'source_failed',
|
|
'source_unknown',
|
|
],
|
|
'source_ready' => ['distributing', 'cancel_pending'],
|
|
'distributing' => ['ready', 'distribution_failed'],
|
|
'upload_failed' => ['uploading', 'cancel_pending', 'cancelled'],
|
|
'source_failed' => ['uploading', 'fetching', 'cancelled'],
|
|
'source_unknown' => ['source_verifying', 'cancel_pending'],
|
|
'distribution_failed' => ['distributing', 'cancel_pending'],
|
|
'cancel_pending' => ['cancelled', 'source_unknown'],
|
|
'ready' => [],
|
|
'cancelled' => [],
|
|
];
|
|
|
|
/** @return list<string> */
|
|
public static function states(): array
|
|
{
|
|
self::assertIntegrity();
|
|
return array_keys(self::TRANSITIONS);
|
|
}
|
|
|
|
public static function move(
|
|
CloudTemplateImportIntent $intent,
|
|
string $next
|
|
): void {
|
|
self::assertTransition((string) $intent->state, $next);
|
|
$intent->state = $next;
|
|
if (in_array($next, ['ready', 'cancelled'], true)) {
|
|
$intent->completed_at = time();
|
|
} else {
|
|
$intent->completed_at = null;
|
|
}
|
|
}
|
|
|
|
public static function assertTransition(string $current, string $next): void
|
|
{
|
|
self::assertIntegrity();
|
|
if ($current === $next && array_key_exists($current,
|
|
self::TRANSITIONS)) {
|
|
return;
|
|
}
|
|
if (!array_key_exists($current, self::TRANSITIONS)
|
|
|| !in_array($next, self::TRANSITIONS[$current], true)) {
|
|
throw new CloudError(sprintf(
|
|
'云镜像导入状态不能从 %s 变更为 %s',
|
|
$current === '' ? '(empty)' : $current,
|
|
$next === '' ? '(empty)' : $next
|
|
), 409);
|
|
}
|
|
}
|
|
|
|
public static function resumable(string $state): bool
|
|
{
|
|
return in_array($state, [
|
|
'prepared', 'uploading', 'upload_failed', 'source_unknown',
|
|
], true);
|
|
}
|
|
|
|
public static function terminal(string $state): bool
|
|
{
|
|
return in_array($state, ['ready', 'cancelled'], true);
|
|
}
|
|
|
|
public static function assertIntegrity(): void
|
|
{
|
|
foreach (self::TRANSITIONS as $state => $targets) {
|
|
if ($state === '' || count($targets) !== count(array_unique($targets))) {
|
|
throw new \LogicException(
|
|
'Cloud-template import transition registry is invalid'
|
|
);
|
|
}
|
|
foreach ($targets as $target) {
|
|
if (!array_key_exists($target, self::TRANSITIONS)) {
|
|
throw new \LogicException(sprintf(
|
|
'Cloud-template import state %s references unknown state %s',
|
|
$state,
|
|
$target
|
|
));
|
|
}
|
|
}
|
|
}
|
|
foreach (['ready', 'cancelled'] as $terminal) {
|
|
if ((self::TRANSITIONS[$terminal] ?? null) !== []) {
|
|
throw new \LogicException(
|
|
'Cloud-template import terminal state has outgoing transitions'
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|