diff --git a/libraries/src/Console/TasksStateCommand.php b/libraries/src/Console/TasksStateCommand.php index 94665264d83..aaf7dafb013 100644 --- a/libraries/src/Console/TasksStateCommand.php +++ b/libraries/src/Console/TasksStateCommand.php @@ -15,6 +15,7 @@ use Joomla\CMS\Factory; use Joomla\Component\Jobs\Administrator\Table\TaskTable; use Joomla\Component\Scheduler\Administrator\Model\TaskModel; +use Joomla\Component\Scheduler\Administrator\Task\Task; use Joomla\Console\Application; use Joomla\Console\Command\AbstractCommand; use Joomla\Utilities\ArrayHelper; @@ -54,38 +55,6 @@ class TasksStateCommand extends AbstractCommand */ private $ioStyle; - /** - * State to enable. - * - * @since __DEPLOY_VERSION__ - */ - const STATE_ENABLE = 1; - - /** - * State to disable. - * - * @since __DEPLOY_VERSION__ - */ - const STATE_DISABLE = 0; - - /** - * State to trash. - * - * @since __DEPLOY_VERSION__ - */ - const STATE_TRASH = -2; - - /** - * Map state enumerations to language verbs. - * - * @since __DEPLOY__VERSION__ - */ - const STATE_MAP = [ - self::STATE_TRASH => 'trash', - self::STATE_DISABLE => 'disable', - self::STATE_ENABLE => 'enable', - ]; - /** * Internal function to execute the command. * @@ -103,20 +72,32 @@ protected function doExecute(InputInterface $input, OutputInterface $output): in $this->configureIO($input, $output); - $id = (int) $input->getOption('id'); + $id = (string) $input->getOption('id'); $state = (string) $input->getOption('state'); - while (!$id) + // Try to validate and process ID, if passed + if (\strlen($id)) { - $id = (int) $this->ioStyle->ask('Please specify the ID of the task'); + if (!Task::isValidId($id)) + { + $this->ioStyle->error('Invalid id passed!'); + + return 2; + } + + $id = (is_numeric($id)) ? ($id + 0) : $id; } - if (\strlen($state) && !is_numeric($state)) + // Try to validate and process state, if passed + if (\strlen($state)) { - // We try to get the enumerated state here (but as a string) - $state = (string) ArrayHelper::arraySearch($state, self::STATE_MAP); + // 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)) + if (!\strlen($state) || !Task::isValidState($state)) { $this->ioStyle->error('Invalid state passed!'); @@ -124,17 +105,24 @@ protected function doExecute(InputInterface $input, OutputInterface $output): in } } + // 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 || !$this->isValidState($state)) + 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 = ($this->isValidState($state)) ?: ArrayHelper::arraySearch($state, self::STATE_MAP); + $state = (Task::isValidState($state)) ?: ArrayHelper::arraySearch($state, Task::STATE_MAP); } - // Finally, the enumerated state in its pure form + // Finally, the enumerated state and id in their pure form $state = (int) $state; + $id = (int) $id; /** @var ConsoleApplication $app */ $app = $this->getApplication(); @@ -163,7 +151,7 @@ protected function doExecute(InputInterface $input, OutputInterface $output): in /** @var TaskTable $table */ $table = $taskModel->getTable(); - $action = self::STATE_MAP[$state]; + $action = Task::STATE_MAP[$state]; if (!$table->publish($id, $state)) { @@ -172,8 +160,7 @@ protected function doExecute(InputInterface $input, OutputInterface $output): in return 3; } - $actionAdjective = $action . ($action[-1] === 'e' ? 'd' : 'ed'); - $this->ioStyle->success("Task ID ${id} ${actionAdjective}."); + $this->ioStyle->success("Task ID ${id} ${action}."); return 0; } @@ -211,26 +198,4 @@ protected function configure(): void $this->setDescription('Enable, disable or trash a scheduled task'); $this->setHelp($help); } - - /** - * Private method to determine whether an enumerated task state (as a string) is valid. - * - * @param string $state The task state (enumerated). - * - * @return boolean - * - * @since __DEPLOY_VERSION__ - */ - private function isValidState(string $state): bool - { - if (!is_numeric($state)) - { - return false; - } - - // Takes care of interpreting as float/int - $state = $state + 0; - - return ArrayHelper::getValue(self::STATE_MAP, $state) !== null; - } }