Total Complexity | 41 |
Total Lines | 297 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like LanguagePackService 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 LanguagePackService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class LanguagePackService implements LoggerAwareInterface |
||
44 | { |
||
45 | use LoggerAwareTrait; |
||
46 | |||
47 | /** |
||
48 | * @var Locales |
||
49 | */ |
||
50 | protected $locales; |
||
51 | |||
52 | /** |
||
53 | * @var Registry |
||
54 | */ |
||
55 | protected $registry; |
||
56 | |||
57 | /** |
||
58 | * @var EventDispatcherInterface |
||
59 | */ |
||
60 | protected $eventDispatcher; |
||
61 | |||
62 | /** |
||
63 | * @var RequestFactory |
||
64 | */ |
||
65 | protected $requestFactory; |
||
66 | |||
67 | private const LANGUAGE_PACK_URL = 'https://localize.typo3.org/xliff/'; |
||
68 | |||
69 | public function __construct(EventDispatcherInterface $eventDispatcher, RequestFactory $requestFactory) |
||
70 | { |
||
71 | $this->eventDispatcher = $eventDispatcher; |
||
72 | $this->locales = GeneralUtility::makeInstance(Locales::class); |
||
73 | $this->registry = GeneralUtility::makeInstance(Registry::class); |
||
74 | $this->requestFactory = $requestFactory; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Get list of available languages |
||
79 | * |
||
80 | * @return array iso=>name |
||
81 | */ |
||
82 | public function getAvailableLanguages(): array |
||
83 | { |
||
84 | return $this->locales->getLanguages(); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * List of languages active in this instance |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | public function getActiveLanguages(): array |
||
93 | { |
||
94 | $availableLanguages = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lang']['availableLanguages'] ?? []; |
||
95 | return array_filter($availableLanguages); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Create an array with language details: active or not, iso codes, last update, ... |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | public function getLanguageDetails(): array |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Create a list of loaded extensions and their language packs details |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | public function getExtensionLanguagePackDetails(): array |
||
137 | { |
||
138 | $activeLanguages = $this->getActiveLanguages(); |
||
139 | $packageManager = GeneralUtility::makeInstance(PackageManager::class); |
||
140 | $activePackages = $packageManager->getActivePackages(); |
||
141 | $extensions = []; |
||
142 | $activeExtensions = []; |
||
143 | foreach ($activePackages as $package) { |
||
144 | $path = $package->getPackagePath(); |
||
145 | $finder = new Finder(); |
||
146 | try { |
||
147 | $files = $finder->files()->in($path . 'Resources/Private/Language/')->name('*.xlf'); |
||
148 | if ($files->count() === 0) { |
||
149 | // This extension has no .xlf files |
||
150 | continue; |
||
151 | } |
||
152 | } catch (\InvalidArgumentException $e) { |
||
153 | // Dir does not exist |
||
154 | continue; |
||
155 | } |
||
156 | $key = $package->getPackageKey(); |
||
157 | $activeExtensions[] = $key; |
||
158 | $title = $package->getValueFromComposerManifest('description') ?? ''; |
||
159 | if (is_file($path . 'ext_emconf.php')) { |
||
160 | $_EXTKEY = $key; |
||
161 | $EM_CONF = []; |
||
162 | include $path . 'ext_emconf.php'; |
||
163 | $title = $EM_CONF[$key]['title'] ?? $title; |
||
164 | |||
165 | $state = $EM_CONF[$key]['state'] ?? ''; |
||
166 | if ($state === 'excludeFromUpdates') { |
||
167 | continue; |
||
168 | } |
||
169 | } |
||
170 | $extension = [ |
||
171 | 'key' => $key, |
||
172 | 'title' => $title, |
||
173 | ]; |
||
174 | if (!empty(ExtensionManagementUtility::getExtensionIcon($path, false))) { |
||
175 | $extension['icon'] = PathUtility::stripPathSitePrefix(ExtensionManagementUtility::getExtensionIcon($path, true)); |
||
176 | } |
||
177 | $extension['packs'] = []; |
||
178 | foreach ($activeLanguages as $iso) { |
||
179 | $isLanguagePackDownloaded = is_dir(Environment::getLabelsPath() . '/' . $iso . '/' . $key . '/'); |
||
180 | $lastUpdate = $this->registry->get('languagePacks', $iso . '-' . $key); |
||
181 | $extension['packs'][] = [ |
||
182 | 'iso' => $iso, |
||
183 | 'exists' => $isLanguagePackDownloaded, |
||
184 | 'lastUpdate' => $this->getFormattedDate($lastUpdate), |
||
185 | ]; |
||
186 | } |
||
187 | $extensions[] = $extension; |
||
188 | } |
||
189 | usort($extensions, function ($a, $b) { |
||
190 | // Sort extensions by key |
||
191 | if ($a['key'] === $b['key']) { |
||
192 | return 0; |
||
193 | } |
||
194 | return $a['key'] < $b['key'] ? -1 : 1; |
||
195 | }); |
||
196 | return $extensions; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Download and unpack a single language pack of one extension. |
||
201 | * |
||
202 | * @param string $key Extension key |
||
203 | * @param string $iso Language iso code |
||
204 | * @return string One of 'update', 'new' or 'failed' |
||
205 | * @throws \RuntimeException |
||
206 | */ |
||
207 | public function languagePackDownload(string $key, string $iso): string |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Set 'last update' timestamp in registry for a series of iso codes. |
||
292 | * |
||
293 | * @param string[] $isos List of iso code timestamps to set |
||
294 | * @throws \RuntimeException |
||
295 | */ |
||
296 | public function setLastUpdatedIsoCode(array $isos) |
||
297 | { |
||
298 | $activeLanguages = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lang']['availableLanguages'] ?? []; |
||
299 | foreach ($isos as $iso) { |
||
300 | if (!in_array($iso, $activeLanguages, true)) { |
||
301 | throw new \RuntimeException('Language iso code ' . (string)$iso . ' not available or active', 1520176318); |
||
302 | } |
||
303 | $this->registry->set('languagePacks', $iso, time()); |
||
304 | } |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Format a timestamp to a formatted date string |
||
309 | * |
||
310 | * @param int|null $timestamp |
||
311 | * @return string|null |
||
312 | */ |
||
313 | protected function getFormattedDate($timestamp) |
||
314 | { |
||
315 | if (is_int($timestamp)) { |
||
316 | $date = new \DateTime('@' . $timestamp); |
||
317 | $format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] . ' ' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm']; |
||
318 | $timestamp = $date->format($format); |
||
319 | } |
||
320 | return $timestamp; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Unzip a language zip file |
||
325 | * |
||
326 | * @param string $file path to zip file |
||
327 | * @param string $path path to extract to |
||
328 | */ |
||
329 | protected function unzipTranslationFile(string $file, string $path) |
||
340 | } |
||
341 | } |
||
342 |