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

isUnknownDatabaseCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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