protected function execute(InputInterface $input, OutputInterface $output)
{
$this->io = new SymfonyStyle($input, $output);
$this->sort = $input->getOption('sort');
$this->sleepTime = $input->getOption('sleepTime');
if (!$input->hasOption('sleepTime') && $output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
$this->sleepTime = 25;
}
// Transform milliseconds in microseconds for usleep()
$this->sleepTime = $this->sleepTime * 1000;
$this->numberOfCodesToGenerate = $input->getOption('amount');
// The length of each outputted code
$this->codeLength = $input->getOption('length');
// All possible chars. By default, 'A' to 'Z' and '0' to '9'
$this->possibleChars = str_split($input->getOption('characters'));
$baseNumberOfChars = count($this->possibleChars);
$this->possibleChars = array_unique($this->possibleChars);
// If there's an error here, we'll say it later
$maxPossibleNumberOfCombinations = pow(count($this->possibleChars), $this->codeLength);
if ($maxPossibleNumberOfCombinations < $this->numberOfCodesToGenerate) {
$this->io->error(sprintf('Cannot generate %s combinations because there are only %s combinations possible', number_format($this->numberOfCodesToGenerate, 0, '', ' '), number_format($maxPossibleNumberOfCombinations, 0, '', ' ')));
return 1;
} else {
$this->io->block(sprintf('Generating %s combinations.', number_format($this->numberOfCodesToGenerate, 0, '', ' ')), null, 'info');
if ($maxPossibleNumberOfCombinations > $this->numberOfCodesToGenerate) {
$this->io->block(sprintf('Note: If you need you can generate %s more combinations (with a maximum of %s).', number_format($maxPossibleNumberOfCombinations - $this->numberOfCodesToGenerate, 0, '', ' '), number_format($maxPossibleNumberOfCombinations, 0, '', ' ')), null, 'comment');
}
}
$this->io->block('Available characters:');
$this->io->block(implode('', $this->possibleChars), null, 'info');
$codesList = $this->doGenerate();
$outputFile = $input->getOption('output');
if ($outputFile) {
$save = true;
if (file_exists($outputFile)) {
$save = $this->io->confirm(sprintf('File %s exists. Erase it?', $outputFile), false);
}
if ($save) {
$this->io->block(sprintf('Output results to %s', $outputFile), null, 'info');
if (!file_put_contents($outputFile, implode("\n", $codesList))) {
throw new \Exception(sprintf('Could not write to %s...', $outputFile));
}
}
} else {
$this->io->text($codesList);
}
if ($baseNumberOfChars !== count($this->possibleChars)) {
$this->io->warning(sprintf('We detected that there were duplicate characters in "%s", so we removed them.', $input->getOption('characters')));
}
return 0;
}