Skip to content

Commit

Permalink
params retrievment
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladyslav Riabchenko committed Jun 28, 2019
1 parent 5b90a07 commit 8ca5e56
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ script:
after_success:
- bash <(curl -s https://codecov.io/bash)


jobs:
include:

Expand Down
13 changes: 12 additions & 1 deletion src/Command/AnonymizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WebnetFr\DatabaseAnonymizer\Command;

use Doctrine\DBAL\DBALException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -68,7 +69,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
return;
}

$connection = $this->getConnectionFromInput($input);
try {
$connection = $this->getConnectionFromInput($input);
} catch (DBALException $e) {
$connection = null;
}

if (!$connection) {
$output->writeln(sprintf('<error>Unable to establish a connection.</error>'));

return;
}

$configFile = $input->getArgument('config');
$configFilePath = realpath($input->getArgument('config'));
Expand Down
29 changes: 17 additions & 12 deletions src/Command/AnonymizeCommandTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,33 @@
trait AnonymizeCommandTrait
{
/**
* @param array $params
* @param InputInterface $input
*
* @throws \Doctrine\DBAL\DBALException
*
* @return Connection
* @return Connection|null
*/
protected function getConnectionFromInput(InputInterface $input)
{
if ($dbURL = $input->getOption('url')) {
$params = ['url' => $dbURL];
} else {
$params = [
'driver' => $input->getOption('type'),
'host' => $input->getOption('host'),
'port' => $input->getOption('port'),
'dbname' => $input->getOption('database'),
'user' => $input->getOption('user'),
return $this->getConnection(['url' => $dbURL]);
} elseif (($type = $input->getOption('type'))
&& ($host = $input->getOption('host'))
&& ($port = $input->getOption('port'))
&& ($database = $input->getOption('database'))
&& ($user = $input->getOption('user'))
) {
return $this->getConnection([
'driver' => $type,
'host' => $host,
'port' => $port,
'dbname' => $database,
'user' => $user,
'password' => $input->getOption('password'),
];
]);
}

return $this->getConnection($params);
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testExecute()
$this->assertTrue(is_string($row['firstname']));
$this->assertTrue(is_string($row['lastname']));
$this->assertTrue(is_string($row['birthdate']));
$this->assertTrue(is_string($row['phone']));
$this->assertTrue(is_string($row['phone']) || is_null($row['phone']));
$this->assertTrue(is_string($row['password']));
}

Expand Down

0 comments on commit 8ca5e56

Please sign in to comment.