Total Complexity | 48 |
Total Lines | 245 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like TranslationsProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TranslationsProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | final class TranslationsProvider implements TranslationsProviderInterface |
||
19 | { |
||
20 | private array $bundles; |
||
21 | |||
22 | private TranslationDomainsProviderInterface $translationDomainsProvider; |
||
23 | |||
24 | private LocalesProviderInterface $localesProvider; |
||
25 | |||
26 | private FileLocator $fileLocator; |
||
27 | |||
28 | private TranslationFileNameProviderInterface $translationFileNameProvider; |
||
29 | |||
30 | private DefaultTranslationDirectoryProviderInterface $defaultTranslationDirectoryProvider; |
||
31 | |||
32 | private ThemesProviderInterface $themesProvider; |
||
33 | |||
34 | public function __construct( |
||
50 | } |
||
51 | |||
52 | public function getTranslations(string $defaultLocaleCode, array $locales): array |
||
53 | { |
||
54 | $bundleTranslations = []; |
||
55 | foreach ($this->bundles as $bundleName => $bundle) { |
||
56 | $bundleTranslations = array_replace_recursive( |
||
57 | $bundleTranslations, |
||
58 | $this->getBundleTranslations($bundleName, $defaultLocaleCode, $locales) |
||
59 | ); |
||
60 | } |
||
61 | $appTranslations = $this->getDirectoryTranslations( |
||
62 | $this->defaultTranslationDirectoryProvider->getDefaultDirectory(), |
||
63 | $defaultLocaleCode, |
||
64 | $locales |
||
65 | ); |
||
66 | |||
67 | $themes = $this->themesProvider->getAll(); |
||
68 | $translations = []; |
||
69 | foreach ($themes as $theme) { |
||
70 | if (!empty($theme->getPath())) { |
||
71 | $translationDirectory = $theme->getPath() . '/translations/'; |
||
72 | } else { |
||
73 | $translationDirectory = $this->defaultTranslationDirectoryProvider->getDefaultDirectory(); |
||
74 | } |
||
75 | $themeTranslations = $this->getDirectoryTranslations($translationDirectory, $defaultLocaleCode, $locales); |
||
76 | $themeTranslations = $this->removeEmptyKeys($themeTranslations); |
||
77 | |||
78 | $mergedTranslations = array_replace_recursive($bundleTranslations, $appTranslations, $themeTranslations); |
||
79 | $mergedTranslations = $this->fillMissingKeys($mergedTranslations, $locales); |
||
80 | $translations[$theme->getName()] = $mergedTranslations; |
||
81 | } |
||
82 | $translations = $this->fillMissingKeys($translations, $locales); |
||
83 | |||
84 | ArrayUtils::recursiveKsort($translations); |
||
85 | |||
86 | return $translations; |
||
87 | } |
||
88 | |||
89 | public function getBundleTranslations(string $bundleName, string $localeCode, array $locales): array |
||
90 | { |
||
91 | if (!$this->doesBundleHaveTranslations($bundleName)) { |
||
92 | return []; |
||
93 | } |
||
94 | $translationsDirectory = $this->getBundleTranslationsDirectory($bundleName); |
||
95 | |||
96 | return $this->getDirectoryTranslations($translationsDirectory, $localeCode, $locales); |
||
|
|||
97 | } |
||
98 | |||
99 | public function getDirectoryTranslations(string $directory, string $localeCode, array $locales): array |
||
100 | { |
||
101 | $domains = $this->translationDomainsProvider->toArray($directory); |
||
102 | $defaultLocales = $this->localesProvider->getLocalesFromCode($localeCode); |
||
103 | |||
104 | $directoryTranslations = []; |
||
105 | foreach ($domains as $domain) { |
||
106 | foreach ($defaultLocales as $defaultLocale) { |
||
107 | $translations = $this->getYamlTranslations($directory, $domain, $defaultLocale); |
||
108 | $translations = array_replace_recursive($translations, $this->getXmlTranslations($directory, $domain, $defaultLocale)); |
||
109 | |||
110 | if (!array_key_exists($domain, $directoryTranslations)) { |
||
111 | $directoryTranslations[$domain] = []; |
||
112 | } |
||
113 | $translations = ArrayUtils::arrayFlatten($translations); |
||
114 | foreach ($translations as $key => $value) { |
||
115 | $translations[$key] = [$localeCode => $value]; |
||
116 | } |
||
117 | |||
118 | $directoryTranslations[$domain] = array_replace_recursive($directoryTranslations[$domain], $translations); |
||
119 | } |
||
120 | |||
121 | foreach ($locales as $locale) { |
||
122 | $availableLocales = $this->localesProvider->getLocalesFromCode($locale); |
||
123 | foreach ($availableLocales as $availableLocale) { |
||
124 | $translations = $this->getYamlTranslations($directory, $domain, $availableLocale); |
||
125 | $translations = array_replace_recursive($translations, $this->getXmlTranslations($directory, $domain, $availableLocale)); |
||
126 | |||
127 | if (!array_key_exists($domain, $directoryTranslations)) { |
||
128 | continue; |
||
129 | } |
||
130 | $translations = ArrayUtils::arrayFlatten($translations); |
||
131 | foreach ($translations as $key => $value) { |
||
132 | $directoryTranslations[$domain][$key][$locale] = $value; |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | |||
138 | return $directoryTranslations; |
||
139 | } |
||
140 | |||
141 | public function doesBundleHaveTranslations(string $bundleName): bool |
||
142 | { |
||
143 | try { |
||
144 | $this->fileLocator->locate(sprintf('@%s/Resources/translations/', $bundleName)); |
||
145 | |||
146 | return true; |
||
147 | } catch (InvalidArgumentException $exception) { |
||
148 | return false; |
||
149 | } |
||
150 | } |
||
151 | |||
152 | public function getBundleTranslationsDirectory(string $bundleName): ?string |
||
153 | { |
||
154 | if (!$this->doesBundleHaveTranslations($bundleName)) { |
||
155 | return null; |
||
156 | } |
||
157 | |||
158 | return $this->fileLocator->locate(sprintf('@%s/Resources/translations/', $bundleName)); |
||
159 | } |
||
160 | |||
161 | public function getYamlTranslations(string $directory, string $domain, string $locale): array |
||
162 | { |
||
163 | $translations = []; |
||
164 | |||
165 | $formats = ['yml', 'yaml']; |
||
166 | foreach ($formats as $format) { |
||
167 | $fileName = $this->translationFileNameProvider->getFromValues($directory, $domain, $locale, $format); |
||
168 | $translations = array_replace_recursive($translations, $this->getTranslationFileContent($fileName)); |
||
169 | } |
||
170 | |||
171 | return $translations; |
||
172 | } |
||
173 | |||
174 | public function getTranslationFileContent(string $filePath, string $type = self::TYPE_YAML): array |
||
175 | { |
||
176 | if (!file_exists($filePath)) { |
||
177 | return []; |
||
178 | } |
||
179 | |||
180 | switch ($type) { |
||
181 | case self::TYPE_XML: |
||
182 | throw new LogicException('This has not been implemented yet'); |
||
183 | case self::TYPE_YAML: |
||
184 | default: |
||
185 | return Yaml::parse(file_get_contents($filePath)); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | public function getXmlTranslations(string $directory, string $domain, string $locale): array |
||
200 | } |
||
201 | |||
202 | private function fillMissingKeys(array $translations, array $locales): array |
||
217 | } |
||
218 | |||
219 | public function removeEmptyKeys(array $translations): array |
||
230 | } |
||
231 | |||
232 | public function defineAllKeys(array $translations, array $locales): array |
||
233 | { |
||
263 | } |
||
264 | } |
||
265 |