Files
panel-pelican-dev/app/Services/Backups/DeleteBackupService.php
2026-01-20 09:45:35 +01:00

46 lines
1.6 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 readonly ConnectionInterface $connection, private readonly 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->backupHost->schema);
if (!$schema) {
throw new Exception('Backup has unknown backup adapter.');
}
$this->connection->transaction(function () use ($schema, $backup) {
$schema->deleteBackup($backup);
$backup->delete();
});
}
}