UpdateTaskStatusUseCase.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 6

Klassen 1

Funktionen 6

Verwendet von 3

Versionen 5

Code

<?php

declare(strict_types=1);

namespace UseCases\Task;

// @responsibility: Aktualisiert Task-Status mit Workflow-Validierung

use Domain\Entity\Task;
use Domain\Entity\TaskComment;
use Domain\Repository\TaskRepositoryInterface;
use Infrastructure\Persistence\TaskCommentRepository;

class UpdateTaskStatusUseCase
{
    public function __construct(
        private TaskRepositoryInterface $taskRepository,
        private TaskCommentRepository $commentRepository
    ) {
    }

    /**
     * Update task status with workflow validation.
     *
     * @throws \InvalidArgumentException When task not found or invalid status
     * @throws \RuntimeException When repository operations fail
     */
    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");
        }

        $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;
    }

    /**
     * @throws \InvalidArgumentException When task not found
     * @throws \RuntimeException When repository operations fail
     */
    public function startTask(int $taskId, string $updatedBy, string $updatedByType = 'human'): Task
    {
        return $this->execute($taskId, 'in_progress', $updatedBy, $updatedByType);
    }

    /**
     * @throws \InvalidArgumentException When task not found
     * @throws \RuntimeException When repository operations fail
     */
    public function completeTask(int $taskId, string $updatedBy, string $updatedByType = 'human'): Task
    {
        return $this->execute($taskId, 'completed', $updatedBy, $updatedByType);
    }

    /**
     * @throws \InvalidArgumentException When task not found
     * @throws \RuntimeException When repository operations fail
     */
    public function failTask(int $taskId, string $updatedBy, string $updatedByType = 'human'): Task
    {
        return $this->execute($taskId, 'failed', $updatedBy, $updatedByType);
    }

    /**
     * @throws \InvalidArgumentException When task not found
     * @throws \RuntimeException When repository operations fail
     */
    public function cancelTask(int $taskId, string $updatedBy, string $updatedByType = 'human'): Task
    {
        return $this->execute($taskId, 'cancelled', $updatedBy, $updatedByType);
    }
}
← Übersicht Graph