Skip to content

Commit

Permalink
extract checks to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
rcerljenko authored and freekmurze committed May 2, 2024
1 parent e922279 commit 1afee28
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/Tasks/Cleanup/Strategies/DefaultStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,33 @@ protected function removeBackupsOlderThan(Carbon $endDate, BackupCollection $bac

protected function removeOldBackupsUntilUsingLessThanMaximumStorage(BackupCollection $backups)
{
if (! $oldest = $backups->oldest()) {
if (! $this->shouldRemoveOldestBackup($backups)) {
return;
}

$backups->oldest()->delete();

$backups = $backups->filter(fn (Backup $backup) => $backup->exists());

$this->removeOldBackupsUntilUsingLessThanMaximumStorage($backups);
}

protected function shouldRemoveOldestBackup(BackupCollection $backups): bool
{
if (! $backups->oldest()) {
return false;
}

$maximumSize = $this->config->get('backup.cleanup.default_strategy.delete_oldest_backups_when_using_more_megabytes_than');

if ($maximumSize === null || ($backups->size() + $this->newestBackup->sizeInBytes()) <= $maximumSize * 1024 * 1024) {
return;
if ($maximumSize === null) {
return false;
}
$oldest->delete();

$backups = $backups->filter(fn (Backup $backup) => $backup->exists());
if (($backups->size() + $this->newestBackup->sizeInBytes()) <= $maximumSize * 1024 * 1024) {
return false;
}

$this->removeOldBackupsUntilUsingLessThanMaximumStorage($backups);
return true;
}
}

0 comments on commit 1afee28

Please sign in to comment.