tasks = array(); $this->taskOutput = array(); } /** * @inheritDoc * @throws TasksException */ public function addTask(Task $task): bool { // Check if the already exists $taskId = $task->getId(); foreach ($this->tasks as $t) { if ($t->getId() === $taskId) throw new TasksException("Could not add Task to TaskStorage. Task '$taskId' already exists."); } $this->tasks[] = clone $task; return true; } /** * @inheritDoc */ public function readTasks(bool $noIncludeDone = false): array { if ($noIncludeDone === false) return $this->tasks; $tasks = []; foreach ($this->tasks as $task) { if ($task->getStatus() !== Task::COMPLETED && $task->getStatus() !== Task::CANCELLED) $tasks[] = $task; } return $tasks; } /** * @inheritDoc */ public function getTaskById(string $identifier): Task { foreach ($this->tasks as $t) if ($t->getId() === $identifier) return $t; throw new TasksException("Could not get task by id. Task not found."); } /** * @inheritDoc */ public function modifyTask(Task $task): bool { // Fire the TaskModifyEvent /** @var TaskModifyEvent $event */ $event = Events::fireEvent(new TaskModifyEvent(), $task); if ($event->isCancelled()) { Logger::log("Did not modify task. Cancelled by taskModifyEvent."); return false; } // And finally replace Task with the event based one. $task = $event->getTask(); $taskId = $task->getId(); for ($i=0;$itasks);$i++) { if ($this->tasks[$i]->getId() === $taskId) { $this->tasks[$i] = clone $task; return true; } } throw new TasksException("Could not modify task. Task '$taskId' doesn't exist."); } /** * @inheritDoc * @throws TasksException */ public function deleteTask(Task $task): bool { $taskId = $task->getId(); for ($i=0;$itasks);$i++) { if ($this->tasks[$i]->getId() === $taskId) { // Remove the task from the main storage unset($this->tasks[$i]); return true; } } throw new TasksException("Could not delete task. Task '$taskId' doesn't exist."); } /** * @inheritDoc */ public function writeTaskOutput(Task $task, string $output, string $errors, int $statusCode): bool { // First check if the task exists $task = $this->getTaskById($task->getId()); // Set the attempt number if (!isset($this->taskOutput[$task->getId()]['outAttempts'])) { $this->taskOutput[$task->getId()]['outAttempts'] = 1; $attempt = $this->taskOutput[$task->getId()]['outAttempts']; } else $attempt = $this->taskOutput[$task->getId()]['outAttempts']++; if (isset($this->taskOutput[$task->getId()]['task'][$attempt])) throw new TasksException("Could not write task output. Output already written."); $this->taskOutput[$task->getId()]['task'][$attempt] = [ 'output' => $output, 'errors' => $errors, 'statusCode' => $statusCode ]; return true; } /** * @inheritDoc */ public function writePostOutput(Task $task, string $output, string $errors, int $statusCode): bool { // First check if the task exists $task = $this->getTaskById($task->getId()); // Set the attempt number if (!isset($this->taskOutput[$task->getId()]['postAttempts'])) { $this->taskOutput[$task->getId()]['postAttempts'] = 1; $attempt = $this->taskOutput[$task->getId()]['postAttempts']; } else $attempt = $this->taskOutput[$task->getId()]['postAttempts']++; if (isset($this->taskOutput[$task->getId()]['post'][$attempt])) throw new TasksException("Could not write task post output. Output already written."); $this->taskOutput[$task->getId()]['post'][$attempt] = [ 'output' => $output, 'errors' => $errors, 'statusCode' => $statusCode ]; return true; } /** * @inheritDoc */ public function readTaskOutput(Task $task, int $attempt = 0): ?array { if (!isset($this->taskOutput[$task->getId()]['task'])) return null; if ($attempt === 0) $attempt = count($this->taskOutput[$task->getId()]['task']); if ($attempt === -1) return $this->taskOutput[$task->getId()]['task']; else { if (isset($this->taskOutput[$task->getId()]['task'][$attempt])) return $this->taskOutput[$task->getId()]['task'][$attempt]; } return null; } /** * @inheritDoc */ public function readPostOutput(Task $task, int $attempt = 0): ?array { if (!isset($this->taskOutput[$task->getId()]['post'])) return null; if ($attempt === 0) $attempt = count($this->taskOutput[$task->getId()]['post']); if ($attempt === -1) return $this->taskOutput[$task->getId()]['post']; else { if (isset($this->taskOutput[$task->getId()]['post'][$attempt])) return $this->taskOutput[$task->getId()]['post'][$attempt]; } return null; } /** * @inheritDoc */ public function reset(): bool { $this->tasks = []; $this->taskOutput = []; return true; } }