Skip to content

Commit 22eda82

Browse files
committed
fix: add base controller
1 parent a72f628 commit 22eda82

File tree

7 files changed

+98
-95
lines changed

7 files changed

+98
-95
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,16 @@ symfony_health_check:
7070
- id: symfony_health_check.doctrine_check
7171
ping_checks:
7272
- id: symfony_health_check.status_up_check
73-
# 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.
73+
```
74+
Change response code:
75+
- default response code is 200.
76+
- determine your custom response code in case of some check fails (Response code must be a valid HTTP status code)
77+
```yaml
78+
symfony_health_check:
79+
health_checks:
80+
- id: symfony_health_check.doctrine_check
81+
ping_checks:
82+
- id: symfony_health_check.status_up_check
7483
ping_error_response_code: 500
7584
health_error_response_code: 404
7685
```
@@ -144,12 +153,12 @@ You can change the default behavior with a light configuration, remember to retu
144153
health:
145154
path: /your/custom/url
146155
methods: GET
147-
controller: SymfonyHealthCheckBundle\Controller\HealthController::healthCheckAction
156+
controller: SymfonyHealthCheckBundle\Controller\HealthController::check
148157
149158
ping:
150159
path: /your/custom/url
151160
methods: GET
152-
controller: SymfonyHealthCheckBundle\Controller\PingController::pingAction
161+
controller: SymfonyHealthCheckBundle\Controller\PingController::check
153162
154163
```
155164

src/Controller/BaseController.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyHealthCheckBundle\Controller;
6+
7+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8+
use Symfony\Component\HttpFoundation\JsonResponse;
9+
use Symfony\Component\HttpFoundation\Response;
10+
use SymfonyHealthCheckBundle\Check\CheckInterface;
11+
12+
abstract class BaseController extends AbstractController
13+
{
14+
/**
15+
* @var array<CheckInterface>
16+
*/
17+
private array $checks = [];
18+
private ?int $customResponseCode = null;
19+
20+
abstract public function check(): JsonResponse;
21+
22+
public function addHealthCheck(CheckInterface $check): void
23+
{
24+
$this->checks[] = $check;
25+
}
26+
27+
public function setCustomResponseCode(?int $code): void
28+
{
29+
$this->customResponseCode = $code;
30+
}
31+
32+
protected function checkAction(): JsonResponse
33+
{
34+
$checkResult = $this->performCheck();
35+
$responseCode = $this->determineResponseCode($checkResult);
36+
37+
return new JsonResponse($checkResult, $responseCode);
38+
}
39+
40+
protected function performCheck(): array
41+
{
42+
return array_map(
43+
fn($healthCheck) => $healthCheck->check()->toArray(),
44+
$this->checks
45+
);
46+
}
47+
48+
protected function determineResponseCode(array $results): int
49+
{
50+
$code = $this->customResponseCode;
51+
$responseCode = Response::HTTP_OK;
52+
53+
if (null !== $code) {
54+
foreach ($results as $result) {
55+
if (!$result['result']) {
56+
$responseCode = $code;
57+
break;
58+
}
59+
}
60+
}
61+
62+
return $responseCode;
63+
}
64+
}

src/Controller/HealthController.php

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,20 @@
44

55
namespace SymfonyHealthCheckBundle\Controller;
66

7-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
87
use Symfony\Component\HttpFoundation\JsonResponse;
9-
use Symfony\Component\HttpFoundation\Response;
108
use Symfony\Component\Routing\Annotation\Route;
11-
use SymfonyHealthCheckBundle\Check\CheckInterface;
129

