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.
- Loading branch information
1 parent
f0e8581
commit fb235bd
Showing
28 changed files
with
400 additions
and
5 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
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,85 @@ | ||
<?php | ||
|
||
namespace Vormkracht10\LaravelOK\Checks; | ||
|
||
use Cron\CronExpression; | ||
use Illuminate\Console\Scheduling\ManagesFrequencies; | ||
use Illuminate\Support\Facades\Date; | ||
use Illuminate\Support\Str; | ||
use Vormkracht10\LaravelOK\Enums\Status; | ||
|
||
abstract class Check | ||
{ | ||
use ManagesFrequencies; | ||
|
||
protected string $expression = '* * * * *'; | ||
|
||
protected ?string $name = null; | ||
|
||
protected ?string $label = null; | ||
|
||
protected bool $shouldRun = true; | ||
|
||
public function __construct() | ||
{ | ||
} | ||
|
||
public static function config(): static | ||
{ | ||
$instance = app(static::class); | ||
|
||
$instance->everyMinute(); | ||
|
||
return $instance; | ||
} | ||
|
||
public function name(string $name): self | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
if ($this->name) { | ||
return $this->name; | ||
} | ||
|
||
$baseName = class_basename(static::class); | ||
|
||
return Str::of($baseName)->beforeLast('Check'); | ||
} | ||
|
||
public function shouldRun(): bool | ||
{ | ||
if (! $this->shouldRun) { | ||
return false; | ||
} | ||
|
||
$date = Date::now(); | ||
|
||
return (new CronExpression($this->expression))->isDue($date->toDateTimeString()); | ||
} | ||
|
||
public function if(bool $condition) | ||
{ | ||
$this->shouldRun = $condition; | ||
|
||
return $this; | ||
} | ||
|
||
public function unless(bool $condition) | ||
{ | ||
$this->shouldRun = ! $condition; | ||
|
||
return $this; | ||
} | ||
|
||
abstract public function run(): Result; | ||
|
||
public function markAsCrashed(): Result | ||
{ | ||
return new Result(Status::CRASHED); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,103 @@ | ||
<?php | ||
|
||
namespace Spatie\Health\Checks\Checks; | ||
|
||
use Exception; | ||
use Illuminate\Support\Facades\Http; | ||
use Spatie\Health\Checks\Check; | ||
use Spatie\Health\Checks\Result; | ||
use Spatie\Health\Exceptions\InvalidCheck; | ||
|
||
class CheckUrl extends Check | ||
{ | ||
protected ?string $url = null; | ||
|
||
protected ?string $failureMessage = null; | ||
|
||
protected int $timeout = 1; | ||
|
||
protected int $retryTimes = 1; | ||
|
||
protected string $method = 'GET'; | ||
|
||
/** @var array<string, string> */ | ||
protected array $headers = []; | ||
|
||
public function url(string $url): self | ||
{ | ||
$this->url = $url; | ||
|
||
return $this; | ||
} | ||
|
||
public function timeout(int $seconds): self | ||
{ | ||
$this->timeout = $seconds; | ||
|
||
return $this; | ||
} | ||
|
||
public function method(string $method): self | ||
{ | ||
$this->method = $method; | ||
|
||
return $this; | ||
} | ||
|
||
public function retryTimes(int $times): self | ||
{ | ||
$this->retryTimes = $times; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param array<string, string> $headers | ||
* @return $this | ||
*/ | ||
public function headers(array $headers = []): self | ||
{ | ||
$this->headers = $headers; | ||
|
||
return $this; | ||
} | ||
|
||
public function failureMessage(string $failureMessage): self | ||
{ | ||
$this->failureMessage = $failureMessage; | ||
|
||
return $this; | ||
} | ||
|
||
public function run(): Result | ||
{ | ||
if (is_null($this->url)) { | ||
throw InvalidCheck::urlNotSet(); | ||
} | ||
|
||
try { | ||
$request = Http::timeout($this->timeout) | ||
->withHeaders($this->headers) | ||
->retry($this->retryTimes) | ||
->send($this->method, $this->url); | ||
|
||
if (! $request->successful()) { | ||
return $this->failedResult(); | ||
} | ||
} catch (Exception) { | ||
return $this->failedResult(); | ||
} | ||
|
||
return Result::make() | ||
->ok() | ||
->shortSummary('Reachable'); | ||
} | ||
|
||
protected function failedResult(): Result | ||
{ | ||
return Result::make() | ||
->failed() | ||
->shortSummary('Unreachable') | ||
->notificationMessage($this->failureMessage ?? "Pinging {$this->getName()} failed."); | ||
} | ||
} |
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,80 @@ | ||
<?php | ||
|
||
namespace Vormkracht10\LaravelOK\Checks; | ||
|
||
use Carbon\CarbonInterface; | ||
use Vormkracht10\LaravelOK\Enums\Status; | ||
|
||
class Result | ||
{ | ||
public Check $check; | ||
|
||
public ?CarbonInterface $ended_at; | ||
|
||
public static function make(string $message = ''): self | ||
{ | ||
return new self(Status::OK, $message); | ||
} | ||
|
||
public function __construct( | ||
public Status $status, | ||
public string $notificationMessage = '', | ||
) { | ||
} | ||
|
||
public function check(Check $check): self | ||
{ | ||
$this->check = $check; | ||
|
||
return $this; | ||
} | ||
|
||
public function notificationMessage(string $notificationMessage): self | ||
{ | ||
$this->notificationMessage = $notificationMessage; | ||
|
||
return $this; | ||
} | ||
|
||
public function ok(string $message = ''): self | ||
{ | ||
$this->notificationMessage = $message; | ||
|
||
$this->status = Status::ok(); | ||
|
||
return $this; | ||
} | ||
|
||
public function warning(string $message = ''): self | ||
{ | ||
$this->notificationMessage = $message; | ||
|
||
$this->status = Status::warning(); | ||
|
||
return $this; | ||
} | ||
|
||
public function failed(string $message = ''): self | ||
{ | ||
$this->notificationMessage = $message; | ||
|
||
$this->status = Status::failed(); | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<string, mixed> $meta */ | ||
public function meta(array $meta): self | ||
{ | ||
$this->meta = $meta; | ||
|
||
return $this; | ||
} | ||
|
||
public function endedAt(CarbonInterface $carbon): self | ||
{ | ||
$this->ended_at = $carbon; | ||
|
||
return $this; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,34 @@ | ||
<?php | ||
|
||
namespace Vormkracht10\LaravelOK\Checks\Checks; | ||
|
||
use function config; | ||
use Vormkracht10\LaravelOK\Checks\Result; | ||
|
||
class DebugModeCheck extends Check | ||
{ | ||
protected bool $expected = false; | ||
|
||
public function expectedToBe(bool $bool): self | ||
{ | ||
$this->expected = $bool; | ||
|
||
return $this; | ||
} | ||
|
||
public function run(): Result | ||
{ | ||
$actual = config('app.debug'); | ||
|
||
$result = Result::make(); | ||
|
||
return $this->expected === $actual | ||
? $result->ok() | ||
: $result->failed("The debug mode was expected to be `{$this->convertToWord((bool) $this->expected)}`, but actually was `{$this->convertToWord((bool) $actual)}`"); | ||
} | ||
|
||
protected function convertToWord(bool $boolean): string | ||
{ | ||
return $boolean ? 'true' : 'false'; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
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,11 @@ | ||
<?php | ||
|
||
namespace Vormkracht10\LaravelOK\Enums; | ||
|
||
enum Status: string | ||
{ | ||
case OK = 'ok'; | ||
case FAILED = 'failed'; | ||
case SKIPPED = 'skipped'; | ||
case CRASHED = 'crashed'; | ||
} |
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
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
Oops, something went wrong.