Completed
Push — master ( a63b69...737fe7 )
by Michael
03:01
created

InstallCommand::cleanDatabase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
ccs 0
cts 15
cp 0
rs 9.3142
cc 2
eloc 11
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Stats\Commands;
4
5
use Joomla\Controller\AbstractController;
6
use Joomla\Database\DatabaseDriver;
7
use Stats\CommandInterface;
8
9
/**
10
 * Snapshot command
11
 *
12
 * @method         \Stats\CliApplication  getApplication()  Get the application object.
13
 * @property-read  \Stats\CliApplication  $app              Application object
14
 *
15
 * @since          1.0
16
 */
17
class InstallCommand extends AbstractController implements CommandInterface
18
{
19
	/**
20
	 * Database driver.
21
	 *
22
	 * @var    DatabaseDriver
23
	 * @since  1.0
24
	 */
25
	private $db = null;
26
27
	/**
28
	 * Constructor.
29
	 *
30
	 * @param   DatabaseDriver  $db  Database driver.
31
	 *
32
	 * @since   1.0
33
	 */
34
	public function __construct(DatabaseDriver $db)
35
	{
36
		$this->db = $db;
37
	}
38
39
	/**
40
	 * Execute the controller.
41
	 *
42
	 * @return  boolean
43
	 *
44
	 * @since   1.0
45
	 */
46
	public function execute()
47
	{
48
		$this->getApplication()->outputTitle('Installer');
49
50
		try
51
		{
52
			// Check if the database "exists"
53
			$tables = $this->db->getTableList();
54
55
			if (!$this->getApplication()->input->getBool('reinstall', false))
56
			{
57
				$this->out()
0 ignored issues
show
Bug introduced by
The method out() does not seem to exist on object<Stats\Commands\InstallCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
					->out('<fg=black;bg=yellow>WARNING: A database has been found !!</fg=black;bg=yellow>')
59
					->out()
60
					->out('Do you want to reinstall ?')
61
					->out()
62
					->out('1) Yes')
63
					->out('2) No')
64
					->out()
65
					->out('<question>' . g11n3t('Select:') . '</question>', false);
66
67
				$in = trim($this->getApplication()->in());
68
69
				if ((int) $in !== 1)
70
				{
71
					$this->out('<info>Aborting installation.</info>');
0 ignored issues
show
Bug introduced by
The method out() does not seem to exist on object<Stats\Commands\InstallCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
73
					return true;
74
				}
75
			}
76
77
			$this->cleanDatabase($tables);
78
		}
79
		catch (\RuntimeException $e)
80
		{
81
			// Check if the message is "Could not connect to database."  Odds are, this means the DB isn't there or the server is down.
82
			if (strpos($e->getMessage(), 'Could not connect to database.') !== false)
83
			{
84
				$this->out('No database found.')
0 ignored issues
show
Bug introduced by
The method out() does not seem to exist on object<Stats\Commands\InstallCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
					->out('Creating the database...', false);
86
87
				$this->db->setQuery('CREATE DATABASE ' . $this->db->quoteName($this->getApplication()->get('database.name')))
88
					->execute();
89
90
				$this->db->select($this->getApplication()->get('database.name'));
91
92
				$this->out('<info>Database created.</info>');
0 ignored issues
show
Bug introduced by
The method out() does not seem to exist on object<Stats\Commands\InstallCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
			}
94
			else
95
			{
96
				throw $e;
97
			}
98
		}
99
100
		// Perform the installation
101
		$this->processSql()
0 ignored issues
show
Bug introduced by
The method out() does not seem to exist on object<Stats\Commands\InstallCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
			->out()
103
			->out('<fg=green;options=bold>Installation has been completed successfully.</fg=green;options=bold>');
104
	}
105
106
	/**
107
	 * Cleanup the database.
108
	 *
109
	 * @param   array  $tables  Tables to remove.
110
	 *
111
	 * @return  $this
112
	 *
113
	 * @since   1.0
114
	 */
115
	private function cleanDatabase(array $tables)
116
	{
117
		$this->out('Removing existing tables...', false);
0 ignored issues
show
Bug introduced by
The method out() does not seem to exist on object<Stats\Commands\InstallCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
119
		// Foreign key constraint fails fix
120
		$this->db->setQuery('SET FOREIGN_KEY_CHECKS=0')
121
			->execute();
122
123
		foreach ($tables as $table)
124
		{
125
			$this->db->dropTable($table, true);
126
			$this->out('.', false);
0 ignored issues
show
Bug introduced by
The method out() does not seem to exist on object<Stats\Commands\InstallCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
		}
128
129
		$this->db->setQuery('SET FOREIGN_KEY_CHECKS=1')
130
			->execute();
131
132
		$this->out('<info>Tables removed.</info>');
0 ignored issues
show
Bug introduced by
The method out() does not seem to exist on object<Stats\Commands\InstallCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
134
		return $this;
135
	}
136
137
	/**
138
	 * Process the main SQL file.
139
	 *
140
	 * @return  $this
141
	 *
142
	 * @since   1.0
143
	 * @throws  \UnexpectedValueException
144
	 */
145
	private function processSql()
146
	{
147
		// Install.
148
		$dbType = $this->getApplication()->get('database.driver');
149
150
		if ('mysqli' == $dbType)
151
		{
152
			$dbType = 'mysql';
153
		}
154
155
		$fName = APPROOT . '/etc/' . $dbType . '.sql';
156
157
		if (!file_exists($fName))
158
		{
159
			throw new \UnexpectedValueException(sprintf('Install SQL file for %s not found.', $dbType));
160
		}
161
162
		$sql = file_get_contents($fName);
163
164
		if (!$sql)
165
		{
166
			throw new \UnexpectedValueException('SQL file corrupted.');
167
		}
168
169
		$this->out(sprintf('Creating tables from file %s', realpath($fName)), false);
0 ignored issues
show
Bug introduced by
The method out() does not seem to exist on object<Stats\Commands\InstallCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
170
171
		foreach ($this->db->splitSql($sql) as $query)
172
		{
173
			$q = trim($this->db->replacePrefix($query));
174
175
			if ('' == trim($q))
176
			{
177
				continue;
178
			}
179
180
			$this->db->setQuery($q)
181
				->execute();
182
183
			$this->out('.', false);
0 ignored issues
show
Bug introduced by
The method out() does not seem to exist on object<Stats\Commands\InstallCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
184
		}
185
186
		$this->out('<info>Database tables created successfully.</info>');
0 ignored issues
show
Bug introduced by
The method out() does not seem to exist on object<Stats\Commands\InstallCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
187
188
		return $this;
189
	}
190
191
	/**
192
	 * Get the command's description
193
	 *
194
	 * @return  string
195
	 *
196
	 * @since   1.0
197
	 */
198
	public function getDescription()
199
	{
200
		return 'Installs the application.';
201
	}
202
203
	/**
204
	 * Get the command's title
205
	 *
206
	 * @return  string
207
	 *
208
	 * @since   1.0
209
	 */
210
	public function getTitle()
211
	{
212
		return 'Install Application';
213
	}
214
}
215