Skip to content

Commit

Permalink
Refactor/simplify BackupsCheck + add metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Aug 2, 2024
1 parent 6527309 commit 22761f1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 46 deletions.
93 changes: 47 additions & 46 deletions src/Checks/Checks/BackupsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}
}
7 changes: 7 additions & 0 deletions src/Checks/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 22761f1

Please sign in to comment.