Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.33% |
35 / 42 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| SeoContentGenerator | |
83.33% |
35 / 42 |
|
80.00% |
4 / 5 |
5.12 | |
0.00% |
0 / 1 |
| generateTagSeo | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
| generateArticleSeo | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| generateGallerySeo | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| getExpectedKeys | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| getLoggerName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace App\Service\Api\Anthropic; |
| 5 | |
| 6 | /** |
| 7 | * SeoContentGenerator Class |
| 8 | * |
| 9 | * This class is responsible for generating SEO content for tags and articles |
| 10 | * using the Anthropic API service. It interacts with the AI prompts table to retrieve |
| 11 | * prompt data and uses the AnthropicApiService to send requests and parse responses. |
| 12 | */ |
| 13 | class SeoContentGenerator extends AbstractAnthropicGenerator |
| 14 | { |
| 15 | /** |
| 16 | * Generates SEO content for a tag. |
| 17 | * |
| 18 | * This method performs the following steps: |
| 19 | * 1. Retrieves the appropriate prompt data for tag SEO analysis. |
| 20 | * 2. Creates a payload with the tag title and description. |
| 21 | * 3. Sends a request to the Anthropic API and processes the response. |
| 22 | * 4. Ensures all expected SEO keys are present in the result. |
| 23 | * |
| 24 | * @param string $tagTitle The title of the tag. |
| 25 | * @param string $tagDescription The description of the tag. |
| 26 | * @return array The generated SEO content, including meta tags and social media descriptions. |
| 27 | * @throws \InvalidArgumentException If the task prompt data is not found. |
| 28 | */ |
| 29 | public function generateTagSeo(string $tagTitle, string $tagDescription): array |
| 30 | { |
| 31 | $promptData = $this->getPromptData('tag_seo_analysis'); |
| 32 | $payload = $this->createPayload($promptData, [ |
| 33 | 'tag_title' => $tagTitle, |
| 34 | 'tag_description' => $tagDescription, |
| 35 | ]); |
| 36 | |
| 37 | $result = $this->sendApiRequest($payload); |
| 38 | |
| 39 | $defaultKeys = [ |
| 40 | 'meta_title', |
| 41 | 'meta_description', |
| 42 | 'meta_keywords', |
| 43 | 'facebook_description', |
| 44 | 'linkedin_description', |
| 45 | 'twitter_description', |
| 46 | 'instagram_description', |
| 47 | 'description', |
| 48 | ]; |
| 49 | |
| 50 | return $this->ensureExpectedKeys($result, $defaultKeys); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Generates SEO content for an article. |
| 55 | * |
| 56 | * This method performs the following steps: |
| 57 | * 1. Strips HTML tags and decodes entities from the article body. |
| 58 | * 2. Retrieves the appropriate prompt data for article SEO analysis. |
| 59 | * 3. Creates a payload with the article title and plain text content. |
| 60 | * 4. Sends a request to the Anthropic API and processes the response. |
| 61 | * 5. Ensures all expected SEO keys are present in the result. |
| 62 | * |
| 63 | * @param string $title The title of the article. |
| 64 | * @param string $body The body content of the article (may contain HTML). |
| 65 | * @return array The generated SEO content, including meta tags and social media descriptions. |
| 66 | * @throws \InvalidArgumentException If the task prompt data is not found. |
| 67 | */ |
| 68 | public function generateArticleSeo(string $title, string $body): array |
| 69 | { |
| 70 | $plainTextContent = strip_tags(html_entity_decode($body)); |
| 71 | $promptData = $this->getPromptData('article_seo_analysis'); |
| 72 | $payload = $this->createPayload($promptData, [ |
| 73 | 'article_title' => $title, |
| 74 | 'article_content' => $plainTextContent, |
| 75 | ]); |
| 76 | |
| 77 | $result = $this->sendApiRequest($payload); |
| 78 | |
| 79 | return $this->ensureExpectedKeys($result); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Generates SEO content for an image gallery. |
| 84 | * |
| 85 | * This method performs the following steps: |
| 86 | * 1. Retrieves the appropriate prompt data for gallery SEO analysis. |
| 87 | * 2. Creates a payload with the gallery name and context information. |
| 88 | * 3. Sends a request to the Anthropic API and processes the response. |
| 89 | * 4. Ensures all expected SEO keys are present in the result. |
| 90 | * |
| 91 | * @param string $name The name of the gallery. |
| 92 | * @param string $context Additional context about the gallery content and images. |
| 93 | * @return array The generated SEO content, including meta tags and social media descriptions. |
| 94 | * @throws \InvalidArgumentException If the task prompt data is not found. |
| 95 | */ |
| 96 | public function generateGallerySeo(string $name, string $context): array |
| 97 | { |
| 98 | $promptData = $this->getPromptData('gallery_seo_analysis'); |
| 99 | $payload = $this->createPayload($promptData, [ |
| 100 | 'gallery_name' => $name, |
| 101 | 'gallery_context' => $context, |
| 102 | ]); |
| 103 | |
| 104 | $result = $this->sendApiRequest($payload); |
| 105 | |
| 106 | return $this->ensureExpectedKeys($result); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Gets the expected keys for the API response. |
| 111 | * |
| 112 | * @return array Array of expected response keys. |
| 113 | */ |
| 114 | protected function getExpectedKeys(): array |
| 115 | { |
| 116 | return [ |
| 117 | 'meta_title', |
| 118 | 'meta_description', |
| 119 | 'meta_keywords', |
| 120 | 'facebook_description', |
| 121 | 'linkedin_description', |
| 122 | 'twitter_description', |
| 123 | 'instagram_description', |
| 124 | ]; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Gets the logger name for this generator. |
| 129 | * |
| 130 | * @return string The logger name. |
| 131 | */ |
| 132 | protected function getLoggerName(): string |
| 133 | { |
| 134 | return 'SEO Content Generator'; |
| 135 | } |
| 136 | } |