Completed
Push — master ( 7602e7...af2cc9 )
by Henry
09:36
created

Restore::_database()   C

Complexity

Conditions 14
Paths 9

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 14.196

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 18
cts 20
cp 0.9
rs 6.2666
c 0
b 0
f 0
cc 14
nc 9
nop 1
crap 14.196

How to fix   Complexity   

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
namespace Redaxscript\Console\Command;
3
4
use Redaxscript\Console\Parser;
5
use Redaxscript\Filesystem;
6
use function exec;
7
use function is_file;
8
9
/**
10
 * children class to execute the restore command
11
 *
12
 * @since 3.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Console
16
 * @author Henry Ruhs
17
 */
18
19
class Restore extends CommandAbstract
20
{
21
	/**
22
	 * array of the command
23
	 *
24
	 * @var array
25
	 */
26
27
	protected $_commandArray =
28
	[
29
		'restore' =>
30
		[
31
			'description' => 'Restore command',
32
			'argumentArray' =>
33
			[
34
				'database' =>
35
				[
36
					'description' => 'Restore the database',
37
					'optionArray' =>
38
					[
39
						'directory' =>
40
						[
41
							'description' => 'Required directory'
42
						],
43
						'file' =>
44
						[
45
							'description' => 'Required file'
46
						]
47
					]
48
				]
49
			]
50
		]
51
	];
52
53
	/**
54
	 * run the command
55
	 *
56
	 * @since 3.0.0
57
	 *
58
	 * @param string $mode name of the mode
59
	 *
60
	 * @return string|null
61 3
	 */
62
63 3
	public function run(string $mode = null) : ?string
64 3
	{
65
		$parser = new Parser($this->_request);
66
		$parser->init($mode);
67
68 3
		/* run command */
69 3
70
		$argumentKey = $parser->getArgument(1);
71 2
		$haltOnError = (bool)$parser->getOption('halt-on-error');
72
		if ($argumentKey === 'database')
73 1
		{
74
			return $this->_database($parser->getOption()) ? $this->success() : $this->error($haltOnError);
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $parser->getOption() targeting Redaxscript\Console\Parser::getOption() can also be of type null or string; however, Redaxscript\Console\Command\Restore::_database() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
75
		}
76
		return $this->getHelp();
77
	}
78
79
	/**
80
	 * restore the database
81
	 *
82
	 * @since 3.0.0
83
	 *
84
	 * @param array $optionArray
85
	 *
86 2
	 * @return bool
87
	 */
88 2
89 2
	protected function _database(array $optionArray = []) : bool
90 2
	{
91 2
		$dbType = $this->_config->get('dbType');
92 2
		$dbHost = $this->_config->get('dbHost');
93 2
		$dbName = $this->_config->get('dbName');
94 2
		$dbUser = $this->_config->get('dbUser');
95
		$dbPassword = $this->_config->get('dbPassword');
96
		$directory = $this->prompt('directory', $optionArray);
97
		$file = $this->prompt('file', $optionArray);
98 2
99 2
		/* restore filesystem */
100
101
		$backupFilesystem = new Filesystem\Directory();
102
		$backupFilesystem->init($directory);
103 2
104
		/* restore */
105 1
106 1
		if (is_file($directory . DIRECTORY_SEPARATOR . $file))
107 1
		{
108
			$command = null;
109
			$content = $backupFilesystem->readFile($file);
110
			if ($dbType === 'mysql' && $dbHost && $dbName && $dbUser && $dbPassword)
111 1
			{
112
				$command = $content . ' | mysql --host=' . $dbHost . ' --username=' . $dbUser . ' --password=' . $dbPassword . ' ' . $dbName;
113
			}
114
			if ($dbType === 'pgsql' && $dbHost && $dbName && $dbUser && $dbPassword)
115 1
			{
116
				$command = $content . ' | psql --host=' . $dbHost . ' --username=' . $dbUser . ' --password=' . $dbPassword . ' ' . $dbName;
117 1
			}
118
			if ($dbType === 'sqlite' && $dbHost)
119 1
			{
120 1
				$command =  'echo ' . $content . ' > ' . $dbHost;
121
			}
122 1
			exec($command, $outputArray, $error);
123
			return $error === 0;
124
		}
125
		return false;
126
	}
127
}
128