Skip to content

Commit

Permalink
Add project
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Oct 28, 2024
1 parent 548854f commit 864ebd8
Show file tree
Hide file tree
Showing 15 changed files with 310 additions and 67 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
timeout-minutes: 15
strategy:
matrix:
php: [ '8.1', '8.2', '8.3', '8.4' ]
php: [ '8.0', '8.1', '8.2', '8.3', '8.4' ]
dependency-version: [ '' ]
include:
- php: '8.1'
Expand Down Expand Up @@ -56,7 +56,5 @@ jobs:
restore-keys: php-composer-locked-
- name: Install PHP dependencies
run: composer install --no-interaction --no-progress --no-suggest
- name: PHP CS
run: vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr
- name: PHPStan
run: vendor/bin/phpstan
9 changes: 0 additions & 9 deletions .phpcs.xml

This file was deleted.

46 changes: 0 additions & 46 deletions README.md

This file was deleted.

24 changes: 15 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
{
"name": "mnapoli/myproject",
"description": "Give it a nice description!",
"name": "bref/laravel-health-check",
"description": "Bref health checks for Laravel applications",
"keywords": [],
"license": "MIT",
"type": "library",
"autoload": {
"psr-4": {
"MyProject\\": "src/"
"Bref\\LaravelHealthCheck\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"MyProject\\Test\\": "tests/"
"Bref\\LaravelHealthCheck\\Test\\": "tests/"
}
},
"require": {
"php": ">=8.0"
"php": ">=8.0",
"illuminate/console": "^8.0 || ^9.0 || ^10.0 || ^11.0",
"illuminate/contracts": "^8.0 || ^9.0 || ^10.0 || ^11.0",
"illuminate/database": "^8.0 || ^9.0 || ^10.0 || ^11.0",
"illuminate/http": "^8.0 || ^9.0 || ^10.0 || ^11.0",
"illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"mnapoli/hard-mode": "^0.3.0",
"phpstan/phpstan": "^1"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
"extra": {
"laravel": {
"providers": [
"Bref\\LaravelHealthCheck\\BrefHealthCheckServiceProvider"
]
}
}
}
Empty file removed src/.gitkeep
Empty file.
16 changes: 16 additions & 0 deletions src/BrefHealthCheckServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Bref\LaravelHealthCheck;

use Bref\LaravelHealthCheck\Commands\BrefHealthCheck;
use Illuminate\Support\ServiceProvider;

class BrefHealthCheckServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->commands([
BrefHealthCheck::class,
]);
}
}
25 changes: 25 additions & 0 deletions src/Check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace Bref\LaravelHealthCheck;

abstract class Check
{
abstract public function getName(): string;

abstract public function check(): CheckResult;

public function ok(?string $message = null): CheckResult
{
return CheckResult::ok($this->getName(), $message);
}

public function warning(?string $message = null): CheckResult
{
return CheckResult::error($this->getName(), $message);
}

public function error(?string $message = null): CheckResult
{
return CheckResult::error($this->getName(), $message);
}
}
51 changes: 51 additions & 0 deletions src/CheckResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

namespace Bref\LaravelHealthCheck;

class CheckResult
{
private const STATUS_OK = 'ok';
private const STATUS_WARNING = 'warning';
private const STATUS_ERROR = 'error';

/** @readonly */
public string $name;
/** @readonly */
public string $status;
/** @readonly */
public ?string $message;

public function __construct(string $name, string $status, ?string $message = null)
{
$this->name = $name;
$this->status = $status;
$this->message = $message;
}

public static function ok(string $name, ?string $message = null): self
{
return new self($name, self::STATUS_OK, $message);
}

public static function warning(string $name, ?string $message = null): self
{
return new self($name, self::STATUS_WARNING, $message);
}

public static function error(string $name, ?string $message = null): self
{
return new self($name, self::STATUS_ERROR, $message);
}

/**
* @return array{status: string, name: string, message: string|null}
*/
public function toArray(): array
{
return [
'name' => $this->name,
'status' => $this->status,
'message' => $this->message,
];
}
}
37 changes: 37 additions & 0 deletions src/Checks/CacheConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace Bref\LaravelHealthCheck\Checks;

