Skip to content

Commit

Permalink
♻️ Rename Dependency to Check for clarity (#9)
Browse files Browse the repository at this point in the history
Conductor describes dependency checks, this implementation should be consistent with that naming.
  • Loading branch information
shrink committed Nov 10, 2020
1 parent 0925b48 commit 2627d1b
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 356 deletions.
20 changes: 0 additions & 20 deletions src/Laravel/CollectsApplicationDependencies.php

This file was deleted.

30 changes: 30 additions & 0 deletions src/Laravel/CollectsApplicationDependencyChecks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Shrink\Conductor\Laravel;

use Shrink\Conductor\ChecksDependencyStatus;

interface CollectsApplicationDependencyChecks
{
/**
* @return array<string,\Shrink\Conductor\ChecksDependencyStatus>
*/
public function listDependencyChecks(): array;

/**
* @throws \InvalidArgumentException
*/
public function dependencyCheckById(string $id): ChecksDependencyStatus;

public function isDependencyCheckRegistered(string $id): bool;

/**
* @throws \InvalidArgumentException
*/
public function addDependencyCheck(
string $id,
ChecksDependencyStatus $check
): void;
}
40 changes: 29 additions & 11 deletions src/Laravel/Conductor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,55 @@
use Illuminate\Routing\Router;
use Illuminate\Routing\RouteRegistrar;
use Illuminate\Support\ServiceProvider;
use Lcobucci\Clock\Clock;
use Shrink\Conductor\AggregateDependency;
use Shrink\Conductor\Laravel\Dependencies\DatabaseSchema;
use Shrink\Conductor\Laravel\Http\AttachDependencyParameter;
use Shrink\Conductor\Laravel\Http\AttachDependencyCheckParameter;
use Shrink\Conductor\Laravel\Http\ShowStatus;

final class Conductor extends ServiceProvider
{
public function boot(Application $app, RouteRegistrar $registrar): void
{
/** @psalm-var \Lcobucci\Clock\Clock */
$clock = $app->make(Clock::class);

$app->instance(
CollectsApplicationDependencyChecks::class,
$checks = new DependencyChecksArray()
);

$this->registerDatabaseChecks($app, $checks);

$checks->addDependencyCheck(
'application',
new AggregateDependency($clock, $checks->listDependencyChecks())
);

$this->registerHttpEndpoints($app, $registrar);
}

private function registerDatabaseChecks(
Application $app,
CollectsApplicationDependencyChecks $checks
): void {
$app->bind(Migrator::class, 'migrator');

$app->when(DatabaseSchema::class)
->needs('$migrationsPath')
->give((string) $app->basePath('database/migrations'));

/** @psalm-var \Shrink\Conductor\Laravel\Dependencies\DatabaseSchema */
$databaseSchemaDependency = $app->make(DatabaseSchema::class);
$databaseSchemaCheck = $app->make(DatabaseSchema::class);

$dependencies = new DependenciesArray(
new Dependency('database', $databaseSchemaDependency)
);

$app->instance(CollectsApplicationDependencies::class, $dependencies);

$this->registerHttpEndpoints($app, $registrar);
$checks->addDependencyCheck('schema', $databaseSchemaCheck);
}

private function registerHttpEndpoints(
Application $app,
RouteRegistrar $registrar
): void {
$app->when(AttachDependencyParameter::class)
$app->when(AttachDependencyCheckParameter::class)
->needs('$parameter')
->give('dependency');

Expand All @@ -48,7 +66,7 @@ private function registerHttpEndpoints(
};

$registrar
->middleware(AttachDependencyParameter::class)
->middleware(AttachDependencyCheckParameter::class)
->prefix('.conductor')
->group($endpoints);
}
Expand Down
50 changes: 0 additions & 50 deletions src/Laravel/DependenciesArray.php

This file was deleted.

52 changes: 0 additions & 52 deletions src/Laravel/Dependency.php

This file was deleted.

62 changes: 62 additions & 0 deletions src/Laravel/DependencyChecksArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Shrink\Conductor\Laravel;

use InvalidArgumentException;
use Shrink\Conductor\ChecksDependencyStatus;
use function array_key_exists;
use function preg_match;

final class DependencyChecksArray implements CollectsApplicationDependencyChecks
{
/**
* @var array<string,\Shrink\Conductor\ChecksDependencyStatus>
*/
private array $checks = [];

/**
* @return array<string,\Shrink\Conductor\ChecksDependencyStatus>
*/
public function listDependencyChecks(): array
{
return $this->checks;
}

/**
* @throws \InvalidArgumentException
*/
public function dependencyCheckById(string $id): ChecksDependencyStatus
{
if (! $this->isDependencyCheckRegistered($id)) {
throw new InvalidArgumentException(<<<EXCEPTION
Dependency check not found for ID {$id}
EXCEPTION);
}

return $this->checks[$id];
}

public function isDependencyCheckRegistered(string $id): bool
{
return array_key_exists($id, $this->checks);
}

/**
* @throws \InvalidArgumentException
*/
public function addDependencyCheck(
string $id,
ChecksDependencyStatus $check
): void {
if (preg_match('%^[\w-]{1,32}$%', $id) !== 1) {
throw new InvalidArgumentException(<<<EXCEPTION
Dependency ID must be 1 to 32 word characters (ASCII letter, digit,
underscore or hyphen) in length
EXCEPTION);
}

$this->checks[$id] = $check;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
namespace Shrink\Conductor\Laravel\Http;

use Illuminate\Http\Request;
use Shrink\Conductor\Laravel\CollectsApplicationDependencies;
use Shrink\Conductor\Laravel\CollectsApplicationDependencyChecks;
use Symfony\Component\HttpFoundation\Response;

final class AttachDependencyParameter
final class AttachDependencyCheckParameter
{
private CollectsApplicationDependencies $dependencies;
private CollectsApplicationDependencyChecks $checks;

/**
* Name of the route parameter to attach the dependency to.
* Name of the route parameter to attach the check to.
*/
private string $parameter;

public function __construct(
CollectsApplicationDependencies $dependencies,
CollectsApplicationDependencyChecks $checks,
string $parameter
) {
$this->dependencies = $dependencies;
$this->checks = $checks;
$this->parameter = $parameter;
}

/**
* Find dependency by identifier from parameter and attach dependency to
* Find dependency check by identifier from parameter and attach check to
* route as a parameter.
*/
public function handle(Request $request, callable $next): Response
Expand All @@ -42,11 +42,11 @@ public function handle(Request $request, callable $next): Response
/** @psalm-var string */
$id = $route->parameter($this->parameter);

$dependency = $this->dependencies->dependencyById(
$check = $this->checks->dependencyCheckById(
(string) $id
);

$route->setParameter($this->parameter, $dependency);
$route->setParameter($this->parameter, $check);

/** @psalm-var \Symfony\Component\HttpFoundation\Response */
return $next($request);
Expand Down
23 changes: 9 additions & 14 deletions src/Laravel/Http/ShowStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,28 @@

use DateTimeInterface;
use Illuminate\Http\JsonResponse;
use Shrink\Conductor\Laravel\Dependency;
use Shrink\Conductor\ChecksDependencyStatus;

final class ShowStatus
{
/**
* Execute a dependency check and create an HTTP Response using a status
* code to represent the state of the check.
* code to represent the status of the check.
*/
public function __invoke(Dependency $dependency): JsonResponse
public function __invoke(ChecksDependencyStatus $check): JsonResponse
{
$status = $dependency->status();

$results = [
1 => ['pass', 200],
0 => ['fail', 503],
];

[$label, $code] = $results[(int) $status->hasStatusCheckPassed()];
$status = $check->dependencyStatus();

$description = [
'dependency' => $dependency->id(),
'status' => $label,
'status' => $status->hasStatusCheckPassed() ? 'pass' : 'fail',
'checkedAt' => $status
->statusCheckedAt()
->format(DateTimeInterface::ATOM),
];

return new JsonResponse($description, $code);
return new JsonResponse(
$description,
$status->hasStatusCheckPassed() ? 200 : 503
);
}
}
Loading

0 comments on commit 2627d1b

Please sign in to comment.