Skip to content

Commit

Permalink
Merge pull request #29 from vormkracht10/feature/file-check
Browse files Browse the repository at this point in the history
add FileCheck
  • Loading branch information
markvaneijk authored Sep 22, 2023
2 parents 92e176d + 003b503 commit 7449c31
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Checks/FileCheck.php
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).']');
}
}

0 comments on commit 7449c31

Please sign in to comment.