Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
9 / 10 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| CommentAnalyzer | |
90.00% |
9 / 10 |
|
66.67% |
2 / 3 |
3.01 | |
0.00% |
0 / 1 |
| analyze | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getExpectedKeys | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getLoggerName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace App\Service\Api\Anthropic; |
| 5 | |
| 6 | /** |
| 7 | * CommentAnalyzer Class |
| 8 | * |
| 9 | * This class is responsible for analyzing comments using the Anthropic API service. |
| 10 | * It interacts with the AI prompts table to retrieve prompt data and uses the AnthropicApiService |
| 11 | * to send requests and parse responses for comment analysis. |
| 12 | */ |
| 13 | class CommentAnalyzer extends AbstractAnthropicGenerator |
| 14 | { |
| 15 | /** |
| 16 | * Analyzes a comment using the Anthropic API. |
| 17 | * |
| 18 | * This method retrieves the appropriate prompt data, creates a payload, |
| 19 | * sends a request to the Anthropic API, and processes the response to analyze the comment. |
| 20 | * |
| 21 | * @param string $comment The comment to be analyzed. |
| 22 | * @return array The analysis results from the API, containing various aspects of the comment analysis. |
| 23 | * @throws \InvalidArgumentException If the task prompt data is not found. |
| 24 | */ |
| 25 | public function analyze(string $comment): array |
| 26 | { |
| 27 | $promptData = $this->getPromptData('comment_analysis'); |
| 28 | $payload = $this->createPayload($promptData, $comment); |
| 29 | |
| 30 | $result = $this->sendApiRequest($payload); |
| 31 | |
| 32 | return $this->ensureExpectedKeys($result); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Gets the expected keys for the API response. |
| 37 | * |
| 38 | * @return array Array of expected response keys. |
| 39 | */ |
| 40 | protected function getExpectedKeys(): array |
| 41 | { |
| 42 | return [ |
| 43 | 'comment', |
| 44 | 'is_inappropriate', |
| 45 | 'reason', |
| 46 | ]; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Gets the logger name for this analyzer. |
| 51 | * |
| 52 | * @return string The logger name. |
| 53 | */ |
| 54 | protected function getLoggerName(): string |
| 55 | { |
| 56 | return 'Comment Analyzer'; |
| 57 | } |
| 58 | } |