generated from vormkracht10/laravel-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from vormkracht10/feature/database-check
add DatabaseCheck
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Vormkracht10\LaravelOK\Checks; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use Vormkracht10\LaravelOK\Checks\Base\Check; | ||
use Vormkracht10\LaravelOK\Checks\Base\Result; | ||
|
||
class DatabaseCheck extends Check | ||
{ | ||
protected string $connectionName; | ||
|
||
public function onConnection(string $name): static | ||
{ | ||
$this->connectionName = $name; | ||
|
||
return $this; | ||
} | ||
|
||
public function run(): Result | ||
{ | ||
$result = Result::new(); | ||
|
||
$connectionName = $this->connectionName ?? config('database.default'); | ||
|
||
try { | ||
DB::connection($connectionName)->getPdo(); | ||
} catch (\Throwable) { | ||
return $result->failed("Could not connect to database on connection [{$connectionName}]"); | ||
} | ||
|
||
return $result->ok('Connected to database successfully'); | ||
} | ||
} |