Skip to content

Commit 864ebd8

Browse files
committed
Add project
1 parent 548854f commit 864ebd8

15 files changed

+310
-67
lines changed

.github/workflows/ci.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
timeout-minutes: 15
1515
strategy:
1616
matrix:
17-
php: [ '8.1', '8.2', '8.3', '8.4' ]
17+
php: [ '8.0', '8.1', '8.2', '8.3', '8.4' ]
1818
dependency-version: [ '' ]
1919
include:
2020
- php: '8.1'
@@ -56,7 +56,5 @@ jobs:
5656
restore-keys: php-composer-locked-
5757
- name: Install PHP dependencies
5858
run: composer install --no-interaction --no-progress --no-suggest
59-
- name: PHP CS
60-
run: vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr
6159
- name: PHPStan
6260
run: vendor/bin/phpstan

.phpcs.xml

-9
This file was deleted.

README.md

-46
This file was deleted.

composer.json

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
{
2-
"name": "mnapoli/myproject",
3-
"description": "Give it a nice description!",
2+
"name": "bref/laravel-health-check",
3+
"description": "Bref health checks for Laravel applications",
44
"keywords": [],
55
"license": "MIT",
66
"type": "library",
77
"autoload": {
88
"psr-4": {
9-
"MyProject\\": "src/"
9+
"Bref\\LaravelHealthCheck\\": "src/"
1010
}
1111
},
1212
"autoload-dev": {
1313
"psr-4": {
14-
"MyProject\\Test\\": "tests/"
14+
"Bref\\LaravelHealthCheck\\Test\\": "tests/"
1515
}
1616
},
1717
"require": {
18-
"php": ">=8.0"
18+
"php": ">=8.0",
19+
"illuminate/console": "^8.0 || ^9.0 || ^10.0 || ^11.0",
20+
"illuminate/contracts": "^8.0 || ^9.0 || ^10.0 || ^11.0",
21+
"illuminate/database": "^8.0 || ^9.0 || ^10.0 || ^11.0",
22+
"illuminate/http": "^8.0 || ^9.0 || ^10.0 || ^11.0",
23+
"illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0"
1924
},
2025
"require-dev": {
2126
"phpunit/phpunit": "^9.0",
22-
"mnapoli/hard-mode": "^0.3.0",
2327
"phpstan/phpstan": "^1"
2428
},
25-
"config": {
26-
"allow-plugins": {
27-
"dealerdirect/phpcodesniffer-composer-installer": true
29+
"extra": {
30+
"laravel": {
31+
"providers": [
32+
"Bref\\LaravelHealthCheck\\BrefHealthCheckServiceProvider"
33+
]
2834
}
2935
}
3036
}

src/.gitkeep

Whitespace-only changes.
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\LaravelHealthCheck;
4+
5+
use Bref\LaravelHealthCheck\Commands\BrefHealthCheck;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class BrefHealthCheckServiceProvider extends ServiceProvider
9+
{
10+
public function register(): void
11+
{
12+
$this->commands([
13+
BrefHealthCheck::class,
14+
]);
15+
}
16+
}

src/Check.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\LaravelHealthCheck;
4+
5+
abstract class Check
6+
{
7+
abstract public function getName(): string;
8+
9+
abstract public function check(): CheckResult;
10+
11+
public function ok(?string $message = null): CheckResult
12+
{
13+
return CheckResult::ok($this->getName(), $message);
14+
}
15+
16+
public function warning(?string $message = null): CheckResult
17+
{
18+
return CheckResult::error($this->getName(), $message);
19+
}
20+
21+
public function error(?string $message = null): CheckResult
22+
{
23+
return CheckResult::error($this->getName(), $message);
24+
}
25+
}

src/CheckResult.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\LaravelHealthCheck;
4+
5+
class CheckResult
6+
{
7+
private const STATUS_OK = 'ok';
8+
private const STATUS_WARNING = 'warning';
9+
private const STATUS_ERROR = 'error';
10+
11+
/** @readonly */
12+
public string $name;
13+
/** @readonly */
14+
public string $status;
15+
/** @readonly */
16+
public ?string $message;
17+
18+
public function __construct(string $name, string $status, ?string $message = null)
19+
{
20+
$this->name = $name;
21+
$this->status = $status;
22+
$this->message = $message;
23+
}
24+
25+
public static function ok(string $name, ?string $message = null): self
26+
{
27+
return new self($name, self::STATUS_OK, $message);
28+
}
29+
30+
public static function warning(string $name, ?string $message = null): self
31+
{
32+
return new self($name, self::STATUS_WARNING, $message);
33+
}
34+
35+
public static function error(string $name, ?string $message = null): self
36+
{
37+
return new self($name, self::STATUS_ERROR, $message);
38+
}
39+
40+
/**
41+
* @return array{status: string, name: string, message: string|null}
42+
*/
43+
public function toArray(): array
44+
{
45+
return [
46+
'name' => $this->name,
47+
'status' => $this->status,
48+
'message' => $this->message,
49+
];
50+
}
51+
}

src/Checks/CacheConnection.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\LaravelHealthCheck\Checks;
4+
5+
use Bref\LaravelHealthCheck\Check;
6+
use Bref\LaravelHealthCheck\CheckResult;
7+
use Illuminate\Support\Facades\Cache;
8+
use Illuminate\Support\Str;
9+
10+
class CacheConnection extends Check
11+
{
12+
public function getName(): string
13+
{
14+
return 'Cache connection';
15+
}
16+
17+
public function check(): CheckResult
18+
{
19+
if (! $this->canWriteValuesToCache()) {
20+
return $this->error();
21+
}
22+
23+
return $this->ok();
24+
}
25+
26+
protected function canWriteValuesToCache(): bool
27+
{
28+
$value = Str::random(5);
29+
$key = "bref:health-check-$value";
30+
31+
Cache::put($key, $value, 10);
32+
$actualValue = Cache::get($key);
33+
Cache::forget($key);
34+
35+
return $actualValue === $value;
36+
}
37+
}

src/Checks/DatabaseConnection.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\LaravelHealthCheck\Checks;
4+
5+
use Bref\LaravelHealthCheck\Check;
6+
use Bref\LaravelHealthCheck\CheckResult;
7+
use Illuminate\Support\Facades\DB;
8+
use Throwable;
9+
10+
class DatabaseConnection extends Check
11+
{
12+
public function getName(): string
13+
{
14+
return 'Database connection';
15+
}
16+
17+
public function check(): CheckResult
18+
{
19+
try {
20+
DB::connection()->getPdo();
21+
22+
return $this->ok();
23+
} catch (Throwable $exception) {
24+
return $this->error("Could not connect to the database: `{$exception->getMessage()}`");
25+
}
26+
}
27+
}

src/Checks/DebugModeIsDisabled.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\LaravelHealthCheck\Checks;
4+
5+
use Bref\LaravelHealthCheck\Check;
6+
use Bref\LaravelHealthCheck\CheckResult;
7+
8+
class DebugModeIsDisabled extends Check
9+
{
10+
public function getName(): string
11+
{
12+
return 'Debug mode is disabled';
13+
}
14+
15+
public function check(): CheckResult
16+
{
17+
return config('app.debug') ? $this->error() : $this->ok();
18+
}
19+
}

src/Checks/InternetConnectivity.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\LaravelHealthCheck\Checks;
4+
5+
use Bref\LaravelHealthCheck\Check;
6+
use Bref\LaravelHealthCheck\CheckResult;
7+
use Illuminate\Support\Facades\Http;
8+
use Throwable;
9+
10+
class InternetConnectivity extends Check
11+
{
12+
public function getName(): string
13+
{
14+
return 'Internet connectivity';
15+
}
16+
17+
public function check(): CheckResult
18+
{
19+
try {
20+
$status = Http::timeout(3)
21+
->connectTimeout(3)
22+
->get('https://google.com')
23+
->successful();
24+
} catch (Throwable $e) {
25+
return $this->error($e->getMessage());
26+
}
27+
28+
return $status ? $this->ok() : $this->error();
29+
}
30+
}

src/Checks/LambdaMemoryLimit.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\LaravelHealthCheck\Checks;
4+
5+
use Bref\LaravelHealthCheck\Check;
6+
use Bref\LaravelHealthCheck\CheckResult;
7+
8+
class LambdaMemoryLimit extends Check
9+
{
10+
public function getName(): string
11+
{
12+
return 'Memory available is sufficient';
13+
}
14+
15+
public function check(): CheckResult
16+
{
17+
$memoryAvailableInMb = (int) ($_SERVER['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'] ?? 0);
18+
$recommendedMemory = 1024;
19+
20+
if ($memoryAvailableInMb < $recommendedMemory) {
21+
return $this->error("The memory limit configured is $memoryAvailableInMb MB, but $recommendedMemory MB is recommended.");
22+
}
23+
24+
return $this->ok();
25+
}
26+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\LaravelHealthCheck\Checks;
4+
5+
use Bref\LaravelHealthCheck\Check;
6+
use Bref\LaravelHealthCheck\CheckResult;
7+
8+
class PhpVersionIsRecentEnough extends Check
9+
{
10+
public function getName(): string
11+
{
12+
return 'PHP version is recent enough';
13+
}
14+
15+
public function check(): CheckResult
16+
{
17+
if (PHP_VERSION_ID < 80100) {
18+
return $this->error('PHP version is no longer supported, upgrade to PHP 8.1 or newer');
19+
}
20+
21+
return $this->ok();
22+
}
23+
}

0 commit comments

Comments
 (0)