Skip to content

Commit

Permalink
Minor code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Aug 2, 2024
1 parent 2fe68f8 commit 832c90b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 18 additions & 9 deletions src/Checks/Checks/BackupsCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public function locatedAt(string $globPath): self
return $this;
}

public function onDisk($disk): static
public function onDisk(string $disk): static
{
$this->disk = Storage::disk($disk);

return $this;
}

public function parseModifiedFormat($parseModifiedFormat = 'Y-m-d_H-i-s'): self
public function parseModifiedFormat(string $parseModifiedFormat = 'Y-m-d_H-i-s'): self
{
$this->parseModifiedUsing = $parseModifiedFormat;

Expand All @@ -67,14 +67,15 @@ public function oldestBackShouldHaveBeenMadeAfter(Carbon $date): self
return $this;
}

public function atLeastSizeInMb(int $minimumSizeInMegabytes, $onlyCheckFirstAndLast = false): self
public function atLeastSizeInMb(int $minimumSizeInMegabytes, bool $onlyCheckFirstAndLast = false): self
{
$this->minimumSizeInMegabytes = $minimumSizeInMegabytes;
$this->onlyCheckSizeOnFirstAndLast = $onlyCheckFirstAndLast;

return $this;
}

public function onlyCheckSizeOnFirstAndLast($onlyCheckSizeOnFirstAndLast = true): self
public function onlyCheckSizeOnFirstAndLast(bool $onlyCheckSizeOnFirstAndLast = true): self
{
$this->onlyCheckSizeOnFirstAndLast = $onlyCheckSizeOnFirstAndLast;

Expand Down Expand Up @@ -116,15 +117,15 @@ public function run(): Result
$oldestBackup = $this->getOldestBackup($eligibleBackups);

$result->appendMeta([
'youngest_backup' => Carbon::createFromTimestamp($youngestBackup?->lastModified())->toDateTimeString(),
'oldest_backup' => Carbon::createFromTimestamp($oldestBackup?->lastModified())->toDateTimeString(),
'youngest_backup' => $youngestBackup ? Carbon::createFromTimestamp($youngestBackup->lastModified())->toDateTimeString() : null,
'oldest_backup' => $oldestBackup ? Carbon::createFromTimestamp($oldestBackup->lastModified())->toDateTimeString() : null,
]);

if ($this->youngestShouldHaveBeenMadeBefore && $this->youngestBackupIsToolOld($youngestBackup)) {
if ($this->youngestBackupIsToolOld($youngestBackup)) {
return $result->failed('The youngest backup was too old');
}

if ($this->oldestShouldHaveBeenMadeAfter && $this->oldestBackupIsTooYoung($oldestBackup)) {
if ($this->oldestBackupIsTooYoung($oldestBackup)) {
return $result->failed('The oldest backup was too young');
}

Expand All @@ -146,7 +147,7 @@ protected function getBackupFiles(): Collection
return collect(
$this->disk
? $this->disk->files($this->locatedAt)
: File::glob($this->locatedAt)
: File::glob($this->locatedAt ?? '')
)->map(function (string $path) {
return new BackupFile($path, $this->disk, $this->parseModifiedUsing);
});
Expand All @@ -161,6 +162,10 @@ protected function getYoungestBackup(Collection $backups): ?BackupFile

protected function youngestBackupIsToolOld(?BackupFile $youngestBackup): bool
{
if ($this->youngestShouldHaveBeenMadeBefore === null) {
return false;
}

$threshold = $this->youngestShouldHaveBeenMadeBefore->getTimestamp();

return !$youngestBackup || $youngestBackup->lastModified() <= $threshold;
Expand All @@ -175,6 +180,10 @@ protected function getOldestBackup(Collection $backups): ?BackupFile
}
protected function oldestBackupIsTooYoung(?BackupFile $oldestBackup): bool
{
if ($this->oldestShouldHaveBeenMadeAfter === null) {
return false;
}

$threshold = $this->oldestShouldHaveBeenMadeAfter->getTimestamp();

return !$oldestBackup || $oldestBackup->lastModified() >= $threshold;
Expand Down
2 changes: 1 addition & 1 deletion src/Support/BackupFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function lastModified(): ?int
$filename = Str::of($this->path)->afterLast('/')->before('.');

try {
return Carbon::createFromFormat($this->parseModifiedUsing, $filename)->timestamp;
return (int) Carbon::createFromFormat($this->parseModifiedUsing, $filename)->timestamp;
} catch (InvalidFormatException $e) {
return null;
}
Expand Down

0 comments on commit 832c90b

Please sign in to comment.