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

Backup::_database()   D

Complexity

Conditions 18
Paths 34

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 18.3782

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 17
cts 19
cp 0.8947
rs 4.8666
c 0
b 0
f 0
cc 18
nc 34
nop 1
crap 18.3782

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\Dater;
6
use Redaxscript\Filesystem;
7
use function exec;
8
use function implode;
9
use function is_dir;
10
use function is_string;
11
use function mkdir;
12
13
/**
14
 * children class to execute the backup command
15
 *
16
 * @since 3.0.0
17
 *
18
 * @package Redaxscript
19
 * @category Console
20
 * @author Henry Ruhs
21
 */
22
23
class Backup extends CommandAbstract
24
{
25
	/**
26
	 * array of the command
27
	 *
28
	 * @var array
29
	 */
30
31
	protected $_commandArray =
32
	[
33
		'backup' =>
34
		[
35
			'description' => 'Backup command',
36
			'argumentArray' =>
37
			[
38
				'database' =>
39
				[
40
					'description' => 'Backup the database',
41
					'optionArray' =>
42
					[
43
						'directory' =>
44
						[
45
							'description' => 'Required directory'
46
						]
47
					]
48
				]
49
			]
50
		]
51
	];
52
53
	/**
54
	 * run the command
55
	 *
56
	 * @since 3.0.0
57
	 *
58 3
	 * @param string $mode name of the mode
59
	 *
60 3
	 * @return string|null
61 3
	 */
62
63
	public function run(string $mode = null) : ?string
64
	{
65 3
		$parser = new Parser($this->_request);
66 3
		$parser->init($mode);
67
68 2
		/* run command */
69
70 1
		$argumentKey = $parser->getArgument(1);
71
		$haltOnError = (bool)$parser->getOption('halt-on-error');
72
		if ($argumentKey === 'database')
73
		{
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\Backup::_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
	 * backup the database
81
	 *
82
	 * @since 3.0.0
83 2
	 *
84
	 * @param array $optionArray
85 2
	 *
86 2
	 * @return bool
87 2
	 */
88 2
89 2
	protected function _database(array $optionArray = []) : bool
90 2
	{
91 2
		$dater = new Dater();
92 2
		$dater->init();
93 2
		$dbType = $this->_config->get('dbType');
94
		$dbHost = $this->_config->get('dbHost');
95
		$dbName = $this->_config->get('dbName');
96
		$dbUser = $this->_config->get('dbUser');
97 2
		$dbPassword = $this->_config->get('dbPassword');
98 2
		$directory = $this->prompt('directory', $optionArray);
99
		$file = $dbName ? $dbName . '_' . $dater->formatDate() . '_' . $dater->formatTime() . '.' . $dbType : $dater->formatDate() . '_' . $dater->formatTime() . '.' . $dbType;
100
101
		/* backup filesystem */
102 2
103
		$backupFilesystem = new Filesystem\Directory();
104 1
		$backupFilesystem->init($directory);
105 1
106
		/* backup */
107
108
		if (is_dir($directory) || is_string($directory) && mkdir($directory))
109 1
		{
110
			$command = null;
111
			if ($dbType === 'mysql' && $dbHost && $dbName && $dbUser && $dbPassword)
112
			{
113 1
				$command = 'mysqldump --host=' . $dbHost . ' --user=' . $dbUser . ' --password=' . $dbPassword . ' ' . $dbName;
114
			}
115 1
			if ($dbType === 'pgsql' && $dbHost && $dbName && $dbUser && $dbPassword)
116
			{
117 1
				$command =  'pg_dump --host=' . $dbHost . ' --username=' . $dbUser . ' --password='. $dbPassword . ' ' . $dbName;
118 1
			}
119 1
			if ($dbType === 'sqlite' && $dbHost)
120
			{
121 1
				$command =  'cat ' . $dbHost;
122
			}
123
			exec($command, $outputArray, $error);
124
			$content = implode($outputArray, PHP_EOL);
125
			return $error === 0 && $backupFilesystem->writeFile($file, $content);
126
		}
127
		return false;
128
	}
129
}
130