use Bref\LaravelHealthCheck\Check;
use Bref\LaravelHealthCheck\CheckResult;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;

class CacheConnection extends Check
{
public function getName(): string
{
return 'Cache connection';
}

public function check(): CheckResult
{
if (! $this->canWriteValuesToCache()) {
return $this->error();
}

return $this->ok();
}

protected function canWriteValuesToCache(): bool
{
$value = Str::random(5);
$key = "bref:health-check-$value";

Cache::put($key, $value, 10);
$actualValue = Cache::get($key);
Cache::forget($key);

return $actualValue === $value;
}
}
27 changes: 27 additions & 0 deletions src/Checks/DatabaseConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace Bref\LaravelHealthCheck\Checks;

use Bref\LaravelHealthCheck\Check;
use Bref\LaravelHealthCheck\CheckResult;
use Illuminate\Support\Facades\DB;
use Throwable;

class DatabaseConnection extends Check
{
public function getName(): string
{
return 'Database connection';
}

public function check(): CheckResult
{
try {
DB::connection()->getPdo();

return $this->ok();
} catch (Throwable $exception) {
return $this->error("Could not connect to the database: `{$exception->getMessage()}`");
}
}
}
19 changes: 19 additions & 0 deletions src/Checks/DebugModeIsDisabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace Bref\LaravelHealthCheck\Checks;

use Bref\LaravelHealthCheck\Check;
use Bref\LaravelHealthCheck\CheckResult;

class DebugModeIsDisabled extends Check
{
public function getName(): string
{
return 'Debug mode is disabled';
}

public function check(): CheckResult
{
return config('app.debug') ? $this->error() : $this->ok();

Check failure on line 17 in src/Checks/DebugModeIsDisabled.php

View workflow job for this annotation

GitHub Actions / Coding standards

Function config not found.
}
}
30 changes: 30 additions & 0 deletions src/Checks/InternetConnectivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

namespace Bref\LaravelHealthCheck\Checks;

use Bref\LaravelHealthCheck\Check;
use Bref\LaravelHealthCheck\CheckResult;
use Illuminate\Support\Facades\Http;
use Throwable;

class InternetConnectivity extends Check
{
public function getName(): string
{
return 'Internet connectivity';
}

public function check(): CheckResult
{
try {
$status = Http::timeout(3)
->connectTimeout(3)
->get('https://google.com')
->successful();
} catch (Throwable $e) {
return $this->error($e->getMessage());
}

return $status ? $this->ok() : $this->error();
}
}
26 changes: 26 additions & 0 deletions src/Checks/LambdaMemoryLimit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace Bref\LaravelHealthCheck\Checks;

use Bref\LaravelHealthCheck\Check;
use Bref\LaravelHealthCheck\CheckResult;

class LambdaMemoryLimit extends Check
{
public function getName(): string
{
return 'Memory available is sufficient';
}

public function check(): CheckResult
{
$memoryAvailableInMb = (int) ($_SERVER['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'] ?? 0);
$recommendedMemory = 1024;

if ($memoryAvailableInMb < $recommendedMemory) {
return $this->error("The memory limit configured is $memoryAvailableInMb MB, but $recommendedMemory MB is recommended.");
}

return $this->ok();
}
}
23 changes: 23 additions & 0 deletions src/Checks/PhpVersionIsRecentEnough.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Bref\LaravelHealthCheck\Checks;

use Bref\LaravelHealthCheck\Check;
use Bref\LaravelHealthCheck\CheckResult;

class PhpVersionIsRecentEnough extends Check
{
public function getName(): string
{
return 'PHP version is recent enough';
}

public function check(): CheckResult
{
if (PHP_VERSION_ID < 80100) {
return $this->error('PHP version is no longer supported, upgrade to PHP 8.1 or newer');
}

return $this->ok();
}
}
Loading

0 comments on commit 864ebd8

Please sign in to comment.