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.
virt/web/app/Models/CloudBackupRestoreAttempt.php

65 lines
2.0 KiB
PHP

<?php
namespace App\Models;
/** One immutable dispatch generation for an independent backup restore. */
final class CloudBackupRestoreAttempt extends Model
{
protected $connection = 'default';
protected $table = 'cloud_backup_restore_attempts';
protected $casts = [
'id' => 'int',
'backup_restore_id' => 'int',
'generation' => 'int',
'job_id' => 'int',
'reservation_id' => 'int',
'target_host_id' => 'int',
'target_network_attachment_id' => 'int',
'active_attempt' => 'int',
'evidence_recorded_at' => 'int',
'effect_resolved_at' => 'int',
'terminal_at' => 'int',
'created_at' => 'int',
'updated_at' => 'int',
];
public function restore()
{
return $this->belongsTo(CloudBackupRestore::class, 'backup_restore_id');
}
public function boundReservationQuery()
{
$reservationId = (int) ($this->reservation_id ?? 0);
$attemptKey = (string) ($this->attempt_key ?? '');
if ($reservationId < 1
|| preg_match('/^[a-f0-9]{32}$/D', $attemptKey) !== 1) {
throw new \LogicException(
'Restore attempt reservation identity is incomplete.'
);
}
return CloudResourceReservation::query()
->where('id', $reservationId)
->where('backup_restore_attempt_key', $attemptKey);
}
public function job()
{
return $this->belongsTo(CloudJob::class, 'job_id');
}
public function matchesReservation(
CloudResourceReservation $reservation
): bool {
$attemptKey = (string) ($this->attempt_key ?? '');
return (int) ($this->reservation_id ?? 0) > 0
&& (int) $this->reservation_id === (int) ($reservation->id ?? 0)
&& preg_match('/^[a-f0-9]{32}$/D', $attemptKey) === 1
&& hash_equals(
$attemptKey,
(string) ($reservation->backup_restore_attempt_key ?? '')
);
}
}