Skip to content

Commit

Permalink
fix: add base controller
Browse files Browse the repository at this point in the history
  • Loading branch information
oparshyna committed Sep 24, 2023
1 parent a72f628 commit 22eda82
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 95 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@ symfony_health_check:
- id: symfony_health_check.doctrine_check
ping_checks:
- id: symfony_health_check.status_up_check
# using this optional parameter you may set custom error code if some check fails. Response code must be a valid HTTP status code. Default response is 200l.
```
Change response code:
- default response code is 200.
- determine your custom response code in case of some check fails (Response code must be a valid HTTP status code)
```yaml
symfony_health_check:
health_checks:
- id: symfony_health_check.doctrine_check
ping_checks:
- id: symfony_health_check.status_up_check
ping_error_response_code: 500
health_error_response_code: 404
```
Expand Down Expand Up @@ -144,12 +153,12 @@ You can change the default behavior with a light configuration, remember to retu
health:
path: /your/custom/url
methods: GET
controller: SymfonyHealthCheckBundle\Controller\HealthController::healthCheckAction
controller: SymfonyHealthCheckBundle\Controller\HealthController::check
ping:
path: /your/custom/url
methods: GET
controller: SymfonyHealthCheckBundle\Controller\PingController::pingAction
controller: SymfonyHealthCheckBundle\Controller\PingController::check
```

Expand Down
64 changes: 64 additions & 0 deletions src/Controller/BaseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace SymfonyHealthCheckBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use SymfonyHealthCheckBundle\Check\CheckInterface;

abstract class BaseController extends AbstractController
{
/**
* @var array<CheckInterface>
*/
private array $checks = [];
private ?int $customResponseCode = null;

abstract public function check(): JsonResponse;

public function addHealthCheck(CheckInterface $check): void
{
$this->checks[] = $check;
}

public function setCustomResponseCode(?int $code): void
{
$this->customResponseCode = $code;
}

protected function checkAction(): JsonResponse
{
$checkResult = $this->performCheck();
$responseCode = $this->determineResponseCode($checkResult);

return new JsonResponse($checkResult, $responseCode);
}

protected function performCheck(): array
{
return array_map(
fn($healthCheck) => $healthCheck->check()->toArray(),
$this->checks
);
}

protected function determineResponseCode(array $results): int
{
$code = $this->customResponseCode;
$responseCode = Response::HTTP_OK;

if (null !== $code) {
foreach ($results as $result) {
if (!$result['result']) {
$responseCode = $code;
break;
}
}
}

return $responseCode;
}
}
41 changes: 3 additions & 38 deletions src/Controller/HealthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,20 @@

namespace SymfonyHealthCheckBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use SymfonyHealthCheckBundle\Check\CheckInterface;

final class HealthController extends AbstractController
final class HealthController extends BaseController
{
/**
* @var array<CheckInterface>
*/
private array $healthChecks = [];
private ?int $customResponseCode = null;

public function setCustomResponseCode(?int $code): void
{
$this->customResponseCode = $code;
}

public function addHealthCheck(CheckInterface $healthCheck): void
{
$this->healthChecks[] = $healthCheck;
}

/**
* @Route(
* path="/health",
* name="health",
* methods={"GET"}
* )
*/
public function healthCheckAction(): JsonResponse
public function check(): JsonResponse
{
$resultHealthCheck = array_map(
fn($healthCheck) => $healthCheck->check()->toArray(),
$this->healthChecks
);

$code = $this->customResponseCode;

if (null !== $code) {
foreach ($resultHealthCheck as $result) {
if (!$result['result']) {
$responseCode = $code;
break;
}
}
}

return new JsonResponse($resultHealthCheck, $responseCode ?? Response::HTTP_OK);
return $this->checkAction();
}
}
41 changes: 3 additions & 38 deletions src/Controller/PingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,20 @@

namespace SymfonyHealthCheckBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use SymfonyHealthCheckBundle\Check\CheckInterface;

final class PingController extends AbstractController
final class PingController extends BaseController
{
/**
* @var array<CheckInterface>
*/
private array $checks = [];
private ?int $customResponseCode = null;

public function addHealthCheck(CheckInterface $check): void
{
$this->checks[] = $check;
}

public function setCustomResponseCode(?int $code): void
{
$this->customResponseCode = $code;
}

/**
* @Route(
* path="/ping",
* name="ping",
* methods={"GET"}
* )
*/
public function pingAction(): JsonResponse
public function check(): JsonResponse
{
$pingCheck = array_map(
fn($healthCheck) => $healthCheck->check()->toArray(),
$this->checks
);

$code = $this->customResponseCode;

if (null !== $code) {
foreach ($pingCheck as $result) {
if (!$result['result']) {
$responseCode = $code;
break;
}
}
}

return new JsonResponse($pingCheck, $responseCode ?? Response::HTTP_OK);
return $this->checkAction();
}
}
4 changes: 2 additions & 2 deletions src/Resources/config/routes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<route
id="health"
path="/health"
controller="SymfonyHealthCheckBundle\Controller\HealthController::healthCheckAction"
controller="SymfonyHealthCheckBundle\Controller\HealthController::check"
methods="GET"
/>
<route
id="ping"
path="/ping"
controller="SymfonyHealthCheckBundle\Controller\PingController::pingAction"
controller="SymfonyHealthCheckBundle\Controller\PingController::check"
methods="GET"
/>
</routes>
14 changes: 7 additions & 7 deletions tests/Integration/Controller/HealthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testAddCheckStatusUpSuccess(): void
$healthController = new HealthController();
$healthController->addHealthCheck(new StatusUpCheck());

