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

includes/Console/Command/Restore.php (1 issue)

Labels
Severity

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\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
	 */
62
63 2
	public function run(string $mode = null) : ?string
64
	{
65 2
		$parser = new Parser($this->_request);
66 2
		$parser->init($mode);
67
68
		/* run command */
69
70 2
		$argumentKey = $parser->getArgument(1);
71 2
		$haltOnError = (bool)$parser->getOption('halt-on-error');
72 2
		if ($argumentKey === 'database')
73
		{
74 1
			return $this->_database($parser->getOption()) ? $this->success() : $this->error($haltOnError);
0 ignored issues
show
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 1
		return $this->getHelp();
77
	}
78
79
	/**
80
	 * restore the database
81
	 *
82
	 * @since 3.0.0
83
	 *
84
	 * @param array $optionArray
85
	 *
86
	 * @return bool
87
	 */
88
89 1
	protected function _database(array $optionArray = []) : bool
90
	{
91 1
		$dbType = $this->_config->get('dbType');
92 1
		$dbHost = $this->_config->get('dbHost');
93 1
		$dbName = $this->_config->get('dbName');
94 1
		$dbUser = $this->_config->get('dbUser');
95 1
		$dbPassword = $this->_config->get('dbPassword');
96 1
		$directory = $this->prompt('directory', $optionArray);
97 1
		$file = $this->prompt('file', $optionArray);
98
99
		/* restore filesystem */
100
101 1
		$backupFilesystem = new Filesystem\Directory();
102 1
		$backupFilesystem->init($directory);
103
104
		/* restore */
105
106 1
		if (is_file($directory . DIRECTORY_SEPARATOR . $file))
107
		{
108
			$command = ':';
109
			$content = $backupFilesystem->readFile($file);
110
			if ($dbType === 'mysql' && $dbHost && $dbName && $dbUser && $dbPassword)
111
			{
112
				$command = $content . ' | mysql --host=' . $dbHost . ' --user=' . $dbUser . ' --password=' . $dbPassword . ' ' . $dbName;
113
			}
114
			if ($dbType === 'pgsql' && $dbHost && $dbName && $dbUser && $dbPassword)
115
			{
116
				$command = 'PGPASSWORD=' . $dbPassword . ' ' . $content . ' | psql --host=' . $dbHost . ' --username=' . $dbUser . ' ' . $dbName;
117
			}
118
			if ($dbType === 'sqlite' && is_file($dbHost))
119
			{
120
				$command = 'echo ' . $content . ' > ' . $dbHost;
121
			}
122
			exec($command, $outputArray, $error);
123
			return $error === 0;
124
		}
125 1
		return false;
126
	}
127
}
128