/**
  * Execute the command
  *
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $db = $input->getOption('db');
     // Throw an exception immediately if we dont have the required DB parameter
     if (empty($db)) {
         throw new \InvalidArgumentException('Database name is required (--db)');
     }
     $password = $input->getOption('password');
     if (is_null($password)) {
         $helper = $this->getHelper('question');
         $question = new Question('Password: '******'host'), $input->getOption('user'), $password);
         $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
     } catch (\Exception $e) {
         throw new \RuntimeException("Can't connect to the database. Check your credentials");
     }
     // Anon READER
     $reader = new \Inet\Neuralyzer\Configuration\Reader($input->getOption('config'));
     // Now work on the DB
     $anon = new \Inet\Neuralyzer\Anonymizer\DB($pdo);
     $anon->setConfiguration($reader);
     $stopwatch = new Stopwatch();
     $stopwatch->start('Neuralyzer');
     // Get tables
     $tables = $reader->getEntities();
     foreach ($tables as $table) {
         try {
             $result = $pdo->query("SELECT COUNT(1) FROM {$table}");
         } catch (\Exception $e) {
             throw new \InvalidArgumentException("Could not count records in table '{$table}' defined in your config");
         }
         $data = $result->fetchAll(\PDO::FETCH_COLUMN);
         $total = (int) $data[0];
         if ($total === 0) {
             $output->writeln("<info>{$table} is empty</info>");
             continue;
         }
         $bar = new ProgressBar($output, $total);
         $bar->setRedrawFrequency($total > 100 ? 100 : 0);
         $output->writeln("<info>Anonymizing {$table}</info>");
         $queries = $anon->processEntity($table, function () use($bar) {
             $bar->advance();
         }, $input->getOption('pretend'), $input->getOption('sql'));
         $output->writeln(PHP_EOL);
         if ($input->getOption('sql')) {
             $output->writeln('<comment>Queries:</comment>');
             $output->writeln(implode(PHP_EOL, $queries));
             $output->writeln(PHP_EOL);
         }
     }
     // Get memory and execution time information
     $event = $stopwatch->stop('Neuralyzer');
     $memory = round($event->getMemory() / 1024 / 1024, 2);
     $time = round($event->getDuration() / 1000, 2);
     $time = $time > 180 ? round($time / 60, 2) . 'mins' : "{$time} sec";
     $output->writeln("<info>Done in {$time} using {$memory} Mb of memory</info>");
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $stopwatch = new \Symfony\Component\Stopwatch\Stopwatch();
     $stopwatch->start('Anon');
     $pdo = $this->getService('sugarcrm.pdo');
     $this->getService('sugarcrm.entrypoint');
     // go to sugar folder to make sure we are in the right folder
     // Make sure that we don't anonymize production
     if ($input->getOption('force') === true && $this->askConfirmation($input, $output) === false) {
         return;
     }
     // Anon READER
     $reader = new \Inet\Neuralyzer\Configuration\Reader($input->getOption('file'));
     // Now work on the DB
     $anon = new \Inet\Neuralyzer\Anonymizer\DB($pdo);
     $anon->setConfiguration($reader);
     // Get tables
     $tables = $input->getOption('table');
     if (empty($tables)) {
         $tables = $reader->getEntities();
     }
     $pretend = $input->getOption('force') === true ? false : true;
     foreach ($tables as $table) {
         // Do I need to clean the table ?
         $this->cleanTable($input, $output, $pdo, $table);
         // Start to work
         $result = $pdo->query($sql = "SELECT COUNT(1) FROM {$table}");
         if ($result === false) {
             throw new \PDOException("Error in SQL: {$sql}, are you sure {$table} exists ?");
         }
         $data = $result->fetchAll(\PDO::FETCH_COLUMN);
         $total = (int) $data[0];
         if ($total === 0) {
             $output->writeln("<info>{$table} is empty</info>" . PHP_EOL);
             continue;
         }
         $bar = new ProgressBar($output, $total);
         $bar->setRedrawFrequency($total > 100 ? 100 : 0);
         $output->writeln("<info>Anonymizing {$table}</info>");
         $queries = $anon->processEntity($table, function () use($bar) {
             $bar->advance();
         }, $pretend, $input->getOption('sql'));
         $output->writeln(PHP_EOL);
         if ($input->getOption('sql')) {
             $output->writeln('<comment>Queries:</comment>');
             $output->writeln(implode(PHP_EOL, $queries));
             $output->writeln(PHP_EOL);
         }
     }
     $this->cleanAuditAndTrackers($output, $pdo);
     // Get memory and execution time information
     $event = $stopwatch->stop('Anon');
     $memory = round($event->getMemory() / 1024 / 1024, 2);
     $time = round($event->getDuration() / 1000, 2);
     $time = $time > 180 ? round($time / 60, 2) . 'mins' : "{$time} sec";
     // Final message
     $output->writeln(PHP_EOL . "<comment>Done in {$time} (consuming {$memory}Mb)</comment>");
     $db = $this->getDb($pdo);
     if ($input->getOption('force') === true) {
         $output->writeln(PHP_EOL . '<comment>To export the db run: </comment>');
         $output->writeln(" mysqldump --skip-lock-tables {$db} | bzip2 > {$db}." . date('Ymd-Hi') . '.sql.bz2');
     } else {
         $output->writeln(PHP_EOL . "<error>The anonymization didn't run. Use --force to run it.</error>");
     }
 }