CreateTaskUseCase.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 7

Klassen 1

Funktionen 3

Verwendet von 2

Versionen 6

Code

<?php

declare(strict_types=1);

namespace UseCases\Task;

// @responsibility: Erstellt neue Tasks mit optionalen Kommentaren und Parent-Verknüpfung

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

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

    /** @param array<string, mixed> $data */
    public function execute(array $data): Task
    {
        $this->validate($data);

        $task = Task::create(
            $data['title'],
            $data['created_by'] ?? 'system',
            $data['description'] ?? null,
            isset($data['type']) ? TaskType::from($data['type']) : null,
            isset($data['parent_task_id']) ? (int) $data['parent_task_id'] : null,
            isset($data['due_date']) ? new \DateTimeImmutable($data['due_date']) : null
        );

        if (isset($data['metadata'])) {
            $task->updateMetadata($data['metadata']);
        }

        $id = $this->taskRepository->save($task);
        $task->setId($id);

        $comment = new TaskComment();
        $comment->setTaskId($id);
        $comment->setAuthor($task->getCreatedBy());
        $comment->setAuthorType($task->getCreatedByType());
        $comment->setCommentType('note');
        $comment->setContent('Task erstellt');
        $this->commentRepository->save($comment);

        return $task;
    }

    /** @param array<string, mixed> $data */
    private function validate(array $data): void
    {
        if (!isset($data['title']) || $data['title'] === '') {
            throw new \InvalidArgumentException('Title is required');
        }

        if (strlen($data['title']) > 255) {
            throw new \InvalidArgumentException('Title must be 255 characters or less');
        }

        if (isset($data['type']) && !in_array($data['type'], ['human_task', 'ai_task', 'mixed'])) {
            throw new \InvalidArgumentException('Invalid task type');
        }
    }
}
← Übersicht Graph