Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| HealthCheckHandler | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Health\Application\Query; |
| 6 | |
| 7 | use Doctrine\DBAL\Exception; |
| 8 | use Doctrine\ORM\EntityManagerInterface; |
| 9 | |
| 10 | class HealthCheckHandler |
| 11 | { |
| 12 | private EntityManagerInterface $entityManager; |
| 13 | |
| 14 | public function __construct(EntityManagerInterface $entityManager) |
| 15 | { |
| 16 | $this->entityManager = $entityManager; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @return array{status: string, services: array{database: string}} |
| 21 | */ |
| 22 | public function handle(HealthCheckQuery $_query): array |
| 23 | { |
| 24 | $dbStatus = 'ok'; |
| 25 | |
| 26 | try { |
| 27 | $connection = $this->entityManager->getConnection(); |
| 28 | $connection->executeQuery('SELECT 1'); |
| 29 | } catch (Exception $e) { |
| 30 | $dbStatus = 'error'; |
| 31 | } |
| 32 | |
| 33 | return [ |
| 34 | 'status' => $dbStatus === 'ok' ? 'ok' : 'error', |
| 35 | 'services' => [ |
| 36 | 'database' => $dbStatus, |
| 37 | ], |
| 38 | ]; |
| 39 | } |
| 40 | } |