mirror of
https://github.com/pelican-dev/panel.git
synced 2026-05-04 18:00:48 +03:00
46 lines
1.5 KiB
PHP
46 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Backups;
|
|
|
|
use App\Exceptions\Service\Backup\BackupLockedException;
|
|
use App\Extensions\BackupAdapter\BackupAdapterService;
|
|
use App\Models\Backup;
|
|
use Exception;
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Throwable;
|
|
|
|
class DeleteBackupService
|
|
{
|
|
public function __construct(private ConnectionInterface $connection, private BackupAdapterService $backupService) {}
|
|
|
|
/**
|
|
* Deletes a backup from the system. If the backup is stored in S3 a request
|
|
* will be made to delete that backup from the disk as well.
|
|
*
|
|
* @throws Throwable
|
|
*/
|
|
public function handle(Backup $backup): void
|
|
{
|
|
// If the backup is marked as failed it can still be deleted, even if locked
|
|
// since the UI doesn't allow you to unlock a failed backup in the first place.
|
|
//
|
|
// I also don't really see any reason you'd have a locked, failed backup to keep
|
|
// around. The logic that updates the backup to the failed state will also remove
|
|
// the lock, so this condition should really never happen.
|
|
if ($backup->is_locked && ($backup->is_successful && !is_null($backup->completed_at))) {
|
|
throw new BackupLockedException();
|
|
}
|
|
|
|
$schema = $this->backupService->get($backup->disk);
|
|
if (!$schema) {
|
|
throw new Exception("Backup uses unknown disk $backup->disk.");
|
|
}
|
|
|
|
$this->connection->transaction(function () use ($schema, $backup) {
|
|
$schema->deleteBackup($backup);
|
|
|
|
$backup->delete();
|
|
});
|
|
}
|
|
}
|