Completed
Push — master ( a52438...bf8826 )
by Henry
06:30
created

Backup::_database()   D

Complexity

Conditions 18
Paths 34

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 18.2433

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 20
cts 22
cp 0.9091
rs 4.8666
c 0
b 0
f 0
cc 18
nc 34
nop 1
crap 18.2433

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 function exec;
7
use function is_dir;
8
use function is_string;
9
use function mkdir;
10
11
/**
12
 * children class to execute the backup command
13
 *
14
 * @since 3.0.0
15
 *
16
 * @package Redaxscript
17
 * @category Console
18
 * @author Henry Ruhs
19
 */
20
21
class Backup extends CommandAbstract
22
{
23
	/**
24
	 * array of the command
25
	 *
26
	 * @var array
27
	 */
28
29
	protected array $_commandArray =
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
30
	[
31
		'backup' =>
32
		[
33
			'description' => 'Backup command',
34
			'argumentArray' =>
35
			[
36
				'database' =>
37
				[
38
					'description' => 'Backup the database',
39
					'optionArray' =>
40
					[
41
						'directory' =>
42
						[
43
							'description' => 'Required directory'
44
						]
45
					]
46
				]
47
			]
48
		]
49
	];
50
51
	/**
52
	 * run the command
53
	 *
54
	 * @since 3.0.0
55
	 *
56
	 * @param string $mode name of the mode
57
	 *
58
	 * @return string|null
59
	 */
60
61 3
	public function run(string $mode = null) : ?string
62
	{
63 3
		$parser = new Parser($this->_request);
64 3
		$parser->init($mode);
65
66
		/* run command */
67
68 3
		$argumentKey = $parser->getArgument(1);
69 3
		$haltOnError = (bool)$parser->getOption('halt-on-error');
70 3
		if ($argumentKey === 'database')
71
		{
72 2
			return $this->_database($parser->getOptionArray()) ? $this->success() : $this->error($haltOnError);
73
		}
74 1
		return $this->getHelp();
75
	}
76
77
	/**
78
	 * backup the database
79
	 *
80
	 * @since 3.0.0
81
	 *
82
	 * @param array $optionArray
83
	 *
84
	 * @return bool
85
	 */
86
87 2
	protected function _database(array $optionArray = []) : bool
88
	{
89 2
		$dater = new Dater();
90 2
		$dater->init();
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 2
		$dbPassword = $this->_config->get('dbPassword');
96 2
		$directory = $this->prompt('directory', $optionArray);
97 2
		$file = $dbName ? $dbName . '_' . $dater->formatDate() . '_' . $dater->formatTime() . '.' . $dbType : $dater->formatDate() . '_' . $dater->formatTime() . '.' . $dbType;
98 2
		$path = $directory . DIRECTORY_SEPARATOR . $file;
99
100
		/* backup */
101
102 2
		if (is_dir($directory) || is_string($directory) && mkdir($directory))
103
		{
104 1
			if ($dbType === 'mysql' && $dbHost && $dbName && $dbUser && $dbPassword)
105
			{
106
				$command = 'mysqldump --host=' . $dbHost . ' --user=' . $dbUser . ' --password=' . $dbPassword . ' ' . $dbName . ' > ' . $path;
107
			}
108 1
			if ($dbType === 'pgsql' && $dbHost && $dbName && $dbUser && $dbPassword)
109
			{
110
				$command = 'pg_dump postgres://' . $dbUser . ':' . $dbPassword . '@' . $dbHost . '/' . $dbName . ' > ' . $path;
111
			}
112 1
			if ($dbType === 'sqlite' && $dbHost)
113
			{
114 1
				$command = 'sqlite3 ' . $dbHost . ' .dump > ' . $path;
115
			}
116 1
			if ($command)
117
			{
118 1
				exec($command, $outputArray, $status);
119
			}
120 1
			return $status === 0;
121
		}
122 1
		return false;
123
	}
124
}
125