From 22761f14ddc8555ec8cf5be0d9904b972c5de474 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Fri, 2 Aug 2024 15:16:43 +0200 Subject: [PATCH] Refactor/simplify BackupsCheck + add metadata --- src/Checks/Checks/BackupsCheck.php | 93 +++++++++++++++--------------- src/Checks/Result.php | 7 +++ 2 files changed, 54 insertions(+), 46 deletions(-) diff --git a/src/Checks/Checks/BackupsCheck.php b/src/Checks/Checks/BackupsCheck.php index 28c8ec8e..2fda45b7 100644 --- a/src/Checks/Checks/BackupsCheck.php +++ b/src/Checks/Checks/BackupsCheck.php @@ -48,7 +48,7 @@ public function onDisk($disk) public function parseModifiedFormat($parseModifiedFormat = 'Y-m-d_H-i-s'): self { - $this->parseModifiedUsing = $parseModifiedFormat; + $this->parseModifiedUsing = $parseModifiedFormat; return $this; } @@ -91,64 +91,65 @@ public function numberOfBackups(?int $min = null, ?int $max = null): self public function run(): Result { - $files = collect($this->disk ? $files = $this->disk->files($this->locatedAt) : File::glob($this->locatedAt)); + $eligibleBackups = $this->getBackupFiles(); - if ($files->isEmpty()) { - return Result::make()->failed('No backups found'); - } + $backupCount = $eligibleBackups->count(); - $eligableBackups = $files - ->map(function (string $path) { - return new BackupFile($path, $this->disk, $this->parseModifiedUsing); - }); + $result = Result::make()->meta([ + 'minimum_size' => $this->minimumSizeInMegabytes.'MB', + 'backup_count' => $backupCount, + ]); - if ($this->minimumNumberOfBackups) { - if ($eligableBackups->count() < $this->minimumNumberOfBackups) { - return Result::make()->failed('Not enough backups found'); - } + if ($backupCount === 0) { + return $result->failed('No backups found'); } - if ($this->maximumNumberOfBackups) { - if ($eligableBackups->count() > $this->maximumNumberOfBackups) { - return Result::make()->failed('Too many backups found'); - } + if ($this->minimumNumberOfBackups && $backupCount < $this->minimumNumberOfBackups) { + return $result->failed('Not enough backups found'); } - $youngestBackup = $this->getYoungestBackup($eligableBackups); - - if ($this->youngestShouldHaveBeenMadeBefore) { - if (!$youngestBackup || $this->youngestBackupIsToolOld($youngestBackup)) { - return Result::make() - ->failed('Youngest backup was too old'); - } + if ($this->maximumNumberOfBackups && $backupCount > $this->maximumNumberOfBackups) { + return $result->failed('Too many backups found'); } - $oldestBackup = $this->getOldestBackup($eligableBackups); + $youngestBackup = $this->getYoungestBackup($eligibleBackups); + $oldestBackup = $this->getOldestBackup($eligibleBackups); - if ($this->oldestShouldHaveBeenMadeAfter) { - if (!$oldestBackup || $this->oldestBackupIsTooYoung($oldestBackup)) { - return Result::make() - ->failed('Oldest backup was too young'); - } - } + $result->appendMeta([ + 'youngest_backup' => Carbon::createFromTimestamp($youngestBackup?->lastModified())->toDateTimeString(), + 'oldest_backup' => Carbon::createFromTimestamp($oldestBackup?->lastModified())->toDateTimeString(), + ]); - if ($eligableBackups->isEmpty()) { - return Result::make()->failed('Backups are not large enough'); + if ($this->youngestShouldHaveBeenMadeBefore && $this->youngestBackupIsToolOld($youngestBackup)) { + return $result->failed('The youngest backup was too old'); } - if ($this->onlyCheckSizeOnFirstAndLast) { - $eligableBackups = collect([$youngestBackup, $oldestBackup]); + if ($this->oldestShouldHaveBeenMadeAfter && $this->oldestBackupIsTooYoung($oldestBackup)) { + return $result->failed('The oldest backup was too young'); } - if ( - $eligableBackups->filter(function (BackupFile $file) { - return $file->size() >= $this->minimumSizeInMegabytes * 1024 * 1024; - })->isEmpty() - ) { - return Result::make()->failed('Backups are not large enough'); + $backupsToCheckSizeOn = $this->onlyCheckSizeOnFirstAndLast + ? collect([$youngestBackup, $oldestBackup]) + : $eligibleBackups; + + if ($backupsToCheckSizeOn->filter( + fn(BackupFile $file) => $file->size() >= $this->minimumSizeInMegabytes * 1024 * 1024 + )->isEmpty()) { + return $result->failed('Backups are not large enough'); } - return Result::make()->ok(); + return $result->ok(); + } + + protected function getBackupFiles(): Collection + { + return collect( + $this->disk + ? $this->disk->files($this->locatedAt) + : File::glob($this->locatedAt) + )->map(function (string $path) { + return new BackupFile($path, $this->disk, $this->parseModifiedUsing); + }); } protected function getYoungestBackup(Collection $backups): ?BackupFile @@ -158,11 +159,11 @@ protected function getYoungestBackup(Collection $backups): ?BackupFile ->first(); } - protected function youngestBackupIsToolOld(BackupFile $youngestBackup): bool + protected function youngestBackupIsToolOld(?BackupFile $youngestBackup): bool { $threshold = $this->youngestShouldHaveBeenMadeBefore->getTimestamp(); - return $youngestBackup->lastModified() <= $threshold; + return !$youngestBackup || $youngestBackup->lastModified() <= $threshold; } protected function getOldestBackup(Collection $backups): ?BackupFile @@ -172,10 +173,10 @@ protected function getOldestBackup(Collection $backups): ?BackupFile ->first(); } - protected function oldestBackupIsTooYoung(BackupFile $oldestBackup): bool + protected function oldestBackupIsTooYoung(?BackupFile $oldestBackup): bool { $threshold = $this->oldestShouldHaveBeenMadeAfter->getTimestamp(); - return $oldestBackup->lastModified() >= $threshold; + return !$oldestBackup || $oldestBackup->lastModified() >= $threshold; } } diff --git a/src/Checks/Result.php b/src/Checks/Result.php index 18e2eda3..c4f586a2 100644 --- a/src/Checks/Result.php +++ b/src/Checks/Result.php @@ -103,6 +103,13 @@ public function meta(array $meta): self return $this; } + public function appendMeta($meta): self + { + $this->meta = array_merge($this->meta, $meta); + + return $this; + } + public function endedAt(CarbonInterface $carbon): self { $this->ended_at = $carbon;