taskRepository = $taskRepository ?? new TaskRepository(); $this->commentRepository = $commentRepository ?? new TaskCommentRepository(); } public function execute(int $taskId, string $newStatus, string $updatedBy, string $updatedByType = 'human'): Task { $task = $this->taskRepository->find($taskId); if ($task === null) { throw new \InvalidArgumentException("Task {$taskId} not found"); } $newStatusEnum = TaskStatus::from($newStatus); $oldStatus = $task->getStatus(); if (!$oldStatus->canTransitionTo($newStatusEnum)) { throw new \InvalidArgumentException( "Cannot transition from {$oldStatus->value} to {$newStatus}" ); } $task->setStatus($newStatusEnum); $this->taskRepository->update($task); $comment = TaskComment::createStatusChange( $taskId, $updatedBy, $updatedByType, $oldStatus->label(), $newStatusEnum->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); } }