Completed
Push — develop ( 204cbc...09ab55 )
by Nicolas
02:41
created

ConfigureDatabase::databaseConnectionIsValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
1
<?php namespace Modules\Core\Console\Installers\Scripts;
2
3
use Illuminate\Console\Command;
4
use Illuminate\Contracts\Config\Repository as Config;
5
use Modules\Core\Console\Installers\SetupScript;
6
use Modules\Core\Console\Installers\Writers\EnvFileWriter;
7
use PDOException;
8
9
class ConfigureDatabase implements SetupScript
10
{
11
    /**
12
     * @var
13
     */
14
    protected $config;
15
16
    /**
17
     * @var EnvFileWriter
18
     */
19
    protected $env;
20
21
    /**
22
     * @param Config        $config
23
     * @param EnvFileWriter $env
24
     */
25
    public function __construct(Config $config, EnvFileWriter $env)
26
    {
27
        $this->config = $config;
28
        $this->env = $env;
29
    }
30
31
    /**
32
     * @var Command
33
     */
34
    protected $command;
35
36
    /**
37
     * Fire the install script
38
     * @param  Command $command
39
     * @return mixed
40
     */
41
    public function fire(Command $command)
42
    {
43
        $this->command = $command;
44
45
        $connected = false;
46
47
        while (! $connected) {
48
            $host = $this->askDatabaseHost();
49
50
            $name = $this->askDatabaseName();
51
52
            $user = $this->askDatabaseUsername();
53
54
            $password = $this->askDatabasePassword();
55
56
            $this->setLaravelConfiguration($name, $user, $password, $host);
57
58
            if ($this->databaseConnectionIsValid()) {
59
                $connected = true;
60
            } else {
61
                $command->error("Please ensure your database credentials are valid.");
62
            }
63
        }
64
65
        $this->env->write($name, $user, $password, $host);
0 ignored issues
show
Bug introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $user does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $password does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $host does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
66
67
        $command->info('Database successfully configured');
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    protected function askDatabaseHost()
74
    {
75
        $host = $this->command->ask('Enter your database host', 'localhost');
76
77
        return $host;
78
    }
79
80
    /**
81
     * @return string
82
     */
83 View Code Duplication
    protected function askDatabaseName()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        do {
86
            $name = $this->command->ask('Enter your database name');
87
            if ($name == '') {
88
                $this->command->error('Database name is required');
89
            }
90
        } while (!$name);
91
92
        return $name;
93
    }
94
95
    /**
96
     * @param
97
     * @return string
98
     */
99 View Code Duplication
    protected function askDatabaseUsername()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        do {
102
            $user = $this->command->ask('Enter your database username', 'root');
103
            if ($user == '') {
104
                $this->command->error('Database username is required');
105
            }
106
        } while (!$user);
107
108
        return $user;
109
    }
110
111
    /**
112
     * @param
113
     * @return string
114
     */
115
    protected function askDatabasePassword()
116
    {
117
        $databasePassword = $this->command->ask('Enter your database password (leave <none> for no password)', '<none>');
118
119
        return ($databasePassword === '<none>') ? '' : $databasePassword;
120
    }
121
122
    /**
123
     * @param $name
124
     * @param $user
125
     * @param $password
126
     */
127
    protected function setLaravelConfiguration($name, $user, $password, $host)
128
    {
129
        $this->config['database.connections.mysql.host'] = $host;
130
        $this->config['database.connections.mysql.database'] = $name;
131
        $this->config['database.connections.mysql.username'] = $user;
132
        $this->config['database.connections.mysql.password'] = $password;
133
    }
134
135
    /**
136
     * Is the database connection valid?
137
     * @return bool
138
     */
139
    protected function databaseConnectionIsValid()
140
    {
141
        try {
142
            app('db')->reconnect();
143
144
            return true;
145
        } catch (PDOException $e) {
146
            return false;
147
        }
148
    }
149
}
150