UseDefaultValetDbCredentialsSolution   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 57
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getSolutionActionDescription() 0 4 1
A getRunButtonText() 0 4 1
A getSolutionTitle() 0 4 1
A run() 0 9 2
A ensureLineExists() 0 12 2
A getDocumentationLinks() 0 4 1
A getRunParameters() 0 6 1
A getSolutionDescription() 0 4 1
1
<?php
2
3
namespace Facade\Ignition\Solutions;
4
5
use Facade\IgnitionContracts\RunnableSolution;
6
use Illuminate\Support\Str;
7
8
class UseDefaultValetDbCredentialsSolution implements RunnableSolution
9
{
10
    public function getSolutionActionDescription(): string
11
    {
12
        return 'Pressing the button below will change `DB_USER` and `DB_PASSWORD` in your `.env` file.';
13
    }
14
15
    public function getRunButtonText(): string
16
    {
17
        return 'Use default Valet credentials';
18
    }
19
20
    public function getSolutionTitle(): string
21
    {
22
        return 'Could not connect to database';
23
    }
24
25
    public function run(array $parameters = [])
26
    {
27
        if (! file_exists(base_path('.env'))) {
28
            return;
29
        }
30
31
        $this->ensureLineExists('DB_USERNAME', 'root');
32
        $this->ensureLineExists('DB_PASSWORD', '');
33
    }
34
35
    protected function ensureLineExists(string $key, string $value)
36
    {
37
        $envPath = base_path('.env');
38
39
        $envLines = array_map(function (string $envLine) use ($value, $key) {
40
            return Str::startsWith($envLine, $key)
41
                ? "{$key}={$value}".PHP_EOL
42
                : $envLine;
43
        }, file($envPath));
44
45
        file_put_contents($envPath, implode('', $envLines));
46
    }
47
48
    public function getDocumentationLinks(): array
49
    {
50
        return [];
51
    }
52
53
    public function getRunParameters(): array
54
    {
55
        return [
56
            'Valet documentation' => 'https://laravel.com/docs/master/valet',
57
        ];
58
    }
59
60
    public function getSolutionDescription(): string
61
    {
62
        return 'You seem to be using Valet, but the .env file does not contain the right default database credentials.';
63
    }
64
}
65