RelationRepository.php
- Pfad:
src/Infrastructure/Persistence/RelationRepository.php - Namespace: Infrastructure\Persistence
- Zeilen: 147 | Größe: 4,055 Bytes
- Geändert: 2025-12-27 23:50:10 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 100
- Dependencies: 100 (25%)
- LOC: 100 (20%)
- Methods: 100 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Dependencies 5
- implements Domain\Repository\RelationRepositoryInterface
- constructor PDO
- use Domain\Constants
- use Domain\Repository\RelationRepositoryInterface
- use PDO
Klassen 1
-
RelationRepositoryclass Zeile 13
Funktionen 9
-
__construct()public Zeile 17 -
findFiltered()public Zeile 25 -
getTypes()public Zeile 53 -
getStats()public Zeile 66 -
find()public Zeile 81 -
create()public Zeile 101 -
update()public Zeile 120 -
delete()public Zeile 132 -
getTypesList()public Zeile 142
Verwendet von 1
Versionen 4
-
v4
2025-12-27 23:50 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v3
2025-12-27 23:49 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v2
2025-12-25 10:28 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v1
2025-12-25 10:28 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
Code
<?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 = Constants::DEFAULT_LIMIT): 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'];
}
}