13-
final class HealthController extends AbstractController
10+
final class HealthController extends BaseController
1411
{
15-
/**
16-
* @var array<CheckInterface>
17-
*/
18-
private array $healthChecks = [];
19-
private ?int $customResponseCode = null;
20-
21-
public function setCustomResponseCode(?int $code): void
22-
{
23-
$this->customResponseCode = $code;
24-
}
25-
26-
public function addHealthCheck(CheckInterface $healthCheck): void
27-
{
28-
$this->healthChecks[] = $healthCheck;
29-
}
30-
3112
/**
3213
* @Route(
3314
* path="/health",
3415
* name="health",
3516
* methods={"GET"}
3617
* )
3718
*/
38-
public function healthCheckAction(): JsonResponse
19+
public function check(): JsonResponse
3920
{
40-
$resultHealthCheck = array_map(
41-
fn($healthCheck) => $healthCheck->check()->toArray(),
42-
$this->healthChecks
43-
);
44-
45-
$code = $this->customResponseCode;
46-
47-
if (null !== $code) {
48-
foreach ($resultHealthCheck as $result) {
49-
if (!$result['result']) {
50-
$responseCode = $code;
51-
break;
52-
}
53-
}
54-
}
55-
56-
return new JsonResponse($resultHealthCheck, $responseCode ?? Response::HTTP_OK);
21+
return $this->checkAction();
5722
}
5823
}

src/Controller/PingController.php

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,20 @@
44

55
namespace SymfonyHealthCheckBundle\Controller;
66

7-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
87
use Symfony\Component\HttpFoundation\JsonResponse;
9-
use Symfony\Component\HttpFoundation\Response;
108
use Symfony\Component\Routing\Annotation\Route;
11-
use SymfonyHealthCheckBundle\Check\CheckInterface;
129

13-
final class PingController extends AbstractController
10+
final class PingController extends BaseController
1411
{
15-
/**
16-
* @var array<CheckInterface>
17-
*/
18-
private array $checks = [];
19-
private ?int $customResponseCode = null;
20-
21-
public function addHealthCheck(CheckInterface $check): void
22-
{
23-
$this->checks[] = $check;
24-
}
25-
26-
public function setCustomResponseCode(?int $code): void
27-
{
28-
$this->customResponseCode = $code;
29-
}
30-
3112
/**
3213
* @Route(
3314
* path="/ping",
3415
* name="ping",
3516
* methods={"GET"}
3617
* )
3718
*/
38-
public function pingAction(): JsonResponse
19+
public function check(): JsonResponse
3920
{
40-
$pingCheck = array_map(
41-
fn($healthCheck) => $healthCheck->check()->toArray(),
42-
$this->checks
43-
);
44-
45-
$code = $this->customResponseCode;
46-
47-
if (null !== $code) {
48-
foreach ($pingCheck as $result) {
49-
if (!$result['result']) {
50-
$responseCode = $code;
51-
break;
52-
}
53-
}
54-
}
55-
56-
return new JsonResponse($pingCheck, $responseCode ?? Response::HTTP_OK);
21+
return $this->checkAction();
5722
}
5823
}

src/Resources/config/routes.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
<route
88
id="health"
99
path="/health"
10-
controller="SymfonyHealthCheckBundle\Controller\HealthController::healthCheckAction"
10+
controller="SymfonyHealthCheckBundle\Controller\HealthController::check"
1111
methods="GET"
1212
/>
1313
<route
1414
id="ping"
1515
path="/ping"
16-
controller="SymfonyHealthCheckBundle\Controller\PingController::pingAction"
16+
controller="SymfonyHealthCheckBundle\Controller\PingController::check"
1717
methods="GET"
1818
/>
1919
</routes>

tests/Integration/Controller/HealthControllerTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testAddCheckStatusUpSuccess(): void
2929
$healthController = new HealthController();
3030
$healthController->addHealthCheck(new StatusUpCheck());
3131

32-
$response = $healthController->healthCheckAction();
32+
$response = $healthController->check();
3333

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

48-
$response = $healthController->healthCheckAction();
48+
$response = $healthController->check();
4949

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

