Skip to content

Commit 33335e0

Browse files
committed
Add early return
1 parent 9bde766 commit 33335e0

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/HealthCheck/DoctrineConnectionHealthCheck.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,41 @@ class DoctrineConnectionHealthCheck implements HealthCheckInterface
3535

3636
public function __construct(
3737
#[Autowire(service: 'doctrine.dbal.default_connection')]
38-
private readonly ?Connection $connection
39-
) {
38+
private readonly ?Connection $connection,
39+
)
40+
{
4041
}
4142

4243
public function check(HealthReportInterface $report): HealthReportInterface
4344
{
44-
if (!is_null($this->connection)) {
45-
try {
46-
$this->connection->connect(); // This will create the SQLite database if it does not exist
47-
// Get the schema manager and grab the first table to later query on
48-
$sm = $this->connection->createSchemaManager();
49-
50-
$tables = $sm->listTables();
45+
// Skip database checking of no database is configured.
46+
if ($this->connection === null) {
47+
return $report;
48+
}
5149

52-
if ($tables === []) {
53-
return HealthReport::buildStatusDown('No tables found in the database.');
54-
}
50+
try {
51+
$this->connection->connect(); // This will create the SQLite database if it does not exist
52+
// Get the schema manager
53+
$sm = $this->connection->createSchemaManager();
5554

56-
$table = reset($tables);
57-
// Perform a light-weight query on the chosen table
58-
$query = "SELECT * FROM %s LIMIT 1";
59-
$this->connection->executeQuery(sprintf($query, $table->getName()));
55+
$tables = $sm->listTables();
6056

61-
} catch (ConnectionException) {
62-
return HealthReport::buildStatusDown('Unable to connect to the database.');
63-
} catch (Exception) {
64-
return HealthReport::buildStatusDown('Unable to execute a query on the database.');
57+
if ($tables === []) {
58+
return HealthReport::buildStatusDown('No tables found in the database.');
6559
}
60+
61+
// Grab the first table to query on
62+
$table = reset($tables);
63+
// Perform a light-weight query on the chosen table
64+
$query = "SELECT * FROM %s LIMIT 1";
65+
$this->connection->executeQuery(sprintf($query, $table->getName()));
66+
67+
} catch (ConnectionException) {
68+
return HealthReport::buildStatusDown('Unable to connect to the database.');
69+
} catch (Exception) {
70+
return HealthReport::buildStatusDown('Unable to execute a query on the database.');
6671
}
72+
6773
return $report;
6874
}
6975
}

0 commit comments

Comments
 (0)