Backup #1871

ID1871
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Infrastructure/Persistence/RelationRepository.php
Version4
Typ modified
Größe3.9 KB
Hashffde0838944045012accd943b40a116ec013a081c367ecc53f1c7d8c3b8daf6a
Datum2025-12-27 23:50:10
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 Entity-Relationen

use Domain\Constants;
use Domain\Repository\RelationRepositoryInterface;
use PDO;

final class RelationRepository implements RelationRepositoryInterface
{
    private PDO $db;

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

    /**
     * {@inheritDoc}
     */
    public function findFiltered(string $type = '', int $limit = 100): array
    {
        $sql = 'SELECT er.*,
                       es.name as source_name, es.type as source_type,
                       et.name as target_name, et.type as target_type
                FROM entity_relations er
                JOIN entities es ON er.source_entity_id = es.id
                JOIN entities et ON er.target_entity_id = et.id
                WHERE 1=1';

        $params = [];

        if ($type !== '') {
            $sql .= ' AND er.relation_type = :type';
            $params['type'] = $type;
        }

        $sql .= ' ORDER BY er.strength DESC LIMIT ' . $limit;

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

        return $stmt->fetchAll();
    }

    /**
     * {@inheritDoc}
     */
    public function getTypes(): array
    {
        return $this->db->query(
            'SELECT relation_type, COUNT(*) as count
             FROM entity_relations
             GROUP BY relation_type
             ORDER BY count DESC'
        )->fetchAll();
    }

    /**
     * {@inheritDoc}
     */
    public function getStats(): array
    {
        $result = $this->db->query(
            'SELECT
                (SELECT COUNT(*) FROM entity_relations) as total,
                (SELECT COUNT(DISTINCT source_entity_id) FROM entity_relations) as sources,
                (SELECT COUNT(DISTINCT target_entity_id) FROM entity_relations) as targets'
        )->fetch();

        return $result !== false ? $result : ['total' => 0, 'sources' => 0, 'targets' => 0];
    }

    /**
     * {@inheritDoc}
     */
    public function find(int $id): ?array
    {
        $stmt = $this->db->prepare(
            'SELECT er.*,
                    es.name as source_name, es.type as source_type,
                    et.name as target_name, et.type as target_type
             FROM entity_relations er
             JOIN entities es ON er.source_entity_id = es.id
             JOIN entities et ON er.target_entity_id = et.id
             WHERE er.id = :id'
        );
        $stmt->execute(['id' => $id]);
        $result = $stmt->fetch();

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

    /**
     * {@inheritDoc}
     */
    public function create(int $sourceId, int $targetId, string $type, float $strength = 1.0): int
    {
        $stmt = $this->db->prepare(
            'INSERT INTO entity_relations (source_entity_id, target_entity_id, relation_type, strength, created_at)
             VALUES (:source, :target, :type, :strength, NOW())'
        );
        $stmt->execute([
            'source' => $sourceId,
            'target' => $targetId,
            'type' => $type,
            'strength' => $strength,
        ]);

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

    /**
     * {@inheritDoc}
     */
    public function update(int $id, string $type, float $strength): bool
    {
        $stmt = $this->db->prepare(
            'UPDATE entity_relations SET relation_type = :type, strength = :strength WHERE id = :id'
        );

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

    /**
     * {@inheritDoc}
     */
    public function delete(int $id): bool
    {
        $stmt = $this->db->prepare('DELETE FROM entity_relations WHERE id = :id');

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

    /**
     * {@inheritDoc}
     */
    public function getTypesList(): array
    {
        return ['RELATED_TO', 'PART_OF', 'DEVELOPED_BY', 'INFLUENCED_BY', 'USED_IN', 'WORKS_WITH', 'TEACHES', 'CREATED'];
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
1871 4 modified 3.9 KB 2025-12-27 23:50
1867 3 modified 3.9 KB 2025-12-27 23:49
1166 2 modified 3.9 KB 2025-12-25 10:28
1159 1 modified 4.0 KB 2025-12-25 10:28

← Zurück zur Übersicht