67-
$response = $healthController->healthCheckAction();
67+
$response = $healthController->check();
6868
self::assertSame(200, $response->getStatusCode());
6969
self::assertSame(
7070
json_encode([[
@@ -83,7 +83,7 @@ public function testTwoCheckSuccess(): void
8383
$healthController->addHealthCheck(new StatusUpCheck());
8484
$healthController->addHealthCheck(new EnvironmentCheck(new ContainerBuilder()));
8585

86-
$response = $healthController->healthCheckAction();
86+
$response = $healthController->check();
8787

8888
self::assertSame(200, $response->getStatusCode());
8989
self::assertSame(
@@ -108,7 +108,7 @@ public function testEnvironmentCheckSuccess(): void
108108
{
109109
$healthController = new HealthController();
110110
$healthController->addHealthCheck(new EnvironmentCheck(static::bootKernel()->getContainer()));
111-
$response = $healthController->healthCheckAction();
111+
$response = $healthController->check();
112112

113113
self::assertSame(200, $response->getStatusCode());
114114
self::assertSame(
@@ -138,7 +138,7 @@ public function testCustomErrorCodeIfOneOfChecksIsFalse(): void
138138
$healthController->addHealthCheck(new EnvironmentCheck(new ContainerBuilder()));
139139
$healthController->setCustomResponseCode(500);
140140

141-
$response = $healthController->healthCheckAction();
141+
$response = $healthController->check();
142142

143143
self::assertSame(500, $response->getStatusCode());
144144
self::assertSame(
@@ -158,7 +158,7 @@ public function testCustomErrorCodeDoesNotAffectSuccessResponse(): void
158158
$healthController->addHealthCheck(new StatusUpCheck());
159159
$healthController->setCustomResponseCode(500);
160160

161-
$response = $healthController->healthCheckAction();
161+
$response = $healthController->check();
162162

163163
self::assertSame(200, $response->getStatusCode());
164164
self::assertSame(

tests/Integration/Controller/PingControllerTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testAddCheckStatusUpSuccess(): void
2929
$pingController = new PingController();
3030
$pingController->addHealthCheck(new StatusUpCheck());
3131

32-
$response = $pingController->pingAction();
32+
$response = $pingController->check();
3333

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

48-
$response = $pingController->pingAction();
48+
$response = $pingController->check();
4949

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

67-
$response = $pingController->pingAction();
67+
$response = $pingController->check();
6868
self::assertSame(200, $response->getStatusCode());
6969
self::assertSame(
7070
json_encode([[
@@ -83,7 +83,7 @@ public function testTwoCheckSuccess(): void
8383
$pingController->addHealthCheck(new StatusUpCheck());
8484
$pingController->addHealthCheck(new EnvironmentCheck(new ContainerBuilder()));
8585

86-
$response = $pingController->pingAction();
86+
$response = $pingController->check();
8787

8888
self::assertSame(200, $response->getStatusCode());
8989
self::assertSame(
@@ -111,7 +111,7 @@ public function testCustomErrorCodeIfOneOfChecksIsFalse(): void
111111
$pingController->addHealthCheck(new EnvironmentCheck(new ContainerBuilder()));
112112
$pingController->setCustomResponseCode(500);
113113

114-
$response = $pingController->pingAction();
114+
$response = $pingController->check();
115115

116116
self::assertSame(500, $response->getStatusCode());
117117
self::assertSame(
@@ -138,7 +138,7 @@ public function testCustomErrorCodeDoesNotAffectSuccessResponse(): void
138138
$pingController->addHealthCheck(new StatusUpCheck());
139139
$pingController->setCustomResponseCode(500);
140140

141-
$response = $pingController->pingAction();
141+
$response = $pingController->check();
142142

143143
self::assertSame(200, $response->getStatusCode());
144144
self::assertSame(
@@ -153,7 +153,7 @@ public function testEnvironmentCheckSuccess(): void
153153
{
154154
$pingController = new PingController();
155155
$pingController->addHealthCheck(new EnvironmentCheck(static::bootKernel()->getContainer()));
156-
$response = $pingController->pingAction();
156+
$response = $pingController->check();
157157

158158
self::assertSame(200, $response->getStatusCode());
159159
self::assertSame(

0 commit comments

Comments
 (0)