Skip to content

Commit

Permalink
[TASK] Add methods to add a task before/after a task in a task group
Browse files Browse the repository at this point in the history
  • Loading branch information
ochorocho committed Dec 12, 2023
1 parent 11cef74 commit 28f8f11
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Task/GroupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,31 @@ public function setGroup(array $group): void
{
$this->group = $group;
}

public function addTaskBefore(string $task, string $addTask): void
{
$this->addTask($task, $addTask);
}

public function addTaskAfter(string $task, string $addTask): void
{
$this->addTask($task, $addTask, 'after');
}

public function addTask(string $task, string $addTask, string $position = 'before'): void
{
$taskPosition = array_search($task, $this->group);
if(!$taskPosition) {
throw new \InvalidArgumentException("Task `$task` not found.");
}

switch ($position) {
case 'before':
array_splice($this->group, $taskPosition, 0, [$addTask]);
break;
case 'after':
array_splice($this->group, $taskPosition + 1, 0, [$addTask]);
break;
}
}
}

0 comments on commit 28f8f11

Please sign in to comment.