Back to TasksStateCommand class

Method doExecute

protected int
doExecute
(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output)
Internal function to execute the command.
Parameters
  • \Symfony\Component\Console\Input\InputInterface $input The input to inject into the command.
  • \Symfony\Component\Console\Output\OutputInterface $output The output to inject into the command.
Returns
  • int The command exit code
Since
  • 4.1.0
-
  • \Exception

Method doExecute - Source code

/**
 * Internal function to execute the command.
 *
 * @param   InputInterface   $input   The input to inject into the command.
 * @param   OutputInterface  $output  The output to inject into the command.
 *
 * @return  integer  The command exit code
 *
 * @since   4.1.0
 * @throws \Exception
 */
protected function doExecute(InputInterface $input, OutputInterface $output) : int
{
    Factory::getApplication()->getLanguage()->load('joomla', JPATH_ADMINISTRATOR);
    $this->configureIO($input, $output);
    $id = (string) $input->getOption('id');
    $state = (string) $input->getOption('state');
    // Try to validate and process ID, if passed
    if (\strlen($id)) {
        if (!Task::isValidId($id)) {
            $this->ioStyle->error('Invalid id passed!');
            return 2;
        }
        $id = is_numeric($id) ? $id + 0 : $id;
    }
    // Try to validate and process state, if passed
    if (\strlen($state)) {
        // If we get the logical state, we try to get the enumeration (but as a string)
        if (!is_numeric($state)) {
            $state = (string) ArrayHelper::arraySearch($state, Task::STATE_MAP);
        }
        if (!\strlen($state) || !Task::isValidState($state)) {
            $this->ioStyle->error('Invalid state passed!');
            return 2;
        }
    }
    // If we didn't get ID as a flag, ask for it interactively
    while (!Task::isValidId($id)) {
        $id = $this->ioStyle->ask('Please specify the ID of the task');
    }
    // If we didn't get state as a flag, ask for it interactively
    while ($state === false || !Task::isValidState($state)) {
        $state = (string) $this->ioStyle->ask('Should the state be "enable" (1), "disable" (0) or "trash" (-2)');
        // Ensure we have the enumerated value (still as a string)
        $state = Task::isValidState($state) ? $state : ArrayHelper::arraySearch($state, Task::STATE_MAP);
    }
    // Finally, the enumerated state and id in their pure form
    $state = (int) $state;
    $id = (int) $id;
    /** @var ConsoleApplication $app */
    $app = $this->getApplication();
    /** @var TaskModel $taskModel */
    $taskModel = $app->bootComponent('com_scheduler')->getMVCFactory()->createModel('Task', 'Administrator');
    $task = $taskModel->getItem($id);
    // We couldn't fetch that task :(
    if (empty($task->id)) {
        $this->ioStyle->error("Task ID '{$id}' does not exist!");
        return 1;
    }
    // If the item is checked-out we need a check in (currently not possible through the CLI)
    if ($taskModel->isCheckedOut($task)) {
        $this->ioStyle->error("Task ID '{$id}' is checked out!");
        return 1;
    }
    /** @var TaskTable $table */
    $table = $taskModel->getTable();
    $action = Task::STATE_MAP[$state];
    if (!$table->publish($id, $state)) {
        $this->ioStyle->error("Can't {$action} Task ID '{$id}'");
        return 3;
    }
    $this->ioStyle->success("Task ID {$id} {$action}.");
    return 0;
}