Passed
Push — master ( af837d...283125 )
by Martin
06:56
created

CheckDBConnection::handle()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\DB;
7
8
class CheckDBConnection extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'app:check-db-connection';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Checks if the database is reachable and ready to receive queries.';
23
24
    /**
25
     * Create a new command instance.
26
     *
27
     * @return void
28
     */
29
    public function __construct()
30
    {
31
        parent::__construct();
32
    }
33
34
    /**
35
     * Execute the console command.
36
     *
37
     * @return mixed
38
     */
39
    public function handle()
40
    {
41
        try {
42
            DB::statement('show tables');
43
            $this->info('Database connection available!');
44
        } catch( \Illuminate\Database\QueryException $e) {
45
            $this->error('Database connection not available!');
46
            exit(1); // return a non-0-code to be used in shell scripts as a falsy exit state
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
47
        }
48
    }
49
}
50