{
"event": "PreToolUse",
"tool_name": "Write",
"tool_input": {
"file_path": "\/var\/www\/dev.campus.systemische-tools.de\/src\/UseCases\/Taxonomy\/AssignChunkTaxonomyUseCase.php",
"content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace UseCases\\Taxonomy;\n\n\/\/ @responsibility: Manuelle und automatische Chunk-Taxonomie-Zuweisung\n\nuse Domain\\Entity\\ChunkTaxonomyMapping;\nuse Domain\\Repository\\ChunkExplorerRepositoryInterface;\nuse Domain\\Repository\\ChunkTaxonomyRepositoryInterface;\nuse Domain\\Repository\\TaxonomyRepositoryInterface;\nuse Domain\\ValueObject\\Confidence;\nuse Domain\\ValueObject\\MappingSource;\nuse Infrastructure\\Audit\\AuditService;\n\nfinal class AssignChunkTaxonomyUseCase\n{\n public function __construct(\n private ChunkTaxonomyRepositoryInterface $chunkTaxonomyRepository,\n private ChunkExplorerRepositoryInterface $chunkRepository,\n private TaxonomyRepositoryInterface $taxonomyRepository,\n private AuditService $auditService\n ) {\n }\n\n \/**\n * Assign a single taxonomy term to a chunk.\n *\n * @throws \\InvalidArgumentException if validation fails\n *\/\n public function execute(\n int $chunkId,\n int $taxonomyTermId,\n float $confidence,\n string $source\n ): int {\n $this->validateChunk($chunkId);\n $this->validateTaxonomyTerm($taxonomyTermId);\n $this->validateConfidence($confidence);\n $this->validateSource($source);\n\n \/\/ Check if mapping already exists\n if ($this->chunkTaxonomyRepository->exists($chunkId, $taxonomyTermId)) {\n throw new \\InvalidArgumentException(\n \"Mapping already exists for chunk {$chunkId} and term {$taxonomyTermId}\"\n );\n }\n\n $mapping = new ChunkTaxonomyMapping(\n chunkId: $chunkId,\n taxonomyTermId: $taxonomyTermId,\n confidence: new Confidence($confidence),\n source: MappingSource::from($source)\n );\n\n $id = $this->chunkTaxonomyRepository->save($mapping);\n\n $this->auditService->logCreate(\n table: 'chunk_taxonomy',\n id: $id,\n data: [\n 'chunk_id' => $chunkId,\n 'taxonomy_term_id' => $taxonomyTermId,\n 'confidence' => $confidence,\n 'source' => $source,\n ],\n actor: 'system',\n actorType: $source === 'auto' ? 'pipeline' : 'user'\n );\n\n return $id;\n }\n\n \/**\n * Batch assign taxonomy terms to chunks.\n *\n * @param array<array{chunk_id: int, taxonomy_term_id: int, confidence: float, source: string}> $assignments\n * @return array{success: int, errors: array<string>}\n *\/\n public function batchAssign(array $assignments): array\n {\n $successCount = 0;\n $errors = [];\n\n foreach ($assignments as $index => $assignment) {\n try {\n $this->execute(\n chunkId: $assignment['chunk_id'],\n taxonomyTermId: $assignment['taxonomy_term_id'],\n confidence: $assignment['confidence'],\n source: $assignment['source']\n );\n $successCount++;\n } catch (\\Exception $e) {\n $errors[] = \"Assignment {$index}: \" . $e->getMessage();\n }\n }\n\n return [\n 'success' => $successCount,\n 'errors' => $errors,\n ];\n }\n\n private function validateChunk(int $chunkId): void\n {\n $chunk = $this->chunkRepository->getChunk($chunkId);\n if ($chunk === null) {\n throw new \\InvalidArgumentException(\"Chunk {$chunkId} not found\");\n }\n }\n\n private function validateTaxonomyTerm(int $termId): void\n {\n $term = $this->taxonomyRepository->find($termId);\n if ($term === null) {\n throw new \\InvalidArgumentException(\"Taxonomy term {$termId} not found\");\n }\n }\n\n private function validateConfidence(float $confidence): void\n {\n if ($confidence < 0.0 || $confidence > 1.0) {\n throw new \\InvalidArgumentException(\n \"Confidence must be between 0.0 and 1.0, got {$confidence}\"\n );\n }\n }\n\n private function validateSource(string $source): void\n {\n $validSources = ['auto', 'manual'];\n if (!in_array($source, $validSources, true)) {\n throw new \\InvalidArgumentException(\n \"Invalid source '{$source}'. Must be one of: \" . implode(', ', $validSources)\n );\n }\n }\n}\n"
}
}
{
"tool_response": {
"type": "create",
"filePath": "\/var\/www\/dev.campus.systemische-tools.de\/src\/UseCases\/Taxonomy\/AssignChunkTaxonomyUseCase.php",
"content": "<?php\n\ndeclare(strict_types=1);\n\nnamespace UseCases\\Taxonomy;\n\n\/\/ @responsibility: Manuelle und automatische Chunk-Taxonomie-Zuweisung\n\nuse Domain\\Entity\\ChunkTaxonomyMapping;\nuse Domain\\Repository\\ChunkExplorerRepositoryInterface;\nuse Domain\\Repository\\ChunkTaxonomyRepositoryInterface;\nuse Domain\\Repository\\TaxonomyRepositoryInterface;\nuse Domain\\ValueObject\\Confidence;\nuse Domain\\ValueObject\\MappingSource;\nuse Infrastructure\\Audit\\AuditService;\n\nfinal class AssignChunkTaxonomyUseCase\n{\n public function __construct(\n private ChunkTaxonomyRepositoryInterface $chunkTaxonomyRepository,\n private ChunkExplorerRepositoryInterface $chunkRepository,\n private TaxonomyRepositoryInterface $taxonomyRepository,\n private AuditService $auditService\n ) {\n }\n\n \/**\n * Assign a single taxonomy term to a chunk.\n *\n * @throws \\InvalidArgumentException if validation fails\n *\/\n public function execute(\n int $chunkId,\n int $taxonomyTermId,\n float $confidence,\n string $source\n ): int {\n $this->validateChunk($chunkId);\n $this->validateTaxonomyTerm($taxonomyTermId);\n $this->validateConfidence($confidence);\n $this->validateSource($source);\n\n \/\/ Check if mapping already exists\n if ($this->chunkTaxonomyRepository->exists($chunkId, $taxonomyTermId)) {\n throw new \\InvalidArgumentException(\n \"Mapping already exists for chunk {$chunkId} and term {$taxonomyTermId}\"\n );\n }\n\n $mapping = new ChunkTaxonomyMapping(\n chunkId: $chunkId,\n taxonomyTermId: $taxonomyTermId,\n confidence: new Confidence($confidence),\n source: MappingSource::from($source)\n );\n\n $id = $this->chunkTaxonomyRepository->save($mapping);\n\n $this->auditService->logCreate(\n table: 'chunk_taxonomy',\n id: $id,\n data: [\n 'chunk_id' => $chunkId,\n 'taxonomy_term_id' => $taxonomyTermId,\n 'confidence' => $confidence,\n 'source' => $source,\n ],\n actor: 'system',\n actorType: $source === 'auto' ? 'pipeline' : 'user'\n );\n\n return $id;\n }\n\n \/**\n * Batch assign taxonomy terms to chunks.\n *\n * @param array<array{chunk_id: int, taxonomy_term_id: int, confidence: float, source: string}> $assignments\n * @return array{success: int, errors: array<string>}\n *\/\n public function batchAssign(array $assignments): array\n {\n $successCount = 0;\n $errors = [];\n\n foreach ($assignments as $index => $assignment) {\n try {\n $this->execute(\n chunkId: $assignment['chunk_id'],\n taxonomyTermId: $assignment['taxonomy_term_id'],\n confidence: $assignment['confidence'],\n source: $assignment['source']\n );\n $successCount++;\n } catch (\\Exception $e) {\n $errors[] = \"Assignment {$index}: \" . $e->getMessage();\n }\n }\n\n return [\n 'success' => $successCount,\n 'errors' => $errors,\n ];\n }\n\n private function validateChunk(int $chunkId): void\n {\n $chunk = $this->chunkRepository->getChunk($chunkId);\n if ($chunk === null) {\n throw new \\InvalidArgumentException(\"Chunk {$chunkId} not found\");\n }\n }\n\n private function validateTaxonomyTerm(int $termId): void\n {\n $term = $this->taxonomyRepository->find($termId);\n if ($term === null) {\n throw new \\InvalidArgumentException(\"Taxonomy term {$termId} not found\");\n }\n }\n\n private function validateConfidence(float $confidence): void\n {\n if ($confidence < 0.0 || $confidence > 1.0) {\n throw new \\InvalidArgumentException(\n \"Confidence must be between 0.0 and 1.0, got {$confidence}\"\n );\n }\n }\n\n private function validateSource(string $source): void\n {\n $validSources = ['auto', 'manual'];\n if (!in_array($source, $validSources, true)) {\n throw new \\InvalidArgumentException(\n \"Invalid source '{$source}'. Must be one of: \" . implode(', ', $validSources)\n );\n }\n }\n}\n",
"structuredPatch": [],
"originalFile": null
}
}