Completed
Pull Request — 2.0 (#103)
by
unknown
14:40 queued 12:24
created

ConfigureDatabase::askDatabasePort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Modules\Core\Console\Installers\Scripts;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Contracts\Config\Repository as Config;
7
use Modules\Core\Console\Installers\SetupScript;
8
use Modules\Core\Console\Installers\Writers\EnvFileWriter;
9
use PDOException;
10
11
class ConfigureDatabase implements SetupScript
12
{
13
    /**
14
     * @var
15
     */
16
    protected $config;
17
18
    /**
19
     * @var EnvFileWriter
20
     */
21
    protected $env;
22
23
    /**
24
     * @param Config        $config
25
     * @param EnvFileWriter $env
26
     */
27
    public function __construct(Config $config, EnvFileWriter $env)
28
    {
29
        $this->config = $config;
30
        $this->env = $env;
31
    }
32
33
    /**
34
     * @var Command
35
     */
36
    protected $command;
37
38
    /**
39
     * Fire the install script
40
     * @param  Command $command
41
     * @return mixed
42
     */
43
    public function fire(Command $command)
44
    {
45
        $this->command = $command;
46
47
        $connected = false;
48
49
        while (! $connected) {
50
            $driver = $this->askDatabaseDriver();
51
            $host = $this->askDatabaseHost();
52
            $port = $this->askDatabasePort($driver);
53
            $name = $this->askDatabaseName();
54
            $user = $this->askDatabaseUsername();
55
            $password = $this->askDatabasePassword();
56
57
            $this->setLaravelConfiguration($driver, $host, $port, $name, $user, $password);
58
59
            if ($this->databaseConnectionIsValid()) {
60
                $connected = true;
61
            } else {
62
                $command->error("Please ensure your database credentials are valid.");
63
            }
64
        }
65
66
        $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...
67
68
        $command->info('Database successfully configured');
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    protected function askDatabaseDriver()
75
    {
76
        $driver = $this->command->ask('Enter your database driver (e.g. mysql, pgsql)', 'mysql');
77
        return $driver;
78
    }
79
80
    
81
    /**
82
     * @return string
83
     */
84
    protected function askDatabaseHost()
85
    {
86
        $host = $this->command->ask('Enter your database host', 'localhost');
87
88
        return $host;
89
    }
90
    
91
    /**
92
     * @return string
93
     */
94
    protected function askDatabasePort($driver)
95
    {
96
        $port = $this->command->ask('Enter your database port', $this->config['database.connections.'.$driver.'.port']);
97
        return $port;
98
    }
99
100
    /**
101
     * @return string
102
     */
103 View Code Duplication
    protected function askDatabaseName()
104
    {
105
        do {
106
            $name = $this->command->ask('Enter your database name', 'homestead');
107
            if ($name == '') {
108
                $this->command->error('Database name is required');
109
            }
110
        } while (!$name);
111
112
        return $name;
113
    }
114
115
    /**
116
     * @param
117
     * @return string
118
     */
119 View Code Duplication
    protected function askDatabaseUsername()
120
    {
121
        do {
122
            $user = $this->command->ask('Enter your database username', 'homestead');
123
            if ($user == '') {
124
                $this->command->error('Database username is required');
125
            }
126
        } while (!$user);
127
128
        return $user;
129
    }
130
131
    /**
132
     * @param
133
     * @return string
134
     */
135
    protected function askDatabasePassword()
136
    {
137
        $databasePassword = $this->command->ask('Enter your database password (leave <none> for no password)', 'secret');
138
139
        return ($databasePassword === '<none>') ? '' : $databasePassword;
140
    }
141
142
    /**
143
     * @param $driver
144
     * @param $name
145
     * @param $port
146
     * @param $user
147
     * @param $password
148
     */
149
    protected function setLaravelConfiguration($driver, $host, $port, $name, $user, $password)
150
    {
151
        $this->config['database.default'] = $driver;
152
        $this->config['database.connections.'.$driver.'.host'] = $host;
153
        $this->config['database.connections.'.$driver.'.port'] = $port;
154
        $this->config['database.connections.'.$driver.'.database'] = $name;
155
        $this->config['database.connections.'.$driver.'.username'] = $user;
156
        $this->config['database.connections.'.$driver.'.password'] = $password;
157
    }
158
159
    /**
160
     * Is the database connection valid?
161
     * @return bool
162
     */
163
    protected function databaseConnectionIsValid()
164
    {
165
        try {
166
            app('db')->reconnect();
167
168
            return true;
169
        } catch (PDOException $e) {
170
            return false;
171
        }
172
    }
173
}
174