Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
Improve scheduler:state command
Browse files Browse the repository at this point in the history
- Better validation and improved consistency.
- Removes now redundant state enumerations, mapping and validators
  which the Task class offers.
  • Loading branch information
ditsuke committed Nov 24, 2021
1 parent 6252e72 commit d1ec907
Showing 1 changed file with 32 additions and 67 deletions.
99 changes: 32 additions & 67 deletions libraries/src/Console/TasksStateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand All @@ -103,38 +72,57 @@ 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!');

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 || !$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();
Expand Down Expand Up @@ -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))
{
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}

0 comments on commit d1ec907

Please sign in to comment.