$response = $healthController->healthCheckAction();
$response = $healthController->check();

self::assertSame(200, $response->getStatusCode());
self::assertSame(
Expand All @@ -45,7 +45,7 @@ public function testEnvironmentCheckCouldNotDetermine(): void
$healthController = new HealthController();
$healthController->addHealthCheck(new EnvironmentCheck(new ContainerBuilder()));

$response = $healthController->healthCheckAction();
$response = $healthController->check();

self::assertSame(200, $response->getStatusCode());
self::assertSame(
Expand All @@ -64,7 +64,7 @@ public function testDoctrineCheckServiceNotFoundException(): void
$healthController = new HealthController();
$healthController->addHealthCheck(new DoctrineCheck(new ContainerBuilder()));

$response = $healthController->healthCheckAction();
$response = $healthController->check();
self::assertSame(200, $response->getStatusCode());
self::assertSame(
json_encode([[
Expand All @@ -83,7 +83,7 @@ public function testTwoCheckSuccess(): void
$healthController->addHealthCheck(new StatusUpCheck());
$healthController->addHealthCheck(new EnvironmentCheck(new ContainerBuilder()));

$response = $healthController->healthCheckAction();
$response = $healthController->check();

self::assertSame(200, $response->getStatusCode());
self::assertSame(
Expand All @@ -108,7 +108,7 @@ public function testEnvironmentCheckSuccess(): void
{
$healthController = new HealthController();
$healthController->addHealthCheck(new EnvironmentCheck(static::bootKernel()->getContainer()));
$response = $healthController->healthCheckAction();
$response = $healthController->check();

self::assertSame(200, $response->getStatusCode());
self::assertSame(
Expand Down Expand Up @@ -138,7 +138,7 @@ public function testCustomErrorCodeIfOneOfChecksIsFalse(): void
$healthController->addHealthCheck(new EnvironmentCheck(new ContainerBuilder()));
$healthController->setCustomResponseCode(500);

$response = $healthController->healthCheckAction();
$response = $healthController->check();

self::assertSame(500, $response->getStatusCode());
self::assertSame(
Expand All @@ -158,7 +158,7 @@ public function testCustomErrorCodeDoesNotAffectSuccessResponse(): void
$healthController->addHealthCheck(new StatusUpCheck());
$healthController->setCustomResponseCode(500);

$response = $healthController->healthCheckAction();
$response = $healthController->check();

self::assertSame(200, $response->getStatusCode());
self::assertSame(
Expand Down
14 changes: 7 additions & 7 deletions tests/Integration/Controller/PingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testAddCheckStatusUpSuccess(): void
$pingController = new PingController();
$pingController->addHealthCheck(new StatusUpCheck());

$response = $pingController->pingAction();
$response = $pingController->check();

self::assertSame(200, $response->getStatusCode());
self::assertSame(
Expand All @@ -45,7 +45,7 @@ public function testEnvironmentCheckCouldNotDetermine(): void
$pingController = new PingController();
$pingController->addHealthCheck(new EnvironmentCheck(new ContainerBuilder()));

$response = $pingController->pingAction();
$response = $pingController->check();

self::assertSame(200, $response->getStatusCode());
self::assertSame(
Expand All @@ -64,7 +64,7 @@ public function testDoctrineCheckServiceNotFoundException(): void
$pingController = new PingController();
$pingController->addHealthCheck(new DoctrineCheck(new ContainerBuilder()));

$response = $pingController->pingAction();
$response = $pingController->check();
self::assertSame(200, $response->getStatusCode());
self::assertSame(
json_encode([[
Expand All @@ -83,7 +83,7 @@ public function testTwoCheckSuccess(): void
$pingController->addHealthCheck(new StatusUpCheck());
$pingController->addHealthCheck(new EnvironmentCheck(new ContainerBuilder()));

$response = $pingController->pingAction();
$response = $pingController->check();

self::assertSame(200, $response->getStatusCode());
self::assertSame(
Expand Down Expand Up @@ -111,7 +111,7 @@ public function testCustomErrorCodeIfOneOfChecksIsFalse(): void
$pingController->addHealthCheck(new EnvironmentCheck(new ContainerBuilder()));
$pingController->setCustomResponseCode(500);

$response = $pingController->pingAction();
$response = $pingController->check();

self::assertSame(500, $response->getStatusCode());
self::assertSame(
Expand All @@ -138,7 +138,7 @@ public function testCustomErrorCodeDoesNotAffectSuccessResponse(): void
$pingController->addHealthCheck(new StatusUpCheck());
$pingController->setCustomResponseCode(500);

$response = $pingController->pingAction();
$response = $pingController->check();

self::assertSame(200, $response->getStatusCode());
self::assertSame(
Expand All @@ -153,7 +153,7 @@ public function testEnvironmentCheckSuccess(): void
{
$pingController = new PingController();
$pingController->addHealthCheck(new EnvironmentCheck(static::bootKernel()->getContainer()));
$response = $pingController->pingAction();
$response = $pingController->check();

self::assertSame(200, $response->getStatusCode());
self::assertSame(
Expand Down

0 comments on commit 22eda82

Please sign in to comment.