setName(self::COMMAND_NAME)
->addOption('--id', '-i', InputOption::VALUE_OPTIONAL, 'LeadField ID.')
->addOption('--user', '-u', InputOption::VALUE_OPTIONAL, 'User ID - User which receives a notification.')
->setHelp(
<<<'EOT'
The %command.name% command will create columns in a lead_fields table if the process should run in background.
php %command.full_name%
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$leadFieldId = (int) $input->getOption('id');
$userId = (int) $input->getOption('user');
if ($leadFieldId) {
return $this->addColumn($leadFieldId, $userId, $input, $output);
}
return $this->addAllMissingColumns($input, $output);
}
private function addAllMissingColumns(InputInterface $input, OutputInterface $output): int
{
$hasNoErrors = Command::SUCCESS;
while ($leadField = $this->leadFieldRepository->getFieldThatIsMissingColumn()) {
if (Command::FAILURE === $this->addColumn($leadField->getId(), $leadField->getCreatedBy(), $input, $output)) {
$hasNoErrors = Command::FAILURE;
}
}
return $hasNoErrors;
}
private function addColumn(int $leadFieldId, ?int $userId, InputInterface $input, OutputInterface $output): int
{
$moderationKey = sprintf('%s-%s-%s', self::COMMAND_NAME, $leadFieldId, $userId);
if (!$this->checkRunStatus($input, $output, $moderationKey)) {
return Command::SUCCESS;
}
try {
$this->backgroundService->addColumn($leadFieldId, $userId);
} catch (LeadFieldWasNotFoundException) {
$output->writeln(''.$this->translator->trans('mautic.lead.field.notfound').'');
return Command::FAILURE;
} catch (ColumnAlreadyCreatedException) {
$output->writeln(''.$this->translator->trans('mautic.lead.field.column_already_created').'');
return Command::SUCCESS;
} catch (AbortColumnCreateException) {
$output->writeln(''.$this->translator->trans('mautic.lead.field.column_creation_aborted').'');
return Command::SUCCESS;
} catch (CustomFieldLimitException|DriverException|SchemaException|\Doctrine\DBAL\Exception|\Mautic\CoreBundle\Exception\SchemaException $e) {
$output->writeln(''.$this->translator->trans($e->getMessage()).'');
return Command::FAILURE;
}
$output->writeln(''.$this->translator->trans('mautic.lead.field.column_was_created', ['%id%' => $leadFieldId]).'');
$this->completeRun();
return Command::SUCCESS;
}
}