Skip to content

Commit

Permalink
Merge pull request #87 from ans-group/strict-typing-src
Browse files Browse the repository at this point in the history
Add strict typing
  • Loading branch information
phily245 authored Jul 19, 2024
2 parents 9143ec2 + cecd7c2 commit 2e45d58
Show file tree
Hide file tree
Showing 32 changed files with 302 additions and 178 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Strict typing has been added to all properties and methods **(breaking change)**

### Updated

- Dropped support for < Laravel 10 **(breaking change)**
- The `context` property for `UKFast\HealthCheck\Checks\CrossServiceHealthCheck` is now an array, not a string, matching other checks **(breaking change)**

## [1.15.0] - 2024-07-17

### Added
Expand Down
27 changes: 15 additions & 12 deletions src/AppHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@

namespace UKFast\HealthCheck;

use Exception;
use Illuminate\Support\Collection;
use UKFast\HealthCheck\Exceptions\CheckNotFoundException;

class AppHealth
{
/**
* @var Collection $checks
*/
protected $checks;

public function __construct($checks)
{
$this->checks = $checks;
public function __construct(
/**
* @var Collection<int, class-string>
*/
protected Collection $checks,
) {
}

public function passes($checkName)
{
/**
* @var HealthCheck $check
*/
$check = $this->checks->filter(function ($check) use ($checkName) {
return $check->name() == $checkName;
})->first();
Expand All @@ -29,22 +32,22 @@ public function passes($checkName)

try {
return $check->status()->isOkay();
} catch (\Exception $e) {
} catch (Exception) {
return false;
}
}

public function fails($checkName)
public function fails($checkName): bool
{
return !$this->passes($checkName);
}

/**
* Returns a collection of all health checks
*
* @return Illuminate\Support\Collection
*
* @return Collection<int, class-string>
*/
public function all()
public function all(): Collection
{
return $this->checks;
}
Expand Down
18 changes: 10 additions & 8 deletions src/Checks/CacheHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@

namespace UKFast\HealthCheck\Checks;

use Exception;
use Illuminate\Support\Facades\Cache;
use UKFast\HealthCheck\HealthCheck;
use Carbon\Carbon;
use UKFast\HealthCheck\Status;

class CacheHealthCheck extends HealthCheck
{
protected $name = 'cache';
protected string $name = 'cache';

protected $workingStores = [];

protected $incorrectValues = [];
protected array $workingStores = [];

protected $exceptions = [];
protected array $incorrectValues = [];

public function status()
protected array $exceptions = [];

public function status(): Status
{
foreach (config('healthcheck.cache.stores') as $store) {
try {
$cache = Cache::store($store);

$cache->put('laravel-health-check', 'healthy', Carbon::now()->addMinutes(1));

$value = $cache->pull('laravel-health-check', 'broken');

if ($value != 'healthy') {
Expand All @@ -35,7 +37,7 @@ public function status()
}

$this->workingStores[] = $store;
} catch (\Exception $e) {
} catch (Exception $e) {
$this->exceptions[] = [
'store' => $store,
'error' => $e->getMessage()
Expand Down
22 changes: 8 additions & 14 deletions src/Checks/CrossServiceHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,28 @@
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
use UKFast\HealthCheck\HealthCheck;
use UKFast\HealthCheck\Status;

class CrossServiceHealthCheck extends HealthCheck
{
protected $name = 'x-service-checks';
protected string $name = 'x-service-checks';

/**
* @var \GuzzleHttp\Client
*/
protected $http;
protected Client $http;

/**
* @var \Illuminate\Http\Request
*/
protected $request;
protected Request $request;

public function __construct(Client $http, Request $request)
{
$this->http = $http;
$this->request = $request;
}

/**
* {@inheritdoc}
*/
public function status()
public function status(): Status
{
if ($this->request->headers->has('X-Service-Check')) {
return $this->okay('Skipped, X-Service-Check header is present');
return $this->okay([
'message' => 'Skipped, X-Service-Check header is present',
]);
}

$failedServices = [];
Expand Down
8 changes: 4 additions & 4 deletions src/Checks/DatabaseHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
use Exception;
use Illuminate\Database\DatabaseManager;
use UKFast\HealthCheck\HealthCheck;
use UKFast\HealthCheck\Status;

class DatabaseHealthCheck extends HealthCheck
{
protected $name = 'database';
protected string $name = 'database';

/** @var \Illuminate\Database\DatabaseManager */
protected $db;
protected DatabaseManager $db;

public function __construct(DatabaseManager $db)
{
$this->db = $db;
}

public function status()
public function status(): Status
{
foreach (config('healthcheck.database.connections') as $connection) {
try {
Expand Down
7 changes: 4 additions & 3 deletions src/Checks/EnvHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace UKFast\HealthCheck\Checks;

use UKFast\HealthCheck\HealthCheck;
use UKFast\HealthCheck\Status;

class EnvHealthCheck extends HealthCheck
{
protected $name = 'env';
public function status()
protected string $name = 'env';

public function status(): Status
{
$default = config('healthcheck.env-check-key', 'HEALTH_CHECK_ENV_DEFAULT_VALUE');

Expand Down
5 changes: 3 additions & 2 deletions src/Checks/FtpHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use League\Flysystem\Adapter\Ftp;
use UKFast\HealthCheck\HealthCheck;
use UKFast\HealthCheck\Status;

class FtpHealthCheck extends HealthCheck
{
protected $name = 'ftp';
protected string $name = 'ftp';

/**
* @var \Illuminate\Contracts\Filesystem\Filesystem
Expand All @@ -19,7 +20,7 @@ public function __construct(Ftp $ftp)
$this->ftp = $ftp;
}

public function status()
public function status(): Status
{
try {
$this->ftp->getConnection();
Expand Down
5 changes: 3 additions & 2 deletions src/Checks/HttpHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Container\Container;
use UKFast\HealthCheck\HealthCheck;
use UKFast\HealthCheck\Status;

class HttpHealthCheck extends HealthCheck
{
protected $name = 'http';
protected string $name = 'http';

public function status()
public function status(): Status
{
$container = Container::getInstance();

Expand Down
12 changes: 9 additions & 3 deletions src/Checks/LogHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@
namespace UKFast\HealthCheck\Checks;

use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Container\Container;
use Psr\Log\LoggerInterface;
use UKFast\HealthCheck\HealthCheck;
use UKFast\HealthCheck\Status;

class LogHealthCheck extends HealthCheck
{
protected $name = 'log';
protected string $name = 'log';

protected $logger;
protected LoggerInterface $logger;

/**
* @throws BindingResolutionException
*/
public function __construct(Container $container)
{
$this->logger = $container->make('log');
}

public function status()
public function status(): Status
{
try {
$this->logger->info('Checking if logs are writable');
Expand Down
31 changes: 7 additions & 24 deletions src/Checks/MigrationUpToDateHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@

class MigrationUpToDateHealthCheck extends HealthCheck
{
protected $name = 'migration';
protected string $name = 'migration';

/**
* @var Migrator
*/
protected $migrator;
protected Migrator $migrator;

/**
* @return Status
*/
public function status()
public function status(): Status
{
try {
$pendingMigrations = (array)$this->getPendingMigrations();
Expand All @@ -38,21 +32,16 @@ public function status()
return $this->okay();
}

/**
* @return array
*/
protected function getPendingMigrations()
protected function getPendingMigrations(): array
{
$files = $this->getMigrator()->getMigrationFiles($this->getMigrationPath());
return array_diff(array_keys($files), $this->getRanMigrations());
}

/**
* Gets ran migrations with repository check
*
* @return array
*/
protected function getRanMigrations()
protected function getRanMigrations(): array
{
if (!$this->getMigrator()->repositoryExists()) {
return [];
Expand All @@ -61,10 +50,7 @@ protected function getRanMigrations()
return $this->getMigrator()->getRepository()->getRan();
}

/**
* @return Migrator
*/
protected function getMigrator()
protected function getMigrator(): Migrator
{
if (is_null($this->migrator)) {
$this->migrator = app('migrator');
Expand All @@ -73,10 +59,7 @@ protected function getMigrator()
return $this->migrator;
}

/**
* @return string
*/
protected function getMigrationPath()
protected function getMigrationPath(): string
{
return database_path() . DIRECTORY_SEPARATOR . 'migrations';
}
Expand Down
18 changes: 14 additions & 4 deletions src/Checks/PackageSecurityHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@
namespace UKFast\HealthCheck\Checks;

use Exception;
use Illuminate\Support\Collection;
use SensioLabs\Security\SecurityChecker;
use UKFast\HealthCheck\HealthCheck;
use UKFast\HealthCheck\Status;

class PackageSecurityHealthCheck extends HealthCheck
{
protected $name = 'package-security';
protected string $name = 'package-security';

protected $vulnerablePackages = [];
/**
* @var Collection<int, string> $vulnerablePackages
*/
protected Collection $vulnerablePackages;

public static function checkDependency()
public function __construct()
{
$this->vulnerablePackages = collect();
}

public static function checkDependency(): bool
{
return class_exists(SecurityChecker::class);
}

public function status()
public function status(): Status
{
try {
if (! static::checkDependency()) {
Expand Down
Loading

0 comments on commit 2e45d58

Please sign in to comment.