Completed
Push — Parse-Tags ( be9c93...0426dd )
by Michael
04:21
created

InstallCommand::execute()   B

Complexity

Conditions 5
Paths 17

Size

Total Lines 61
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 61
ccs 0
cts 47
cp 0
rs 8.6806
cc 5
eloc 33
nc 17
nop 0
crap 30

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * Install 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
		return true;
106
	}
107
108
	/**
109
	 * Cleanup the database.
110
	 *
111
	 * @param   array  $tables  Tables to remove.
112
	 *
113
	 * @return  $this
114
	 *
115
	 * @since   1.0
116
	 */
117
	private function cleanDatabase(array $tables)
118
	{
119
		$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...
120
121
		// Foreign key constraint fails fix
122
		$this->db->setQuery('SET FOREIGN_KEY_CHECKS=0')
123
			->execute();
124
125
		foreach ($tables as $table)
126
		{
127
			$this->db->dropTable($table, true);
128
			$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...
129
		}
130
131
		$this->db->setQuery('SET FOREIGN_KEY_CHECKS=1')
132
			->execute();
133
134
		$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...
135
136
		return $this;
137
	}
138
139
	/**
140
	 * Process the main SQL file.
141
	 *
142
	 * @return  $this
143
	 *
144
	 * @since   1.0
145
	 * @throws  \UnexpectedValueException
146
	 */
147
	private function processSql()
148
	{
149
		// Install.
150
		$dbType = $this->getApplication()->get('database.driver');
151
152
		if ('mysqli' == $dbType)
153
		{
154
			$dbType = 'mysql';
155
		}
156
157
		$fName = APPROOT . '/etc/' . $dbType . '.sql';
158
159
		if (!file_exists($fName))
160
		{
161
			throw new \UnexpectedValueException(sprintf('Install SQL file for %s not found.', $dbType));
162
		}
163
164
		$sql = file_get_contents($fName);
165
166
		if (!$sql)
167
		{
168
			throw new \UnexpectedValueException('SQL file corrupted.');
169
		}
170
171
		$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...
172
173
		foreach ($this->db->splitSql($sql) as $query)
174
		{
175
			$q = trim($this->db->replacePrefix($query));
176
177
			if ('' == trim($q))
178
			{
179
				continue;
180
			}
181
182
			$this->db->setQuery($q)
183
				->execute();
184
185
			$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...
186
		}
187
188
		$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...
189
190
		return $this;
191
	}
192
193
	/**
194
	 * Get the command's description
195
	 *
196
	 * @return  string
197
	 *
198
	 * @since   1.0
199
	 */
200
	public function getDescription()
201
	{
202
		return 'Installs the application.';
203
	}
204
205
	/**
206
	 * Get the command's title
207
	 *
208
	 * @return  string
209
	 *
210
	 * @since   1.0
211
	 */
212
	public function getTitle()
213
	{
214
		return 'Install Application';
215
	}
216
}
217