canSolve()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.5386
c 0
b 0
f 0
cc 7
nc 7
nop 1
1
<?php
2
3
namespace Facade\Ignition\SolutionProviders;
4
5
use Facade\Ignition\Solutions\UseDefaultValetDbCredentialsSolution;
6
use Facade\IgnitionContracts\HasSolutionsForThrowable;
7
use Illuminate\Database\QueryException;
8
use Throwable;
9
10
class IncorrectValetDbCredentialsSolutionProvider implements HasSolutionsForThrowable
11
{
12
    const MYSQL_ACCESS_DENIED_CODE = 1045;
13
14
    public function canSolve(Throwable $throwable): bool
15
    {
16
        if (! PHP_OS === 'Darwin') {
17
            return false;
18
        }
19
20
        if (! $throwable instanceof QueryException) {
21
            return false;
22
        }
23
24
        if (! $this->isAccessDeniedCode($throwable->getCode())) {
25
            return false;
26
        }
27
28
        if (! $this->envFileExists()) {
29
            return false;
30
        }
31
32
        if (! $this->isValetInstalled()) {
33
            return false;
34
        }
35
36
        if ($this->usingCorrectDefaultCredentials()) {
37
            return false;
38
        }
39
40
        return true;
41
    }
42
43
    public function getSolutions(Throwable $throwable): array
44
    {
45
        return [new UseDefaultValetDbCredentialsSolution()];
46
    }
47
48
    protected function envFileExists(): bool
49
    {
50
        return file_exists(base_path('.env'));
51
    }
52
53
    protected function isAccessDeniedCode($code): bool
54
    {
55
        return $code === static::MYSQL_ACCESS_DENIED_CODE;
56
    }
57
58
    protected function isValetInstalled(): bool
59
    {
60
        return file_exists('/usr/local/bin/valet');
61
    }
62
63
    protected function usingCorrectDefaultCredentials(): bool
64
    {
65
        return env('DB_USERNAME') === 'root' && env('DB_PASSWORD') === '';
66
    }
67
}
68