Completed
Push — master ( a1f222...7ceccc )
by Emmanuel
03:50
created

ConfigGenerateCommand::execute()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 40
rs 9.552
c 0
b 0
f 0
ccs 16
cts 16
cp 1
cc 4
nc 5
nop 2
crap 4
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.1
6
 *
7
 * @author    Emmanuel Dyan
8
 * @author    Rémi Sauvat
9
 * @copyright 2018 Emmanuel Dyan
10
 *
11
 * @package   edyan/neuralyzer
12
 *
13
 * @license   GNU General Public License v2.0
14
 *
15
 * @link      https://github.com/edyan/neuralyzer
16
 */
17
18
namespace Edyan\Neuralyzer\Console\Commands;
19
20
use Edyan\Neuralyzer\Utils\DBUtils;
21
use Symfony\Component\Console\Command\Command;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Console\Question\Question;
26
27
/**
28
 * Command to generate a config file from a DB
29
 */
30
class ConfigGenerateCommand extends Command
31
{
32
    /**
33
     * Set the command shortcut to be used in configuration
34
     *
35
     * @var string
36
     */
37
    protected $command = 'config:generate';
38
39
    /**
40
     * Store the DBUtils Object (autowiring)
41
     *
42
     * @var DBUtils
43
     */
44 28
    private $dbUtils;
45
46
    /**
47 28
     * RunCommand constructor.
48 28
     *
49 28
     * @param DBUtils $dbUtils
50 28
     */
51 28
    public function __construct(DBUtils $dbUtils)
52 28
    {
53 28
        parent::__construct();
54 28
55 28
        $this->dbUtils = $dbUtils;
56 28
    }
57 28
58 28
    /**
59 28
     * Configure the command
60 28
     *
61 28
     * @return void
62 28
     */
63 28
    protected function configure(): void
64 28
    {
65 28
        // First command : Test the DB Connexion
66 28
        $this->setName($this->command)
67 28
            ->setDescription(
68 28
                'Generate configuration for the Anonymizer'
69 28
            )->setHelp(
70 28
                'This command will connect to a DB and extract a list of tables / fields to a yaml file'.PHP_EOL.
71 28
                "Usage: neuralyzer <info>{$this->command} -u app -p app -f neuralyzer.yml</info>"
72 28
            )->addOption(
73 28
                'driver',
74 28
                'D',
75 28
                InputOption::VALUE_REQUIRED,
76 28
                'Driver (check Doctrine documentation to have the list)',
77 28
                'pdo_mysql'
78 28
            )->addOption(
79 28
                'host',
80 28
                'H',
81 28
                InputOption::VALUE_REQUIRED,
82 28
                'Host',
83 28
                '127.0.0.1'
84 28
            )->addOption(
85 28
                'db',
86 28
                'd',
87 28
                InputOption::VALUE_REQUIRED,
88 28
                'Database Name'
89 28
            )->addOption(
90 28
                'user',
91 28
                'u',
92 28
                InputOption::VALUE_REQUIRED,
93 28
                'User Name',
94 28
                get_current_user()
95 28
            )->addOption(
96 28
                'password',
97 28
                'p',
98 28
                InputOption::VALUE_REQUIRED,
99 28
                "Password (or it'll be prompted)"
100 28
            )->addOption(
101 28
                'file',
102
                'f',
103 28
                InputOption::VALUE_REQUIRED,
104
                'File',
105
                'neuralyzer.yml'
106
            )->addOption(
107
                'protect',
108
                null,
109
                InputOption::VALUE_NONE,
110
                'Protect IDs and other fields'
111
            )->addOption(
112
                'ignore-table',
113 6
                null,
114
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
115
                'Table to ignore. Can be repeated'
116 6
            )->addOption(
117 1
                'ignore-field',
118
                null,
119
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
120 5
                'Field to ignore. Regexp in the form "table.field". Can be repeated'
121 5
            );
122 4
    }
123 4
124
    /**
125 4
     * @param InputInterface  $input
126
     * @param OutputInterface $output
127
     *
128 5
     * @throws \Doctrine\DBAL\DBALException
129
     * @throws \Edyan\Neuralyzer\Exception\NeuralizerConfigurationException
130
     */
131 5
    protected function execute(InputInterface $input, OutputInterface $output): void
132 5
    {
133 5
        // Throw an exception immediately if we dont have the required DB parameter
134 5
        if (empty($input->getOption('db'))) {
135 5
            throw new \InvalidArgumentException('Database name is required (--db)');
136 5
        }
137
138
        $password = $input->getOption('password');
139 5
        if (null === $password){
140 5
            $question = new Question('Password: ');
141
            $question->setHidden(true)->setHiddenFallback(false);
142
143 5
            $password = $this->getHelper('question')->ask($input, $output, $question);
144 1
        }
145 1
146
        $ignoreFields = $input->getOption('ignore-field');
147
148 5
149 5
        $this->dbUtils->configure([
150 3
            'driver' => $input->getOption('driver'),
151
            'host' => $input->getOption('host'),
152 3
            'dbname' => $input->getOption('db'),
153 3
            'user' => $input->getOption('user'),
154
            'password' => $password,
155
        ]);
156
157
        $writer = new \Edyan\Neuralyzer\Configuration\Writer;
158
        $writer->protectCols($input->getOption('protect'));
159
160
        // Override the protection if fields are defined
161
        if (!empty($ignoreFields)) {
162
            $writer->protectCols(true);
163
            $writer->setProtectedCols($ignoreFields);
0 ignored issues
show
Bug introduced by
It seems like $ignoreFields can also be of type boolean and string; however, parameter $protectedCols of Edyan\Neuralyzer\Configu...ter::setProtectedCols() does only seem to accept array, 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

163
            $writer->setProtectedCols(/** @scrutinizer ignore-type */ $ignoreFields);
Loading history...
164
        }
165
166
        $writer->setIgnoredTables($input->getOption('ignore-table'));
0 ignored issues
show
Bug introduced by
It seems like $input->getOption('ignore-table') can also be of type boolean and null and string; however, parameter $ignoredTables of Edyan\Neuralyzer\Configu...ter::setIgnoredTables() does only seem to accept array, 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

166
        $writer->setIgnoredTables(/** @scrutinizer ignore-type */ $input->getOption('ignore-table'));
Loading history...
167
        $data = $writer->generateConfFromDB($this->dbUtils, new \Edyan\Neuralyzer\Guesser);
168
        $writer->save($data, $input->getOption('file'));
169
170
        $output->writeln('<comment>Configuration written to '.$input->getOption('file').'</comment>');
171
    }
172
}
173