DeleteTaskUseCase.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 2

Klassen 1

Funktionen 3

Verwendet von 2

Versionen 2

Code

<?php

declare(strict_types=1);

namespace UseCases\Task;

// @responsibility: Löscht Tasks nach Validierung der Subtask-Abhängigkeiten

use Domain\Repository\TaskRepositoryInterface;

class DeleteTaskUseCase
{
    public function __construct(
        private TaskRepositoryInterface $taskRepository
    ) {
    }

    public function execute(int $taskId): bool
    {
        $task = $this->taskRepository->find($taskId);
        if ($task === null) {
            throw new \InvalidArgumentException("Task {$taskId} not found");
        }

        $subtasks = $this->taskRepository->findSubtasks($taskId);
        if (count($subtasks) > 0) {
            throw new \InvalidArgumentException(
                'Cannot delete task with subtasks. Delete subtasks first or use cascade delete.'
            );
        }

        return $this->taskRepository->delete($taskId);
    }

    public function executeCascade(int $taskId): int
    {
        $task = $this->taskRepository->find($taskId);
        if ($task === null) {
            throw new \InvalidArgumentException("Task {$taskId} not found");
        }

        $deleted = 0;

        $subtasks = $this->taskRepository->findSubtasks($taskId);
        foreach ($subtasks as $subtask) {
            $deleted += $this->executeCascade($subtask->getId());
        }

        $this->taskRepository->delete($taskId);
        $deleted++;

        return $deleted;
    }
}
← Übersicht Graph