Lines 100.00% 33 / 33
Methods 100.00% 4 / 4
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 getLanguagesToTranslate 100.00% 8 / 8 100.00% 1 / 1 3
 getExistingTranslations 100.00% 8 / 8 100.00% 1 / 1 2
 getAllExistingTranslations 100.00% 10 / 10 100.00% 1 / 1 3
 getLanguagesInUse 100.00% 7 / 7 100.00% 1 / 1 2
27final class CategoryLanguageService
28{
29    /**
30     * Returns languages available to translate for a given category id.
31     * Key = language code (lowercase), Value = display name.
32     *
33     * @return array<string, string>
34     */
35    public function getLanguagesToTranslate(Configuration $configuration, int $categoryId): array
36    {
37        $existing = $configuration->getLanguage()->isLanguageAvailable($categoryId, table: 'faqcategories');
38        $existingLower = array_map(strtolower(...), $existing);
39
40        $result = [];
41        foreach (LanguageHelper::getAvailableLanguages() as $lang => $name) {
42            $langLower = strtolower((string) $lang);
43            if (!in_array($langLower, $existingLower, strict: true)) {
44                $result[$langLower] = $name;
45            }
46        }
47
48        return $result;
49    }
50
51    /**
52     * Returns existing translation languages for a given category id.
53     * Key = language code (lowercase), Value = display name.
54     *
55     * @return array<string, string>
56     */
57    public function getExistingTranslations(Configuration $configuration, int $categoryId): array
58    {
59        $existing = $configuration->getLanguage()->isLanguageAvailable($categoryId, table: 'faqcategories');
60        $available = LanguageHelper::getAvailableLanguages();
61
62        $result = [];
63        foreach ($existing as $code) {
64            $codeLower = strtolower($code);
65            $result[$codeLower] = $available[$codeLower] ?? LanguageCodes::get($codeLower) ?? $codeLower;
66        }
67
68        ksort($result);
69        return $result;
70    }
71
72    /**
73     * Returns the translated language codes for every category in one query.
74     *
75     * @return array<int, string[]> category id => language codes
76     */
77    public function getAllExistingTranslations(Configuration $configuration): array
78    {
79        $query = sprintf('SELECT id, lang FROM %sfaqcategories', Database::getTablePrefix());
80        $result = $configuration->getDb()->query($query);
81
82        $map = [];
83        while (true) {
84            $row = $configuration->getDb()->fetchObject($result);
85            if (!$row instanceof \stdClass) {
86                break;
87            }
88
89            $id = (int) $row->id;
90            $map[$id][] = strtolower((string) $row->lang);
91        }
92
93        return $map;
94    }
95
96    /**
97     * Returns all languages currently used by categories (distinct langs in faqcategories).
98     * Key = language code (lowercase), Value = display name.
99     *
100     * @return array<string, string>
101     */
102    public function getLanguagesInUse(Configuration $configuration): array
103    {
104        $all = $configuration->getLanguage()->isLanguageAvailable(identifier: 0, table: 'faqcategories');
105        $result = [];
106        foreach ($all as $code) {
107            $codeLower = strtolower($code);
108            $result[$codeLower] = LanguageCodes::get($codeLower) ?? $codeLower;
109        }
110
111        asort($result);
112        return $result;
113    }
114}