webnet-fr /
database-anonymizer
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace WebnetFr\DatabaseAnonymizer\Command; |
||||
| 4 | |||||
| 5 | use Doctrine\DBAL\DBALException; |
||||
| 6 | use Symfony\Component\Console\Command\Command; |
||||
| 7 | use Symfony\Component\Console\Input\InputArgument; |
||||
| 8 | use Symfony\Component\Console\Input\InputInterface; |
||||
| 9 | use Symfony\Component\Console\Input\InputOption; |
||||
| 10 | use Symfony\Component\Console\Output\OutputInterface; |
||||
| 11 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
||||
| 12 | use WebnetFr\DatabaseAnonymizer\Anonymizer; |
||||
| 13 | use WebnetFr\DatabaseAnonymizer\Config\TargetFactory; |
||||
| 14 | use WebnetFr\DatabaseAnonymizer\GeneratorFactory\GeneratorFactoryInterface; |
||||
| 15 | |||||
| 16 | /** |
||||
| 17 | * @author Vlad Riabchenko <[email protected]> |
||||
| 18 | */ |
||||
| 19 | class AnonymizeCommand extends Command |
||||
| 20 | { |
||||
| 21 | use AnonymizeCommandTrait; |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * @var GeneratorFactoryInterface |
||||
| 25 | */ |
||||
| 26 | private $generatorFactory; |
||||
| 27 | |||||
| 28 | /** |
||||
| 29 | * @param GeneratorFactoryInterface $generatorFactory |
||||
| 30 | */ |
||||
| 31 | public function __construct(GeneratorFactoryInterface $generatorFactory) |
||||
| 32 | { |
||||
| 33 | parent::__construct(); |
||||
| 34 | |||||
| 35 | $this->generatorFactory = $generatorFactory; |
||||
| 36 | } |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * {@inheritdoc} |
||||
| 40 | */ |
||||
| 41 | protected function configure() |
||||
| 42 | { |
||||
| 43 | parent::configure(); |
||||
| 44 | |||||
| 45 | $this |
||||
| 46 | ->setName('webnet-fr:anonymizer:anonymize') |
||||
| 47 | ->setDescription('Anoymize database.') |
||||
| 48 | ->setHelp('Anoymize database according to GDPR (General Data Protection Regulation).') |
||||
| 49 | ->addArgument('config', InputArgument::REQUIRED, 'Configuration file.') |
||||
| 50 | ->addOption('url', 'U', InputOption::VALUE_REQUIRED, 'Database connection string.') |
||||
| 51 | ->addOption('type', 't', InputOption::VALUE_REQUIRED, 'Database type.') |
||||
| 52 | ->addOption('host', 'H', InputOption::VALUE_REQUIRED, 'Database host.') |
||||
| 53 | ->addOption('port', 'P', InputOption::VALUE_REQUIRED, 'Database port.') |
||||
| 54 | ->addOption('database', 'd', InputOption::VALUE_REQUIRED, 'Database name.') |
||||
| 55 | ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'User.') |
||||
| 56 | ->addOption('password', 'p', InputOption::VALUE_REQUIRED, 'Password.') |
||||
| 57 | ; |
||||
| 58 | } |
||||
| 59 | |||||
| 60 | /** |
||||
| 61 | * {@inheritdoc} |
||||
| 62 | */ |
||||
| 63 | protected function execute(InputInterface $input, OutputInterface $output) |
||||
| 64 | { |
||||
| 65 | $questionHelper = $this->getHelper('question'); |
||||
| 66 | $question = new ConfirmationQuestion('Are you sure you want to anonymize your database?', false); |
||||
| 67 | |||||
| 68 | if (!$input->getOption('no-interaction') && !$questionHelper->ask($input, $output, $question)) { |
||||
| 69 | return 1; |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | try { |
||||
| 73 | $connection = $this->getConnectionFromInput($input); |
||||
| 74 | } catch (DBALException $e) { |
||||
| 75 | $connection = null; |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | if (!$connection) { |
||||
| 79 | $output->writeln(sprintf('<error>Unable to establish a connection.</error>')); |
||||
| 80 | |||||
| 81 | return 1; |
||||
| 82 | } |
||||
| 83 | |||||
| 84 | $configFile = $input->getArgument('config'); |
||||
| 85 | $configFilePath = realpath($input->getArgument('config')); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 86 | if (!is_file($configFilePath)) { |
||||
| 87 | $output->writeln(sprintf('<error>Configuration file "%s" does not exist.</error>', $configFile)); |
||||
|
0 ignored issues
–
show
It seems like
$configFile can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 88 | |||||
| 89 | return 1; |
||||
| 90 | } |
||||
| 91 | |||||
| 92 | $config = $this->getConfigFromFile($configFilePath); |
||||
| 93 | |||||
| 94 | $targetFactory = new TargetFactory($this->generatorFactory); |
||||
| 95 | $targetFactory->setConnection($connection); |
||||
| 96 | $targetTables = $targetFactory->createTargets($config); |
||||
| 97 | |||||
| 98 | $anonymizer = new Anonymizer(); |
||||
| 99 | $anonymizer->anonymize($connection, $targetTables); |
||||
| 100 | |||||
| 101 | return 0; |
||||
| 102 | } |
||||
| 103 | } |
||||
| 104 |