Backup #1167

ID1167
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Infrastructure/Persistence/TaxonomyRepository.php
Version3
Typ modified
Größe4.3 KB
Hashb7552f511ab42057c45082c32b366a0346d2ad7dd44eb6da6dbc189ea2b8465c
Datum2025-12-25 10:28:53
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Infrastructure\Persistence;

// @responsibility: CRUD-Operationen für Taxonomy-Begriffe (Hierarchie)

use Domain\Repository\TaxonomyRepositoryInterface;
use PDO;

final class TaxonomyRepository implements TaxonomyRepositoryInterface
{
    private PDO $db;

    public function __construct()
    {
        $this->db = DatabaseFactory::content();
    }

    /**
     * {@inheritDoc}
     */
    public function findAll(): array
    {
        return $this->db->query(
            'SELECT t.*,
                    COUNT(DISTINCT ct.chunk_id) as chunk_count,
                    (SELECT COUNT(*) FROM taxonomy_terms WHERE parent_id = t.id) as children_count
             FROM taxonomy_terms t
             LEFT JOIN chunk_taxonomy ct ON t.id = ct.taxonomy_term_id
             GROUP BY t.id
             ORDER BY t.path, t.name'
        )->fetchAll();
    }

    /**
     * {@inheritDoc}
     */
    public function getStats(): array
    {
        $result = $this->db->query(
            'SELECT
                (SELECT COUNT(*) FROM taxonomy_terms) as total_terms,
                (SELECT COUNT(*) FROM taxonomy_terms WHERE parent_id IS NULL) as root_terms,
                (SELECT MAX(depth) FROM taxonomy_terms) as max_depth,
                (SELECT COUNT(DISTINCT chunk_id) FROM chunk_taxonomy) as tagged_chunks'
        )->fetch();

        return $result !== false ? $result : [
            'total_terms' => 0,
            'root_terms' => 0,
            'max_depth' => 0,
            'tagged_chunks' => 0,
        ];
    }

    /**
     * {@inheritDoc}
     */
    public function find(int $id): ?array
    {
        $stmt = $this->db->prepare('SELECT * FROM taxonomy_terms WHERE id = :id');
        $stmt->execute(['id' => $id]);
        $result = $stmt->fetch();

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

    /**
     * {@inheritDoc}
     */
    public function create(string $name, ?int $parentId = null): int
    {
        $depth = 0;
        $path = $name;

        if ($parentId !== null) {
            $parent = $this->find($parentId);
            if ($parent !== null) {
                $depth = $parent['depth'] + 1;
                $path = $parent['path'] . '/' . $name;
            }
        }

        $stmt = $this->db->prepare(
            'INSERT INTO taxonomy_terms (name, parent_id, depth, path, created_at)
             VALUES (:name, :parent, :depth, :path, NOW())'
        );
        $stmt->execute([
            'name' => $name,
            'parent' => $parentId,
            'depth' => $depth,
            'path' => $path,
        ]);

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

    /**
     * {@inheritDoc}
     */
    public function update(int $id, string $name, ?int $parentId = null): bool
    {
        $depth = 0;
        $path = $name;

        if ($parentId !== null) {
            $parent = $this->find($parentId);
            if ($parent !== null) {
                $depth = $parent['depth'] + 1;
                $path = $parent['path'] . '/' . $name;
            }
        }

        $stmt = $this->db->prepare(
            'UPDATE taxonomy_terms SET name = :name, parent_id = :parent, depth = :depth, path = :path WHERE id = :id'
        );

        return $stmt->execute([
            'id' => $id,
            'name' => $name,
            'parent' => $parentId,
            'depth' => $depth,
            'path' => $path,
        ]);
    }

    /**
     * {@inheritDoc}
     */
    public function delete(int $id): bool
    {
        // Check for children
        $stmt = $this->db->prepare('SELECT COUNT(*) FROM taxonomy_terms WHERE parent_id = :id');
        $stmt->execute(['id' => $id]);

        if ((int) $stmt->fetchColumn() > 0) {
            return false; // Has children, cannot delete
        }

        // Delete chunk associations
        $stmt = $this->db->prepare('DELETE FROM chunk_taxonomy WHERE taxonomy_term_id = :id');
        $stmt->execute(['id' => $id]);

        // Delete term
        $stmt = $this->db->prepare('DELETE FROM taxonomy_terms WHERE id = :id');

        return $stmt->execute(['id' => $id]);
    }

    /**
     * {@inheritDoc}
     */
    public function findForSelect(): array
    {
        return $this->db->query(
            'SELECT id, name, depth, path FROM taxonomy_terms ORDER BY path, name'
        )->fetchAll();
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
1167 3 modified 4.3 KB 2025-12-25 10:28
1160 2 modified 4.3 KB 2025-12-25 10:28
1116 1 modified 4.3 KB 2025-12-25 09:24

← Zurück zur Übersicht