CreateMysqlDatabase   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 152
ccs 64
cts 64
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A prompt() 0 4 1
A prepare() 0 15 2
A getDatabaseConfigs() 0 16 2
A getOtherConnectionUser() 0 8 1
A generateSqlCommands() 0 12 1
A generateConsoleCommand() 0 10 2
A preview() 0 6 1
A finish() 0 11 1
1
<?php namespace Lanin\Laravel\SetupWizard\Commands\Steps;
2
3
use Illuminate\Console\Command;
4
use Illuminate\Database\Connection;
5
use Symfony\Component\Process\Process;
6
7
class CreateMysqlDatabase extends AbstractStep
8
{
9
    /**
10
     * @var Connection
11
     */
12
    protected $connection;
13
14
    /**
15
     * @inheritDoc
16
     */
17 20
    public function __construct(Command $command)
18
    {
19 20
        parent::__construct($command);
20
21 20
        $this->connection = \DB::connection();
22 20
    }
23
24
25
    /**
26
     * Return command prompt text.
27
     *
28
     * @return string
29
     */
30 2
    public function prompt()
31
    {
32 2
        return 'Do you want to create MySQL database?';
33
    }
34
35
    /**
36
     * Prepare step data.
37
     *
38
     * @return mixed
39
     */
40 4
    public function prepare()
41
    {
42 4
        $return = [];
43
44 4
        $this->getDatabaseConfigs($return);
45
46 4
        if ($this->command->confirm('Do you want to provide an other user to connect to DB?', false))
47 4
        {
48 2
            $this->getOtherConnectionUser($return);
49 2
        }
50
51 4
        $this->generateSqlCommands($return);
52
53 4
        return $return;
54
    }
55
56
    /**
57
     * Get database config.
58
     *
59
     * @param array $return
60
     */
61 4
    protected function getDatabaseConfigs(array &$return)
62
    {
63 4
        $return['localhost'] = 'localhost';
64
65 4
        $return['host']     = $this->connection->getConfig('host');
66 4
        $return['database'] = $this->connection->getConfig('database');
67 4
        $return['username'] = $this->connection->getConfig('username');
68 4
        $return['password'] = $this->connection->getConfig('password');
69 4
        $return['connection_username'] = $return['username'];
70 4
        $return['connection_password'] = $return['password'];
71
72 4
        if ( ! in_array($return['host'], ['localhost', '127.0.0.1']))
73 4
        {
74 4
            $return['localhost'] = $this->command->ask('Database is on the other server. Provide local IP/hostname.');
75 4
        }
76 4
    }
77
78
    /**
79
     * Ask info about 'root' user.
80
     *
81
     * @param array $return
82
     */
83 2
    protected function getOtherConnectionUser(array &$return)
84
    {
85 2
        $return['connection_username'] = $this->command->ask(
86 2
            'Provide user\'s login with <comment>CREATE DATABASE</comment> grants',
87
            'root'
88 2
        );
89 2
        $return['connection_password'] = $this->command->secret('Password');
90 2
    }
91
92
    /**
93
     * Generate SQL commands.
94
     *
95
     * @param array $return
96
     */
97 4
    protected function generateSqlCommands(array &$return)
98
    {
99 4
        $return['commands']   = [];
100 4
        $return['commands'][] = "CREATE DATABASE IF NOT EXISTS {$return['database']};";
101 4
        $return['commands'][] = sprintf(
102 4
            "GRANT ALL PRIVILEGES ON %s.* TO %s@%s IDENTIFIED BY '%s';",
103 4
            $return['database'],
104 4
            $return['username'],
105 4
            $return['localhost'],
106 4
            $return['password']
107 4
        );
108 4
    }
109
110
    /**
111
     * Generate terminal command.
112
     *
113
     * @param  array $results
114
     * @param  bool $full
115
     * @return string
116
     */
117 2
    protected function generateConsoleCommand($results, $full = false)
118
    {
119 2
        return sprintf(
120 2
            "mysql -u\"%s\" -p\"%s\" -h\"%s\" -e\"%s\"",
121 2
            $results['connection_username'],
122 2
            $full ? $results['connection_password'] : '******',
123 2
            $results['host'],
124 2
            join(' ', $results['commands'])
125 2
        );
126
    }
127
128
    /**
129
     * Preview results.
130
     *
131
     * @param  mixed $results
132
     * @return void
133
     */
134 2
    public function preview($results)
135
    {
136 2
        $this->command->info(
137 2
            "This command will be executed: <comment>" . $this->generateConsoleCommand($results) . "</comment>"
138 2
        );
139 2
    }
140
141
    /**
142
     * Finish step.
143
     *
144
     * @param  mixed $results
145
     * @return bool
146
     */
147 2
    public function finish($results)
148
    {
149 2
        $process = new Process($this->generateConsoleCommand($results, true));
150
151 2
        $process->run(function ($type, $output)
152
        {
153 2
            $this->command->line($output);
154 2
        });
155
156 2
        return ! (bool) $process->getExitCode();
157
    }
158
}