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.
156 lines
4.9 KiB
PHP
156 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudBackupPeriodEntitlement;
|
|
use App\Models\CloudBackupSubscription;
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudInstanceBackup;
|
|
|
|
/**
|
|
* One durable eligibility contract for restoring an automatic backup over its
|
|
* original business instance. The instance id is the stable identity across a
|
|
* maintenance migration; source_uuid remains immutable provenance only.
|
|
*/
|
|
final class CloudOriginalBackupRestorePolicy
|
|
{
|
|
/**
|
|
* @return array{
|
|
* eligible:bool,
|
|
* reason:string,
|
|
* message:string,
|
|
* http_status:int
|
|
* }
|
|
*/
|
|
public static function evaluate(?CloudInstance $instance,
|
|
CloudInstanceBackup $backup, ?int $now = null): array
|
|
{
|
|
$now ??= time();
|
|
if ($instance === null) {
|
|
return self::denied(
|
|
'instance_missing', '云备份对应的原实例不存在', 404
|
|
);
|
|
}
|
|
if ((int) $backup->user_id !== (int) $instance->user_id
|
|
|| (int) $backup->source_instance_id !== (int) $instance->id) {
|
|
return self::denied(
|
|
'backup_instance_mismatch',
|
|
'云备份不属于该实例的自动备份',
|
|
404
|
|
);
|
|
}
|
|
if ((string) $backup->purpose !== 'portable'
|
|
|| (string) $backup->source !== 'automatic'
|
|
|| $backup->subscription_id === null) {
|
|
return self::denied(
|
|
'backup_type_mismatch',
|
|
'云备份不属于该实例的自动备份',
|
|
404
|
|
);
|
|
}
|
|
|
|
$subscription = CloudBackupSubscription::where(
|
|
'id', (int) $backup->subscription_id
|
|
)->first();
|
|
if ($subscription === null
|
|
|| (int) $subscription->instance_id !== (int) $instance->id
|
|
|| (int) $subscription->user_id !== (int) $instance->user_id) {
|
|
return self::denied(
|
|
'subscription_mismatch',
|
|
'云备份订阅账本不一致',
|
|
409
|
|
);
|
|
}
|
|
if ((string) $instance->status !== 'active') {
|
|
return self::denied(
|
|
'instance_unavailable',
|
|
'只有活动实例可以恢复云备份',
|
|
409
|
|
);
|
|
}
|
|
if ((string) $instance->service_status !== 'active'
|
|
|| CloudAutomaticBackupService::shouldPauseForInstanceExpiry(
|
|
$instance, $now
|
|
)) {
|
|
return self::denied(
|
|
'instance_service_expired',
|
|
'实例服务已到期,请续费后再恢复云备份',
|
|
409
|
|
);
|
|
}
|
|
if (!self::withinRecoveryWindow($backup, $now)) {
|
|
return self::denied(
|
|
'recovery_window_expired',
|
|
'自动备份恢复宽限期已结束',
|
|
409
|
|
);
|
|
}
|
|
if ((string) $backup->state !== 'ready'
|
|
|| (int) $backup->object_size_bytes < 1
|
|
|| preg_match('/^[a-f0-9]{64}$/D',
|
|
(string) $backup->sha256) !== 1) {
|
|
return self::denied(
|
|
'backup_not_ready',
|
|
'云备份尚未处于可恢复状态',
|
|
409
|
|
);
|
|
}
|
|
return [
|
|
'eligible' => true,
|
|
'reason' => 'eligible',
|
|
'message' => '',
|
|
'http_status' => 200,
|
|
];
|
|
}
|
|
|
|
public static function assertEligible(CloudInstance $instance,
|
|
CloudInstanceBackup $backup, ?int $now = null): void
|
|
{
|
|
$decision = self::evaluate($instance, $backup, $now);
|
|
if (!$decision['eligible']) {
|
|
throw new CloudError(
|
|
$decision['message'], $decision['http_status']
|
|
);
|
|
}
|
|
}
|
|
|
|
/** A paid period remains recoverable until three days after its end. */
|
|
public static function withinRecoveryWindow(CloudInstanceBackup $backup,
|
|
?int $now = null): bool
|
|
{
|
|
if ((string) $backup->source !== 'automatic'
|
|
|| $backup->subscription_id === null) {
|
|
return false;
|
|
}
|
|
$paidUntil = CloudBackupPeriodEntitlement::where(
|
|
'subscription_id', (int) $backup->subscription_id
|
|
)->where('instance_id', (int) $backup->source_instance_id)
|
|
->where('user_id', (int) $backup->user_id)
|
|
->max('ends_at');
|
|
if ($paidUntil === null) {
|
|
return false;
|
|
}
|
|
return (int) $paidUntil + CloudInvoiceService::PAYMENT_GRACE_SECONDS
|
|
> ($now ?? time());
|
|
}
|
|
|
|
/**
|
|
* @return array{
|
|
* eligible:false,
|
|
* reason:string,
|
|
* message:string,
|
|
* http_status:int
|
|
* }
|
|
*/
|
|
private static function denied(string $reason, string $message,
|
|
int $httpStatus): array
|
|
{
|
|
return [
|
|
'eligible' => false,
|
|
'reason' => $reason,
|
|
'message' => $message,
|
|
'http_status' => $httpStatus,
|
|
];
|
|
}
|
|
}
|