Completed
Pull Request — master (#277)
by
unknown
01:28
created

DefaultDbNameSolutionProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A canSolve() 0 15 4
A getSolutions() 0 4 1
A canTryDatabaseConnection() 0 4 1
A isUnknownDatabaseCode() 0 4 1
1
<?php
2
3
namespace Facade\Ignition\SolutionProviders;
4
5
use Facade\Ignition\Solutions\SuggestUsingCorrectDbNameSolution;
6
use Facade\IgnitionContracts\HasSolutionsForThrowable;
7
use Illuminate\Support\Facades\DB;
8
use Throwable;
9
10
class DefaultDbNameSolutionProvider implements HasSolutionsForThrowable
11
{
12
    const MYSQL_UNKNOWN_DATABASE_CODE = 1049;
13
14
    public function canSolve(Throwable $throwable): bool
15
    {
16
        if ($this->canTryDatabaseConnection()) {
17
            try {
18
                DB::connection()->select('SELECT 1');
19
            } catch (\Exception $e) {
20
21
                if ($this->isUnknownDatabaseCode($e->getCode())) {
22
                    return in_array(env('DB_DATABASE'), ['homestead', 'laravel']);
23
                }
24
            }
25
        }
26
27
        return false;
28
    }
29
30
    public function getSolutions(Throwable $throwable): array
31
    {
32
        return [new SuggestUsingCorrectDbNameSolution()];
33
    }
34
35
    protected function canTryDatabaseConnection()
36
    {
37
        return version_compare(app()->version(), '5.6.28', '>');
38
    }
39
40
    protected function isUnknownDatabaseCode($code): bool
41
    {
42
        return $code === static::MYSQL_UNKNOWN_DATABASE_CODE;
43
    }
44
}
45