Passed
Push — master ( df3b95...265c0b )
by Webnet
03:01
created

AnonymizeCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WebnetFr\DatabaseAnonymizer\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\ConfirmationQuestion;
11
use WebnetFr\DatabaseAnonymizer\Anonymizer;
12
use WebnetFr\DatabaseAnonymizer\Config\TargetFactory;
13
use WebnetFr\DatabaseAnonymizer\GeneratorFactory\GeneratorFactoryInterface;
14
15
/**
16
 * @author Vlad Riabchenko <[email protected]>
17
 */
18
class AnonymizeCommand extends Command
19
{
20
    use AnonymizeCommandTrait;
21
22
    /**
23
     * @var GeneratorFactoryInterface
24
     */
25
    private $generatorFactory;
26
27
    /**
28
     * @param GeneratorFactoryInterface $generatorFactory
29
     */
30
    public function __construct(GeneratorFactoryInterface $generatorFactory)
31
    {
32
        parent::__construct();
33
34
        $this->generatorFactory = $generatorFactory;
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    protected function configure()
41
    {
42
        parent::configure();
43
44
        $this->setName('webnet-fr:anonymizer:anonymize')
45
            ->setDescription('Anoymize database.')
46
            ->setHelp('Anoymize database according to GDPR (General Data Protection Regulation).')
47
            ->addArgument('config', InputArgument::REQUIRED, 'Configuration file.')
48
            ->addOption('url', 'U', InputOption::VALUE_REQUIRED, 'Database connection string.')
49
            ->addOption('type', 't', InputOption::VALUE_REQUIRED, 'Database type.')
50
            ->addOption('host', 'H', InputOption::VALUE_REQUIRED, 'Database connection string.')
51
            ->addOption('port', 'P', InputOption::VALUE_REQUIRED, 'Database connection string.')
52
            ->addOption('database', 'd', InputOption::VALUE_REQUIRED, 'Database connection string.')
53
            ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'Database connection string.')
54
            ->addOption('password', 'p', InputOption::VALUE_REQUIRED, 'Database connection string.')
55
        ;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        $questionHelper = $this->getHelper('question');
64
        $question = new ConfirmationQuestion('Are you sure you want to anonymize your database?', false);
65
66
        if (!$input->getOption('no-interaction') && !$questionHelper->ask($input, $output, $question)) {
67
            return;
68
        }
69
70
        if ($dbURL = $input->getOption('url')) {
71
            $params = ['url' => $dbURL];
72
        } else {
73
            $params = [
74
                'driver' => $input->getOption('type'),
75
                'host' => $input->getOption('host'),
76
                'port' => $input->getOption('port'),
77
                'dbname' => $input->getOption('database'),
78
                'user' => $input->getOption('user'),
79
                'password' => $input->getOption('password'),
80
            ];
81
        }
82
83
        $connection = $this->getConnection($params);
84
85
        $configFile = $input->getArgument('config');
86
        $configFilePath = realpath($input->getArgument('config'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('config') can also be of type string[]; however, parameter $path of realpath() 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 ignore-type  annotation

86
        $configFilePath = realpath(/** @scrutinizer ignore-type */ $input->getArgument('config'));
Loading history...
87
        if (!is_file($configFilePath)) {
88
            $output->writeln(sprintf('<error>Configuration file "%s" does not exist.</error>', $configFile));
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

88
            $output->writeln(sprintf('<error>Configuration file "%s" does not exist.</error>', /** @scrutinizer ignore-type */ $configFile));
Loading history...
89
90
            return;
91
        }
92
93
        $config = $this->getConfigFromFile($configFilePath);
94
95
        $targetFactory = (new TargetFactory($this->generatorFactory))
96
            ->setConnection($connection);
97
        $targetTables = $targetFactory->createTargets($config);
98
99
        $anonymizer = new Anonymizer();
100
        $anonymizer->anonymize($connection, $targetTables);
101
    }
102
}
103