AssignChunkTaxonomyUseCase.php
- Pfad:
src/UseCases/Taxonomy/AssignChunkTaxonomyUseCase.php - Namespace: UseCases\Taxonomy
- Zeilen: 140 | Größe: 4,431 Bytes
- Geändert: 2025-12-25 20:13:42 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 95
- Dependencies: 80 (25%)
- LOC: 100 (20%)
- Methods: 100 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Dependencies 11
- constructor Domain\Repository\ChunkTaxonomyRepositoryInterface
- constructor Domain\Repository\ChunkExplorerRepositoryInterface
- constructor Domain\Repository\TaxonomyRepositoryInterface
- constructor Infrastructure\Audit\AuditService
- use Domain\Entity\ChunkTaxonomyMapping
- use Domain\Repository\ChunkExplorerRepositoryInterface
- use Domain\Repository\ChunkTaxonomyRepositoryInterface
- use Domain\Repository\TaxonomyRepositoryInterface
- use Domain\ValueObject\Confidence
- use Domain\ValueObject\MappingSource
- use Infrastructure\Audit\AuditService
Klassen 1
-
AssignChunkTaxonomyUseCaseclass Zeile 17
Funktionen 7
-
__construct()public Zeile 19 -
execute()public Zeile 32 -
batchAssign()Zeile 80 -
validateChunk()Zeile 105 -
validateTaxonomyTerm()Zeile 113 -
validateConfidence()Zeile 121 -
validateSource()Zeile 130
Versionen 1
-
v1
2025-12-25 20:13 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
Code
<?php
declare(strict_types=1);
namespace UseCases\Taxonomy;
// @responsibility: Manuelle und automatische Chunk-Taxonomie-Zuweisung
use Domain\Entity\ChunkTaxonomyMapping;
use Domain\Repository\ChunkExplorerRepositoryInterface;
use Domain\Repository\ChunkTaxonomyRepositoryInterface;
use Domain\Repository\TaxonomyRepositoryInterface;
use Domain\ValueObject\Confidence;
use Domain\ValueObject\MappingSource;
use Infrastructure\Audit\AuditService;
final class AssignChunkTaxonomyUseCase
{
public function __construct(
private ChunkTaxonomyRepositoryInterface $chunkTaxonomyRepository,
private ChunkExplorerRepositoryInterface $chunkRepository,
private TaxonomyRepositoryInterface $taxonomyRepository,
private AuditService $auditService
) {
}
/**
* Assign a single taxonomy term to a chunk.
*
* @throws \InvalidArgumentException if validation fails
*/
public function execute(
int $chunkId,
int $taxonomyTermId,
float $confidence,
string $source
): int {
$this->validateChunk($chunkId);
$this->validateTaxonomyTerm($taxonomyTermId);
$this->validateConfidence($confidence);
$this->validateSource($source);
// Check if mapping already exists
if ($this->chunkTaxonomyRepository->exists($chunkId, $taxonomyTermId)) {
throw new \InvalidArgumentException(
"Mapping already exists for chunk {$chunkId} and term {$taxonomyTermId}"
);
}
$mapping = new ChunkTaxonomyMapping();
$mapping->setChunkId($chunkId);
$mapping->setTaxonomyTermId($taxonomyTermId);
$mapping->setConfidence(Confidence::fromFloat($confidence));
$mapping->setSource(MappingSource::from($source));
$id = $this->chunkTaxonomyRepository->save($mapping);
$this->auditService->logCreate(
table: 'chunk_taxonomy',
id: $id,
data: [
'chunk_id' => $chunkId,
'taxonomy_term_id' => $taxonomyTermId,
'confidence' => $confidence,
'source' => $source,
],
actor: 'system',
actorType: $source === 'auto' ? 'pipeline' : 'user'
);
return $id;
}
/**
* Batch assign taxonomy terms to chunks.
*
* @param array<array{chunk_id: int, taxonomy_term_id: int, confidence: float, source: string}> $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(
chunkId: $assignment['chunk_id'],
taxonomyTermId: $assignment['taxonomy_term_id'],
confidence: $assignment['confidence'],
source: $assignment['source']
);
$successCount++;
} catch (\Exception $e) {
$errors[] = "Assignment {$index}: " . $e->getMessage();
}
}
return [
'success' => $successCount,
'errors' => $errors,
];
}
private function validateChunk(int $chunkId): void
{
$chunk = $this->chunkRepository->getChunk($chunkId);
if ($chunk === null) {
throw new \InvalidArgumentException("Chunk {$chunkId} 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 validateConfidence(float $confidence): void
{
if ($confidence < 0.0 || $confidence > 1.0) {
throw new \InvalidArgumentException(
"Confidence must be between 0.0 and 1.0, got {$confidence}"
);
}
}
private function validateSource(string $source): void
{
$validSources = ['auto', 'manual'];
if (!in_array($source, $validSources, true)) {
throw new \InvalidArgumentException(
"Invalid source '{$source}'. Must be one of: " . implode(', ', $validSources)
);
}
}
}