From 1afee2869520264347eb36252d9318b68128e65a Mon Sep 17 00:00:00 2001 From: Ricardo Cerljenko Date: Thu, 2 May 2024 10:58:20 +0200 Subject: [PATCH] extract checks to separate function --- .../Cleanup/Strategies/DefaultStrategy.php | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Tasks/Cleanup/Strategies/DefaultStrategy.php b/src/Tasks/Cleanup/Strategies/DefaultStrategy.php index 4e01631c..81152de4 100644 --- a/src/Tasks/Cleanup/Strategies/DefaultStrategy.php +++ b/src/Tasks/Cleanup/Strategies/DefaultStrategy.php @@ -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; } }