TaskCommentRepository.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 4

Klassen 1

Funktionen 6

Verwendet von 9

Versionen 5

Code

<?php

declare(strict_types=1);

namespace Infrastructure\Persistence;

// @responsibility: Persistenz für Task-Kommentare

use Domain\Entity\TaskComment;
use Domain\Repository\TaskCommentRepositoryInterface;

class TaskCommentRepository implements TaskCommentRepositoryInterface
{
    private \PDO $pdo;

    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    public function find(int $id): ?TaskComment
    {
        $stmt = $this->pdo->prepare('SELECT * FROM task_comments WHERE id = :id');
        $stmt->execute(['id' => $id]);
        $row = $stmt->fetch();

        return $row !== false ? TaskComment::fromArray($row) : null;
    }

    public function findByTaskId(int $taskId, int $limit = 50): array
    {
        $stmt = $this->pdo->prepare(
            'SELECT * FROM task_comments WHERE task_id = :task_id ORDER BY created_at DESC LIMIT :limit'
        );
        $stmt->bindValue(':task_id', $taskId, \PDO::PARAM_INT);
        $stmt->bindValue(':limit', $limit, \PDO::PARAM_INT);
        $stmt->execute();

        $comments = [];
        while ($row = $stmt->fetch()) {
            $comments[] = TaskComment::fromArray($row);
        }

        return $comments;
    }

    public function findByType(int $taskId, string $commentType): array
    {
        $stmt = $this->pdo->prepare(
            'SELECT * FROM task_comments WHERE task_id = :task_id AND comment_type = :type ORDER BY created_at DESC'
        );
        $stmt->execute(['task_id' => $taskId, 'type' => $commentType]);

        $comments = [];
        while ($row = $stmt->fetch()) {
            $comments[] = TaskComment::fromArray($row);
        }

        return $comments;
    }

    public function save(TaskComment $comment): int
    {
        $sql = 'INSERT INTO task_comments (
            task_id, author, author_type, comment_type,
            content, metadata, created_at
        ) VALUES (
            :task_id, :author, :author_type, :comment_type,
            :content, :metadata, :created_at
        )';

        $stmt = $this->pdo->prepare($sql);
        $data = $comment->toArray();

        $stmt->execute([
            'task_id' => $data['task_id'],
            'author' => $data['author'],
            'author_type' => $data['author_type'],
            'comment_type' => $data['comment_type'],
            'content' => $data['content'],
            'metadata' => json_encode($data['metadata']),
            'created_at' => $data['created_at'],
        ]);

        return (int) $this->pdo->lastInsertId();
    }

    public function delete(int $id): bool
    {
        $stmt = $this->pdo->prepare('DELETE FROM task_comments WHERE id = :id');

        return $stmt->execute(['id' => $id]);
    }
}
← Übersicht Graph