1 | <?php |
||
32 | class AnonRunCommand extends Command |
||
33 | { |
||
34 | use DBTrait; |
||
35 | |||
36 | /** |
||
37 | * Anonymizer DB Interface |
||
38 | * @var Inet\Neuralyzer\Anonymizer\DB |
||
39 | */ |
||
40 | private $anon; |
||
41 | |||
42 | /** |
||
43 | * Set the command shortcut to be used in configuration |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | private $command = 'run'; |
||
48 | |||
49 | /** |
||
50 | * @var InputInterface |
||
51 | */ |
||
52 | private $input; |
||
53 | |||
54 | /** |
||
55 | * @var OutputInterface |
||
56 | */ |
||
57 | private $output; |
||
58 | |||
59 | |||
60 | /** |
||
61 | * Configure the command |
||
62 | * |
||
63 | * @return void |
||
64 | */ |
||
65 | 13 | protected function configure() |
|
114 | |||
115 | /** |
||
116 | * Execute the command |
||
117 | * |
||
118 | * @param InputInterface $input |
||
119 | * @param OutputInterface $output |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | 7 | protected function execute(InputInterface $input, OutputInterface $output) |
|
124 | { |
||
125 | 7 | $password = $input->getOption('password'); |
|
126 | 7 | if (is_null($password)) { |
|
127 | 2 | $question = new Question('Password: '); |
|
128 | 2 | $question->setHidden(true)->setHiddenFallback(false); |
|
129 | |||
130 | 2 | $password = $this->getHelper('question')->ask($input, $output, $question); |
|
|
|||
131 | } |
||
132 | |||
133 | 7 | $this->connectToDB( |
|
134 | 7 | $input->getOption('host'), |
|
135 | 7 | $input->getOption('db'), |
|
136 | 7 | $input->getOption('user'), |
|
137 | $password |
||
138 | ); |
||
139 | |||
140 | 5 | $this->input = $input; |
|
141 | 5 | $this->output = $output; |
|
142 | |||
143 | // Anon READER |
||
144 | 5 | $reader = new \Inet\Neuralyzer\Configuration\Reader($input->getOption('config')); |
|
145 | |||
146 | // Now work on the DB |
||
147 | 5 | $this->anon = new \Inet\Neuralyzer\Anonymizer\DB($this->pdo); |
|
148 | 5 | $this->anon->setConfiguration($reader); |
|
149 | |||
150 | 5 | $stopwatch = new Stopwatch(); |
|
151 | 5 | $stopwatch->start('Neuralyzer'); |
|
152 | // Get tables |
||
153 | 5 | $tables = $reader->getEntities(); |
|
154 | 5 | foreach ($tables as $table) { |
|
155 | 5 | $this->anonymizeTable($table, $input, $output); |
|
156 | } |
||
157 | |||
158 | // Get memory and execution time information |
||
159 | 4 | $event = $stopwatch->stop('Neuralyzer'); |
|
160 | 4 | $memory = round($event->getMemory() / 1024 / 1024, 2); |
|
161 | 4 | $time = round($event->getDuration() / 1000, 2); |
|
162 | 4 | $time = ($time > 180 ? round($time / 60, 2) . 'mins' : "$time sec"); |
|
163 | |||
164 | 4 | $output->writeln("<info>Done in $time using $memory Mb of memory</info>"); |
|
165 | 4 | } |
|
166 | |||
167 | /** |
||
168 | * Anonmyze a specific table and display info about the job |
||
169 | * |
||
170 | * @param string $table |
||
171 | */ |
||
172 | 5 | private function anonymizeTable(string $table) |
|
173 | { |
||
174 | 5 | $total = $this->countRecords($table); |
|
175 | 4 | if ($total === 0) { |
|
176 | 1 | $this->output->writeln("<info>$table is empty</info>"); |
|
177 | 1 | return; |
|
178 | } |
||
179 | |||
180 | 3 | $bar = new ProgressBar($this->output, $total); |
|
181 | 3 | $bar->setRedrawFrequency($total > 100 ? 100 : 0); |
|
182 | |||
183 | 3 | $this->output->writeln("<info>Anonymizing $table</info>"); |
|
184 | |||
185 | try { |
||
186 | 3 | $queries = $this->anon->processEntity($table, function () use ($bar) { |
|
187 | 2 | $bar->advance(); |
|
188 | 3 | }, $this->input->getOption('pretend'), $this->input->getOption('sql')); |
|
189 | 1 | } catch (\Exception $e) { |
|
190 | 1 | $msg = "<error>Error anonymizing $table. Message was : " . $e->getMessage() . "</error>"; |
|
191 | 1 | $this->output->writeln(PHP_EOL . $msg . PHP_EOL); |
|
192 | 1 | return; |
|
193 | } |
||
194 | |||
195 | 2 | $this->output->writeln(PHP_EOL); |
|
196 | |||
197 | 2 | if ($this->input->getOption('sql')) { |
|
198 | 1 | $this->output->writeln('<comment>Queries:</comment>'); |
|
199 | 1 | $this->output->writeln(implode(PHP_EOL, $queries)); |
|
200 | 1 | $this->output->writeln(PHP_EOL); |
|
201 | } |
||
202 | 2 | } |
|
203 | |||
204 | /** |
||
205 | * Count records on a table |
||
206 | * @param string $table |
||
207 | * @return int |
||
208 | */ |
||
209 | 5 | private function countRecords(string $table): int |
|
222 | } |
||
223 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: