Backup #1847
| ID | 1847 |
| Dateipfad | /var/www/dev.campus.systemische-tools.de/src/UseCases/Taxonomy/AssignEntityTaxonomyUseCase.php |
| Version | 2 |
| Typ |
modified |
| Größe | 5.2 KB |
| Hash | 5ea1296162e7ac217c23f403c676e78179f0c448f2162675345a9752aefdccc0 |
| Datum | 2025-12-27 23:22:46 |
| Geändert von | claude-code-hook |
| Grund | Claude Code Pre-Hook Backup vor Edit-Operation |
| Datei existiert |
Ja
|
Dateiinhalt
<?php
declare(strict_types=1);
namespace UseCases\Taxonomy;
// @responsibility: Manuelle und automatische Entity-Taxonomie-Zuweisung
use Domain\Constants;
use Domain\Entity\EntityTaxonomyMapping;
use Domain\Repository\EntityRepositoryInterface;
use Domain\Repository\EntityTaxonomyRepositoryInterface;
use Domain\Repository\TaxonomyRepositoryInterface;
use Domain\ValueObject\Confidence;
use Infrastructure\Audit\AuditService;
final class AssignEntityTaxonomyUseCase
{
public function __construct(
private EntityTaxonomyRepositoryInterface $entityTaxonomyRepository,
private EntityRepositoryInterface $entityRepository,
private TaxonomyRepositoryInterface $taxonomyRepository,
private AuditService $auditService
) {
}
/**
* Assign a taxonomy term to an entity.
*
* @throws \InvalidArgumentException if validation fails
*/
public function execute(
int $entityId,
int $taxonomyTermId,
float $relevance
): int {
$this->validateEntity($entityId);
$this->validateTaxonomyTerm($taxonomyTermId);
$this->validateRelevance($relevance);
// Check if mapping already exists
if ($this->entityTaxonomyRepository->exists($entityId, $taxonomyTermId)) {
throw new \InvalidArgumentException(
"Mapping already exists for entity {$entityId} and term {$taxonomyTermId}"
);
}
$mapping = new EntityTaxonomyMapping();
$mapping->setEntityId($entityId);
$mapping->setTaxonomyTermId($taxonomyTermId);
$mapping->setRelevance(Confidence::fromFloat($relevance));
$mapping->setValidated(false);
$id = $this->entityTaxonomyRepository->save($mapping);
$this->auditService->logCreate(
table: 'entity_taxonomy_mapping',
id: $id,
data: [
'entity_id' => $entityId,
'taxonomy_term_id' => $taxonomyTermId,
'relevance' => $relevance,
'validated' => false,
],
actor: 'system',
actorType: 'pipeline'
);
return $id;
}
/**
* Validate an existing mapping (mark as human-verified).
*
* @throws \InvalidArgumentException if mapping not found
*/
public function validateMapping(int $mappingId): void
{
$mappings = $this->entityTaxonomyRepository->getUnvalidatedMappings(1000);
$found = null;
foreach ($mappings as $mapping) {
if ($mapping->getId() === $mappingId) {
$found = $mapping;
break;
}
}
if ($found === null) {
// Check if mapping exists at all
$result = $this->entityTaxonomyRepository->markAsValidated($mappingId);
if (!$result) {
throw new \InvalidArgumentException("Mapping {$mappingId} not found");
}
} else {
$this->entityTaxonomyRepository->markAsValidated($mappingId);
}
$this->auditService->logUpdate(
table: 'entity_taxonomy_mapping',
id: $mappingId,
oldData: ['validated' => false],
newData: ['validated' => true],
actor: 'user',
actorType: 'user'
);
}
/**
* Batch assign taxonomy terms to entities.
*
* @param array<array{entity_id: int, taxonomy_term_id: int, relevance: float}> $assignments
* @return array{success: int, errors: array<string>}
*/
public function batchAssign(array $assignments): array
{
$successCount = 0;
$errors = [];
foreach ($assignments as $index => $assignment) {
try {
$this->execute(
entityId: $assignment['entity_id'],
taxonomyTermId: $assignment['taxonomy_term_id'],
relevance: $assignment['relevance']
);
$successCount++;
} catch (\Exception $e) {
$errors[] = "Assignment {$index}: " . $e->getMessage();
}
}
return [
'success' => $successCount,
'errors' => $errors,
];
}
/**
* Get validation statistics.
*
* @return array{total: int, validated: int, pending: int}
*/
public function getValidationStats(): array
{
return $this->entityTaxonomyRepository->getValidationStats();
}
private function validateEntity(int $entityId): void
{
$entity = $this->entityRepository->find($entityId);
if ($entity === null) {
throw new \InvalidArgumentException("Entity {$entityId} not found");
}
}
private function validateTaxonomyTerm(int $termId): void
{
$term = $this->taxonomyRepository->find($termId);
if ($term === null) {
throw new \InvalidArgumentException("Taxonomy term {$termId} not found");
}
}
private function validateRelevance(float $relevance): void
{
if ($relevance < 0.0 || $relevance > 1.0) {
throw new \InvalidArgumentException(
"Relevance must be between 0.0 and 1.0, got {$relevance}"
);
}
}
}
Vollständig herunterladen
Aktionen
Andere Versionen dieser Datei
| ID |
Version |
Typ |
Größe |
Datum |
| 1847 |
2 |
modified |
5.2 KB |
2025-12-27 23:22 |
| 1845 |
1 |
modified |
5.2 KB |
2025-12-27 23:22 |
← Zurück zur Übersicht