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

DefaultDbNameSolutionProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A canSolve() 0 14 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
                if ($this->isUnknownDatabaseCode($e->getCode())) {
21
                    return in_array(env('DB_DATABASE'), ['homestead', 'laravel']);
22
                }
23
            }
24
        }
25
26
        return false;
27
    }
28
29
    public function getSolutions(Throwable $throwable): array
30
    {
31
        return [new SuggestUsingCorrectDbNameSolution()];
32
    }
33
34
    protected function canTryDatabaseConnection()
35
    {
36
        return version_compare(app()->version(), '5.6.28', '>');
37
    }
38
39
    protected function isUnknownDatabaseCode($code): bool
40
    {
41
        return $code === static::MYSQL_UNKNOWN_DATABASE_CODE;
42
    }
43
}
44