Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
15 / 18
72.73% covered (warning)
72.73%
8 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
AiService
83.33% covered (warning)
83.33%
15 / 18
72.73% covered (warning)
72.73%
8 / 11
11.56
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 getProvider
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getProviderName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 generateTagSeo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 generateArticleSeo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 generateGallerySeo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 generateArticleTags
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 analyzeImage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 analyzeComment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 generateTextSummary
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 translateStrings
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare(strict_types=1);
3
4namespace App\Service\Api;
5
6use App\Model\Table\AipromptsTable;
7use App\Service\Api\Anthropic\ArticleTagsGenerator;
8use App\Service\Api\Anthropic\CommentAnalyzer;
9use App\Service\Api\Anthropic\ImageAnalyzer;
10use App\Service\Api\Anthropic\SeoContentGenerator;
11use App\Service\Api\Anthropic\TextSummaryGenerator;
12use App\Service\Api\Anthropic\TranslationGenerator;
13use Cake\ORM\TableRegistry;
14
15/**
16 * AiService Class
17 *
18 * Unified AI service that uses the configured provider (Anthropic or OpenRouter).
19 * This class provides the same API as AnthropicApiService but supports provider switching
20 * based on application settings.
21 *
22 * Jobs and other consumers should use this class to benefit from provider flexibility.
23 */
24class AiService
25{
26    /**
27     * @var \App\Service\Api\AiProviderInterface The AI provider instance.
28     */
29    private AiProviderInterface $provider;
30
31    /**
32     * @var \App\Model\Table\AipromptsTable The table instance for AI prompts.
33     */
34    private AipromptsTable $aipromptsTable;
35
36    /**
37     * @var \App\Service\Api\Anthropic\SeoContentGenerator The SEO content generator service.
38     */
39    private SeoContentGenerator $seoContentGenerator;
40
41    /**
42     * @var \App\Service\Api\Anthropic\ImageAnalyzer The image analyzer service.
43     */
44    private ImageAnalyzer $imageAnalyzer;
45
46    /**
47     * @var \App\Service\Api\Anthropic\CommentAnalyzer The comment analyzer service.
48     */
49    private CommentAnalyzer $commentAnalyzer;
50
51    /**
52     * @var \App\Service\Api\Anthropic\ArticleTagsGenerator The article tags generator service.
53     */
54    private ArticleTagsGenerator $articleTagsGenerator;
55
56    /**
57     * @var \App\Service\Api\Anthropic\TextSummaryGenerator The text summary generator service.
58     */
59    private TextSummaryGenerator $textSummaryGenerator;
60
61    /**
62     * @var \App\Service\Api\Anthropic\TranslationGenerator The translation generator service.
63     */
64    private TranslationGenerator $translationGenerator;
65
66    /**
67     * AiService constructor.
68     *
69     * Creates the service with the configured provider or an injected one for testing.
70     *
71     * @param \App\Service\Api\AiProviderInterface|null $provider Optional provider for testing.
72     */
73    public function __construct(?AiProviderInterface $provider = null)
74    {
75        $this->provider = $provider ?? AiServiceFactory::createProvider();
76        $this->aipromptsTable = TableRegistry::getTableLocator()->get('Aiprompts');
77
78        $this->seoContentGenerator = new SeoContentGenerator($this->provider, $this->aipromptsTable);
79        $this->imageAnalyzer = new ImageAnalyzer($this->provider, $this->aipromptsTable);
80        $this->commentAnalyzer = new CommentAnalyzer($this->provider, $this->aipromptsTable);
81        $this->articleTagsGenerator = new ArticleTagsGenerator($this->provider, $this->aipromptsTable);
82        $this->textSummaryGenerator = new TextSummaryGenerator($this->provider, $this->aipromptsTable);
83        $this->translationGenerator = new TranslationGenerator($this->provider, $this->aipromptsTable);
84    }
85
86    /**
87     * Gets the current provider instance.
88     *
89     * @return \App\Service\Api\AiProviderInterface The AI provider.
90     */
91    public function getProvider(): AiProviderInterface
92    {
93        return $this->provider;
94    }
95
96    /**
97     * Gets the provider name.
98     *
99     * @return string The provider identifier.
100     */
101    public function getProviderName(): string
102    {
103        return $this->provider->getProviderName();
104    }
105
106    /**
107     * Generates SEO content for a tag.
108     *
109     * @param string $tagTitle The title of the tag.
110     * @param string $tagDescription The description of the tag.
111     * @return array The generated SEO content.
112     */
113    public function generateTagSeo(string $tagTitle, string $tagDescription): array
114    {
115        return $this->seoContentGenerator->generateTagSeo($tagTitle, $tagDescription);
116    }
117
118    /**
119     * Generates SEO content for an article.
120     *
121     * @param string $title The title of the article.
122     * @param string $body The body content of the article.
123     * @return array The generated SEO content.
124     */
125    public function generateArticleSeo(string $title, string $body): array
126    {
127        return $this->seoContentGenerator->generateArticleSeo($title, $body);
128    }
129
130    /**
131     * Generates SEO content for an image gallery.
132     *
133     * @param string $name The name of the gallery.
134     * @param string $context Additional context about the gallery content and images.
135     * @return array The generated SEO content.
136     */
137    public function generateGallerySeo(string $name, string $context): array
138    {
139        return $this->seoContentGenerator->generateGallerySeo($name, $context);
140    }
141
142    /**
143     * Generates tags for an article.
144     *
145     * @param array $allTags All available tags.
146     * @param string $title The title of the article.
147     * @param string $body The body content of the article.
148     * @return array The generated article tags.
149     */
150    public function generateArticleTags(array $allTags, string $title, string $body): array
151    {
152        return $this->articleTagsGenerator->generateArticleTags($allTags, $title, $body);
153    }
154
155    /**
156     * Analyzes an image.
157     *
158     * @param string $imagePath The path to the image file.
159     * @return array The analysis results.
160     */
161    public function analyzeImage(string $imagePath): array
162    {
163        return $this->imageAnalyzer->analyze($imagePath);
164    }
165
166    /**
167     * Analyzes a comment.
168     *
169     * @param string $comment The comment text to analyze.
170     * @return array The analysis results.
171     */
172    public function analyzeComment(string $comment): array
173    {
174        return $this->commentAnalyzer->analyze($comment);
175    }
176
177    /**
178     * Generates a summary for a given text.
179     *
180     * @param string $context The context of the text (e.g., 'article', 'page', 'report').
181     * @param string $text The text to summarize.
182     * @return array The generated summary.
183     */
184    public function generateTextSummary(string $context, string $text): array
185    {
186        return $this->textSummaryGenerator->generateTextSummary($context, $text);
187    }
188
189    /**
190     * Translates an array of strings from one locale to another.
191     *
192     * @param array $strings The array of strings to be translated.
193     * @param string $localeFrom The locale code of the source language (e.g., 'en_US').
194     * @param string $localeTo The locale code of the target language (e.g., 'fr_FR').
195     * @return array The translated strings.
196     */
197    public function translateStrings(array $strings, string $localeFrom, string $localeTo): array
198    {
199        return $this->translationGenerator->generateTranslation($strings, $localeFrom, $localeTo);
200    }
201}