Skip to content

Commit

Permalink
Refactor addInputOptionsToConfig function in dump command
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Jun 6, 2024
1 parent eed905b commit 5080e85
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/Console/Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,20 @@ public function __construct(
*/
public function configure(): void
{
$configHint = ' (can also be specified in the configuration file)';

$this->setName('gdpr-dump')
->setDescription('Create an anonymized dump')
->addArgument(
'config_file',
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
'Dump configuration file(s)'
)
->addOption('host', null, InputOption::VALUE_REQUIRED, 'Database host')
->addOption('port', null, InputOption::VALUE_REQUIRED, 'Database port')
->addOption('user', null, InputOption::VALUE_REQUIRED, 'Database user')
->addOption('password', null, InputOption::VALUE_REQUIRED, 'Database password')
->addOption('database', null, InputOption::VALUE_REQUIRED, 'Database name');
->addOption('host', null, InputOption::VALUE_REQUIRED, 'Database host' . $configHint)
->addOption('port', null, InputOption::VALUE_REQUIRED, 'Database port' . $configHint)
->addOption('user', null, InputOption::VALUE_REQUIRED, 'Database user' . $configHint)
->addOption('password', null, InputOption::VALUE_REQUIRED, 'Database password' . $configHint)
->addOption('database', null, InputOption::VALUE_REQUIRED, 'Database name' . $configHint);
}

/**
Expand All @@ -60,7 +62,7 @@ public function configure(): void
public function execute(InputInterface $input, OutputInterface $output): int
{
try {
// Load the config
// Load the config file(s)
$config = $this->loadConfig($input);

// Validate the config data
Expand Down Expand Up @@ -133,19 +135,19 @@ private function addInputOptionsToConfig(ConfigInterface $config, InputInterface
continue;
}

if ($value === '' && $option !== 'password') {
// Option must have a value (except the "password" option)
throw new ConfigException(sprintf('Please provide a value for the option "%s".', $option));
}

$configKey = $option === 'database' ? 'name' : $option;
if ($value === '') {
// Remove the password from the config if an empty value was provided
unset($databaseConfig[$configKey]);
continue;
if ($option === 'password') {
// Remove the password from the config if an empty value was provided
unset($databaseConfig['password']);
continue;
}

// Option must have a value
throw new ConfigException(sprintf('Please provide a value for the option "%s".', $option));
}

// Override the config value with the provided option value
$configKey = $option === 'database' ? 'name' : $option;
$databaseConfig[$configKey] = $value;
}

Expand Down

0 comments on commit 5080e85

Please sign in to comment.