Completed
Pull Request — master (#60)
by
unknown
177:15
created

DatabaseCanBeAccessed::check()   A

Complexity

Conditions 4
Paths 14

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.7
c 0
b 0
f 0
cc 4
nc 14
nop 1
crap 4
1
<?php
2
3
namespace BeyondCode\SelfDiagnosis\Checks;
4
5
use DB;
6
7
class DatabaseCanBeAccessed implements Check
8
{
9
    private $message;
10
11
    /**
12
     * The name of the check.
13
     *
14
     * @param array $config
15
     * @return string
16
     */
17 4
    public function name(array $config): string
18
    {
19 4
        return trans('self-diagnosis::checks.database_can_be_accessed.name');
20
    }
21
22
    /**
23
     * Perform the actual verification of this check.
24
     *
25
     * @param array $config
26
     * @return bool
27
     */
28 8
    public function check(array $config): bool
29
    {
30
        try {
31 8
            if (array_get($config, 'default_connection', true)) {
32 8
                DB::connection()->getPdo();
33
            }
34
35 8
            foreach (array_get($config, 'connections', []) as $connection) {
36 4
                DB::connection($connection)->getPdo();
37
            }
38
39 8
            return true;
40 8
        } catch (\Exception $e) {
41 8
            $this->message = $e->getMessage();
42
        }
43 8
        return false;
44
    }
45
46
    /**
47
     * The error message to display in case the check does not pass.
48
     *
49
     * @param array $config
50
     * @return string
51
     */
52 4
    public function message(array $config): string
53
    {
54 4
        return trans('self-diagnosis::checks.database_can_be_accessed.message', [
55 4
            'error' => $this->message,
56
        ]);
57
    }
58
}
59