<?php
declare(strict_types=1);
namespace UseCases\Task;
// @responsibility: Löscht Tasks nach Validierung der Subtask-Abhängigkeiten
use Infrastructure\Persistence\TaskRepository;
class DeleteTaskUseCase
{
private TaskRepository $taskRepository;
public function __construct(?TaskRepository $taskRepository = null)
{
$this->taskRepository = $taskRepository ?? new 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;
}
}