Lines 100.00% 8 / 8
Methods 100.00% 2 / 2
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 translate 100.00% 7 / 7 100.00% 1 / 1 3
 doTranslate n/a 0 / 0 n/a 0 / 0 0
 doTranslateBatch n/a 0 / 0 n/a 0 / 0 0
 mapLanguageCode n/a 0 / 0 n/a 0 / 0 0
31abstract class AbstractTranslationProvider implements TranslationProviderInterface
32{
33    protected HtmlPreserver $htmlPreserver;
34
35    /**
36     * Constructor.
37     *
38     * @param Configuration $configuration phpMyFAQ configuration
39     * @param HttpClientInterface $httpClient HTTP client for API requests
40     */
41    public function __construct(
42        protected readonly Configuration $configuration,
43        protected readonly HttpClientInterface $httpClient,
44    ) {
45        $this->htmlPreserver = new HtmlPreserver();
46    }
47
48    /**
49     * Translate text with optional HTML preservation.
50     *
51     * @param string $text Text to translate
52     * @param string $sourceLang Source language code
53     * @param string $targetLang Target language code
54     * @param bool $preserveHtml Whether to preserve HTML tags
55     * @return string Translated text
56     * @throws TranslationException
57     */
58    public function translate(string $text, string $sourceLang, string $targetLang, bool $preserveHtml = false): string
59    {
60        if ($text === '') {
61            return '';
62        }
63
64        if ($preserveHtml) {
65            [$textWithPlaceholders, $htmlMap] = $this->htmlPreserver->replaceTags($text);
66            $translated = $this->doTranslate($textWithPlaceholders, $sourceLang, $targetLang);
67            return $this->htmlPreserver->restoreTags($translated, $htmlMap);
68        }
69
70        return $this->doTranslate($text, $sourceLang, $targetLang);
71    }
72
73    /**
74     * Provider-specific translation implementation.
75     *
76     * @param string $text Text to translate (without HTML placeholders if preserveHtml was true)
77     * @param string $sourceLang Source language code
78     * @param string $targetLang Target language code
79     * @return string Translated text
80     * @throws TranslationException
81     */
82    abstract protected function doTranslate(string $text, string $sourceLang, string $targetLang): string;
83
84    /**
85     * Provider-specific batch translation implementation.
86     *
87     * @param array<string> $texts Texts to translate
88     * @param string $sourceLang Source language code
89     * @param string $targetLang Target language code
90     * @return array<string> Translated texts
91     * @throws TranslationException
92     */
93    abstract protected function doTranslateBatch(array $texts, string $sourceLang, string $targetLang): array;
94
95    /**
96     * Map phpMyFAQ language codes to provider-specific codes.
97     *
98     * @param string $pmfLangCode phpMyFAQ language code (e.g., 'en', 'de', 'zh')
99     * @return string Provider-specific language code
100     */
101    abstract protected function mapLanguageCode(string $pmfLangCode): string;
102}