Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
created

includes/Console/Command/Backup.php (8 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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_file;
11
use function is_string;
12
use function mkdir;
13
14
/**
15
 * children class to execute the backup command
16
 *
17
 * @since 3.0.0
18
 *
19
 * @package Redaxscript
20
 * @category Console
21
 * @author Henry Ruhs
22
 */
23
24
class Backup extends CommandAbstract
25
{
26
	/**
27
	 * array of the command
28
	 *
29
	 * @var array
30
	 */
31
32
	protected $_commandArray =
33
	[
34
		'backup' =>
35
		[
36
			'description' => 'Backup command',
37
			'argumentArray' =>
38
			[
39
				'database' =>
40
				[
41
					'description' => 'Backup the database',
42
					'optionArray' =>
43
					[
44
						'directory' =>
45
						[
46
							'description' => 'Required directory'
47
						]
48
					]
49
				]
50
			]
51
		]
52
	];
53
54
	/**
55
	 * run the command
56
	 *
57
	 * @since 3.0.0
58
	 *
59
	 * @param string $mode name of the mode
60
	 *
61
	 * @return string|null
62
	 */
63
64 2
	public function run(string $mode = null) : ?string
65
	{
66 2
		$parser = new Parser($this->_request);
67 2
		$parser->init($mode);
68
69
		/* run command */
70
71 2
		$argumentKey = $parser->getArgument(1);
72 2
		$haltOnError = (bool)$parser->getOption('halt-on-error');
73 2
		if ($argumentKey === 'database')
74
		{
75 1
			return $this->_database($parser->getOptionArray()) ? $this->success() : $this->error($haltOnError);
76
		}
77 1
		return $this->getHelp();
78
	}
79
80
	/**
81
	 * backup the database
82
	 *
83
	 * @since 3.0.0
84
	 *
85
	 * @param array $optionArray
86
	 *
87
	 * @return bool
88
	 */
89
90 1
	protected function _database(array $optionArray = []) : bool
91
	{
92 1
		$dater = new Dater();
93 1
		$dater->init();
94 1
		$dbType = $this->_config->get('dbType');
95 1
		$dbHost = $this->_config->get('dbHost');
96 1
		$dbName = $this->_config->get('dbName');
97 1
		$dbUser = $this->_config->get('dbUser');
98 1
		$dbPassword = $this->_config->get('dbPassword');
99 1
		$directory = $this->prompt('directory', $optionArray);
100 1
		$file = $dbName ? $dbName . '_' . $dater->formatDate() . '_' . $dater->formatTime() . '.' . $dbType : $dater->formatDate() . '_' . $dater->formatTime() . '.' . $dbType;
101
102
		/* backup filesystem */
103
104 1
		$backupFilesystem = new Filesystem\Directory();
105 1
		$backupFilesystem->init($directory);
106
107
		/* backup */
108
109 1
		if (is_dir($directory) || is_string($directory) && mkdir($directory))
110
		{
111
			$command = ':';
112
			if ($dbType === 'mysql' && $dbHost && $dbName && $dbUser && $dbPassword)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $dbHost of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by redaxmedia
The expression $dbName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by redaxmedia
The expression $dbUser of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by redaxmedia
The expression $dbPassword of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
113
			{
114
				$command = 'mysqldump --host=' . $dbHost . ' --user=' . $dbUser . ' --password=' . $dbPassword . ' ' . $dbName;
115
			}
116
			if ($dbType === 'pgsql' && $dbHost && $dbName && $dbUser && $dbPassword)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $dbHost of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by redaxmedia
The expression $dbName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by redaxmedia
The expression $dbUser of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by redaxmedia
The expression $dbPassword of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
117
			{
118
				$command = 'PGPASSWORD=' . $dbPassword . ' pg_dump --host=' . $dbHost . ' --username=' . $dbUser . ' ' . $dbName;
119
			}
120
			if ($dbType === 'sqlite' && is_file($dbHost))
121
			{
122
				$command = 'cat ' . $dbHost;
123
			}
124
			exec($command, $outputArray, $error);
125
			$content = implode($outputArray, PHP_EOL);
126
			return $error === 0 && $backupFilesystem->writeFile($file, $content);
127
		}
128 1
		return false;
129
	}
130
}
131