SeiteExplorerRepository.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 4

Klassen 1

Funktionen 10

Verwendet von 1

Code

<?php

declare(strict_types=1);

namespace Infrastructure\Persistence;

use Domain\Repository\SeiteExplorerRepositoryInterface;
use PDO;

// @responsibility: Persistenz für Seiten-Explorer (Unterseiten mit Stats)

class SeiteExplorerRepository implements SeiteExplorerRepositoryInterface
{
    private PDO $pdo;

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

    public function countSeiten(): int
    {
        return (int) $this->pdo->query(
            'SELECT COUNT(*) FROM dokumentation WHERE depth > 0'
        )->fetchColumn();
    }

    public function countSeitenFiltered(string $search = '', string $parentId = ''): int
    {
        $sql = 'SELECT COUNT(*) FROM dokumentation s WHERE s.depth > 0';
        $params = [];

        if ($search !== '') {
            $sql .= ' AND (s.title LIKE :search OR s.path LIKE :search2)';
            $params['search'] = '%' . $search . '%';
            $params['search2'] = '%' . $search . '%';
        }

        if ($parentId !== '') {
            $sql .= ' AND s.parent_id = :parent';
            $params['parent'] = (int) $parentId;
        }

        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($params);

        return (int) $stmt->fetchColumn();
    }

    /**
     * @return array<array<string, mixed>>
     */
    public function getSeitenFiltered(string $search = '', string $parentId = ''): array
    {
        $sql = 'SELECT s.id, s.title, s.path, s.depth, s.parent_id,
                       p.title as parent_title,
                       (SELECT COUNT(*) FROM dokumentation_chunks WHERE dokumentation_id = s.id) as chunks_count,
                       (SELECT COALESCE(SUM(token_count), 0) FROM dokumentation_chunks WHERE dokumentation_id = s.id) as token_count
                FROM dokumentation s
                LEFT JOIN dokumentation p ON s.parent_id = p.id
                WHERE s.depth > 0';
        $params = [];

        if ($search !== '') {
            $sql .= ' AND (s.title LIKE :search OR s.path LIKE :search2)';
            $params['search'] = '%' . $search . '%';
            $params['search2'] = '%' . $search . '%';
        }

        if ($parentId !== '') {
            $sql .= ' AND s.parent_id = :parent_id';
            $params['parent_id'] = (int) $parentId;
        }

        $sql .= ' ORDER BY s.path';

        $stmt = $this->pdo->prepare($sql);
        $stmt->execute($params);

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * @return array<array<string, mixed>>
     */
    public function getSeitenPaginated(
        string $search = '',
        string $parentId = '',
        int $limit = 50,
        int $offset = 0
    ): array {
        $sql = 'SELECT s.id, s.title, s.path, s.depth, s.parent_id,
                       p.title as parent_title,
                       (SELECT COUNT(*) FROM dokumentation_chunks WHERE dokumentation_id = s.id) as chunks_count,
                       (SELECT COALESCE(SUM(token_count), 0) FROM dokumentation_chunks WHERE dokumentation_id = s.id) as token_count
                FROM dokumentation s
                LEFT JOIN dokumentation p ON s.parent_id = p.id
                WHERE s.depth > 0';
        $params = [];

        if ($search !== '') {
            $sql .= ' AND (s.title LIKE :search OR s.path LIKE :search2)';
            $params['search'] = '%' . $search . '%';
            $params['search2'] = '%' . $search . '%';
        }

        if ($parentId !== '') {
            $sql .= ' AND s.parent_id = :parent';
            $params['parent'] = (int) $parentId;
        }

        $sql .= ' ORDER BY p.title, s.depth, s.title LIMIT :limit OFFSET :offset';

        $stmt = $this->pdo->prepare($sql);
        foreach ($params as $key => $value) {
            $stmt->bindValue(':' . $key, $value, is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR);
        }
        $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
        $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
        $stmt->execute();

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * @return array<string, mixed>|null
     */
    public function getSeiteWithParent(int $id): ?array
    {
        $stmt = $this->pdo->prepare(
            'SELECT s.*, p.title as parent_title, p.path as parent_path
             FROM dokumentation s
             LEFT JOIN dokumentation p ON s.parent_id = p.id
             WHERE s.id = :id'
        );
        $stmt->execute(['id' => $id]);
        $result = $stmt->fetch(PDO::FETCH_ASSOC);

        return $result !== false ? $result : null;
    }

    /**
     * @return array<array<string, mixed>>
     */
    public function getSeitenWithStatsForParent(int $parentId): array
    {
        $stmt = $this->pdo->prepare(
            'SELECT s.id, s.title, s.path, s.depth,
                    (SELECT COUNT(*) FROM dokumentation_chunks WHERE dokumentation_id = s.id) as chunks_count,
                    (SELECT COALESCE(SUM(token_count), 0) FROM dokumentation_chunks WHERE dokumentation_id = s.id) as token_count
             FROM dokumentation s
             WHERE s.parent_id = :id
             ORDER BY s.title'
        );
        $stmt->execute(['id' => $parentId]);

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * @return array<array<string, mixed>>
     */
    public function getUnterseiten(int $parentId): array
    {
        $stmt = $this->pdo->prepare(
            'SELECT id, title, path, depth FROM dokumentation WHERE parent_id = :id ORDER BY title'
        );
        $stmt->execute(['id' => $parentId]);

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * @param array<string, mixed> $seite
     * @return array<array{id: int, title: string, path: string, depth: int}>
     */
    public function buildBreadcrumb(array $seite): array
    {
        $breadcrumb = [];
        $current = $seite;

        while ($current !== null && $current !== false) {
            array_unshift($breadcrumb, [
                'id' => $current['id'],
                'title' => $current['title'],
                'path' => $current['path'],
                'depth' => $current['depth'],
            ]);

            if ($current['parent_id'] === null) {
                break;
            }

            $stmt = $this->pdo->prepare('SELECT * FROM dokumentation WHERE id = :id');
            $stmt->execute(['id' => $current['parent_id']]);
            $current = $stmt->fetch(PDO::FETCH_ASSOC);
        }

        return $breadcrumb;
    }

    /**
     * @return array<array{taxonomy_category: string, count: int}>
     */
    public function getTaxonomyForDokumentTree(int $dokumentId): array
    {
        $stmt = $this->pdo->prepare(
            'SELECT taxonomy_category, COUNT(*) as count
             FROM dokumentation_chunks
             WHERE dokumentation_id IN (
                 SELECT id FROM dokumentation WHERE id = :id OR parent_id = :id2
             )
             AND taxonomy_category IS NOT NULL
             GROUP BY taxonomy_category
             ORDER BY count DESC'
        );
        $stmt->execute(['id' => $dokumentId, 'id2' => $dokumentId]);

        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}
← Übersicht Graph