generated from vormkracht10/laravel-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from vormkracht10/feature/file-check
add FileCheck
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Vormkracht10\LaravelOK\Checks; | ||
|
||
use Vormkracht10\LaravelOK\Checks\Base\Check; | ||
use Vormkracht10\LaravelOK\Checks\Base\Result; | ||
|
||
class FileCheck extends Check | ||
{ | ||
protected array $files = []; | ||
|
||
/** | ||
* @param array<int, string> $files | ||
* @return $this | ||
*/ | ||
public function files(array $files): static | ||
{ | ||
$this->files = $files; | ||
|
||
return $this; | ||
} | ||
|
||
public function run(): Result | ||
{ | ||
$result = Result::new(); | ||
|
||
if (empty($files = $this->files)) { | ||
return $result->failed('No files have been configured'); | ||
} | ||
|
||
$paths = array_map( | ||
fn ($path) => str_starts_with($path, '/') ? $path : base_path($path), | ||
$files, | ||
); | ||
|
||
$failed = array_filter( | ||
$paths, | ||
fn ($path) => ! file_exists($path), | ||
); | ||
|
||
return empty($failed) | ||
? $result->ok('All paths are files') | ||
: $result->failed('Some paths don\'t exist or are not files ['.implode(', ', $failed).']'); | ||
} | ||
} |