taskRepository->find($taskId); if ($task === null) { throw new \InvalidArgumentException("Task {$taskId} not found"); } $oldStatus = $task->getStatus(); match ($newStatus) { 'in_progress' => $task->start(), 'completed' => $task->complete(), 'failed' => $task->fail(), 'cancelled' => $task->cancel(), 'pending' => $task->retry(), default => throw new \InvalidArgumentException("Invalid status: {$newStatus}"), }; $this->taskRepository->update($task); $comment = TaskComment::createStatusChange( $taskId, $updatedBy, $updatedByType, $oldStatus->label(), $task->getStatus()->label() ); $this->commentRepository->save($comment); return $task; } public function startTask(int $taskId, string $updatedBy, string $updatedByType = 'human'): Task { return $this->execute($taskId, 'in_progress', $updatedBy, $updatedByType); } public function completeTask(int $taskId, string $updatedBy, string $updatedByType = 'human'): Task { return $this->execute($taskId, 'completed', $updatedBy, $updatedByType); } public function failTask(int $taskId, string $updatedBy, string $updatedByType = 'human'): Task { return $this->execute($taskId, 'failed', $updatedBy, $updatedByType); } public function cancelTask(int $taskId, string $updatedBy, string $updatedByType = 'human'): Task { return $this->execute($taskId, 'cancelled', $updatedBy, $updatedByType); } }