Passed
Push — master ( 3ccc44...cef086 )
by Emmanuel
02:16 queued 15s
created

ConfigGenerateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 58
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 56
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 56
cts 56
cp 1
rs 9.639
c 0
b 0
f 0
cc 1
eloc 55
nc 1
nop 0
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Console\Question\Question;
25
26
/**
27
 * Command to generate a config file from a DB
28
 */
29
class ConfigGenerateCommand extends Command
30
{
31
    /**
32
     * Set the command shortcut to be used in configuration
33
     *
34
     * @var string
35
     */
36
    protected $command = 'config:generate';
37
38
39
    /**
40
     * Configure the command
41
     *
42
     * @return void
43
     */
44 27
    protected function configure(): void
45
    {
46
        // First command : Test the DB Connexion
47 27
        $this->setName($this->command)
48 27
            ->setDescription(
49 27
                'Generate configuration for the Anonymizer'
50 27
            )->setHelp(
51 27
                'This command will connect to a DB and extract a list of tables / fields to a yaml file' . PHP_EOL .
52 27
                "Usage: neuralyzer <info>{$this->command} -u app -p app -f neuralyzer.yml</info>"
53 27
            )->addOption(
54 27
                'driver',
55 27
                'D',
56 27
                InputOption::VALUE_REQUIRED,
57 27
                'Driver (check Doctrine documentation to have the list)',
58 27
                'pdo_mysql'
59 27
            )->addOption(
60 27
                'host',
61 27
                'H',
62 27
                InputOption::VALUE_REQUIRED,
63 27
                'Host',
64 27
                '127.0.0.1'
65 27
            )->addOption(
66 27
                'db',
67 27
                'd',
68 27
                InputOption::VALUE_REQUIRED,
69 27
                'Database Name'
70 27
            )->addOption(
71 27
                'user',
72 27
                'u',
73 27
                InputOption::VALUE_REQUIRED,
74 27
                'User Name',
75 27
                get_current_user()
76 27
            )->addOption(
77 27
                'password',
78 27
                'p',
79 27
                InputOption::VALUE_REQUIRED,
80 27
                "Password (or it'll be prompted)"
81 27
            )->addOption(
82 27
                'file',
83 27
                'f',
84 27
                InputOption::VALUE_REQUIRED,
85 27
                'File',
86 27
                'neuralyzer.yml'
87 27
            )->addOption(
88 27
                'protect',
89 27
                null,
90 27
                InputOption::VALUE_NONE,
91 27
                'Protect IDs and other fields'
92 27
            )->addOption(
93 27
                'ignore-table',
94 27
                null,
95 27
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
96 27
                'Table to ignore. Can be repeated'
97 27
            )->addOption(
98 27
                'ignore-field',
99 27
                null,
100 27
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
101 27
                'Field to ignore. Regexp in the form "table.field". Can be repeated'
102
            );
103 27
    }
104
105
    /**
106
     * Execute the command
107
     *
108
     * @param InputInterface  $input
109
     * @param OutputInterface $output
110
     *
111
     * @return void
112
     */
113 6
    protected function execute(InputInterface $input, OutputInterface $output): void
114
    {
115
        // Throw an exception immediately if we dont have the required DB parameter
116 6
        if (empty($input->getOption('db'))) {
117 1
            throw new \InvalidArgumentException('Database name is required (--db)');
118
        }
119
120 5
        $password = $input->getOption('password');
121 5
        if (is_null($password)) {
122 4
            $question = new Question('Password: ');
123 4
            $question->setHidden(true)->setHiddenFallback(false);
124
125 4
            $password = $this->getHelper('question')->ask($input, $output, $question);
126
        }
127
128 5
        $ignoreFields = $input->getOption('ignore-field');
129
130
        // Now work on the DB
131 5
        $db = new \Edyan\Neuralyzer\Anonymizer\DB([
132 5
            'driver' => $input->getOption('driver'),
133 5
            'host' => $input->getOption('host'),
134 5
            'dbname' => $input->getOption('db'),
135 5
            'user' => $input->getOption('user'),
136 5
            'password' => $password,
137
        ]);
138
139 5
        $writer = new \Edyan\Neuralyzer\Configuration\Writer;
140 5
        $writer->protectCols($input->getOption('protect'));
141
142
        // Override the protection if fields are defined
143 5
        if (!empty($ignoreFields)) {
144 1
            $writer->protectCols(true);
145 1
            $writer->setProtectedCols($ignoreFields);
146
        }
147
148 5
        $writer->setIgnoredTables($input->getOption('ignore-table'));
149 5
        $data = $writer->generateConfFromDB($db, new \Edyan\Neuralyzer\Guesser);
150 3
        $writer->save($data, $input->getOption('file'));
151
152 3
        $output->writeln('<comment>Configuration written to ' . $input->getOption('file') . '</comment>');
153 3
    }
154
}
155