Total Complexity | 71 |
Total Lines | 505 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like AppSettingsController 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 AppSettingsController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
56 | class AppSettingsController extends Controller { |
||
57 | |||
58 | /** @var \OCP\IL10N */ |
||
59 | private $l10n; |
||
60 | /** @var IConfig */ |
||
61 | private $config; |
||
62 | /** @var INavigationManager */ |
||
63 | private $navigationManager; |
||
64 | /** @var IAppManager */ |
||
65 | private $appManager; |
||
66 | /** @var CategoryFetcher */ |
||
67 | private $categoryFetcher; |
||
68 | /** @var AppFetcher */ |
||
69 | private $appFetcher; |
||
70 | /** @var IFactory */ |
||
71 | private $l10nFactory; |
||
72 | /** @var BundleFetcher */ |
||
73 | private $bundleFetcher; |
||
74 | /** @var Installer */ |
||
75 | private $installer; |
||
76 | /** @var IURLGenerator */ |
||
77 | private $urlGenerator; |
||
78 | /** @var LoggerInterface */ |
||
79 | private $logger; |
||
80 | |||
81 | /** @var array */ |
||
82 | private $allApps = []; |
||
83 | |||
84 | /** |
||
85 | * @param string $appName |
||
86 | * @param IRequest $request |
||
87 | * @param IL10N $l10n |
||
88 | * @param IConfig $config |
||
89 | * @param INavigationManager $navigationManager |
||
90 | * @param IAppManager $appManager |
||
91 | * @param CategoryFetcher $categoryFetcher |
||
92 | * @param AppFetcher $appFetcher |
||
93 | * @param IFactory $l10nFactory |
||
94 | * @param BundleFetcher $bundleFetcher |
||
95 | * @param Installer $installer |
||
96 | * @param IURLGenerator $urlGenerator |
||
97 | * @param LoggerInterface $logger |
||
98 | */ |
||
99 | public function __construct(string $appName, |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @NoCSRFRequired |
||
128 | * |
||
129 | * @return TemplateResponse |
||
130 | */ |
||
131 | public function viewApps(): TemplateResponse { |
||
132 | $params = []; |
||
133 | $params['appstoreEnabled'] = $this->config->getSystemValueBool('appstoreenabled', true); |
||
134 | $params['updateCount'] = count($this->getAppsWithUpdates()); |
||
135 | $params['developerDocumentation'] = $this->urlGenerator->linkToDocs('developer-manual'); |
||
136 | $params['bundles'] = $this->getBundles(); |
||
137 | $this->navigationManager->setActiveEntry('core_apps'); |
||
138 | |||
139 | $templateResponse = new TemplateResponse('settings', 'settings-vue', ['serverData' => $params, 'pageTitle' => $this->l10n->t('Apps')]); |
||
140 | $policy = new ContentSecurityPolicy(); |
||
141 | $policy->addAllowedImageDomain('https://usercontent.apps.nextcloud.com'); |
||
142 | $templateResponse->setContentSecurityPolicy($policy); |
||
143 | |||
144 | return $templateResponse; |
||
145 | } |
||
146 | |||
147 | private function getAppsWithUpdates() { |
||
148 | $appClass = new \OC_App(); |
||
149 | $apps = $appClass->listAllApps(); |
||
150 | foreach ($apps as $key => $app) { |
||
151 | $newVersion = $this->installer->isUpdateAvailable($app['id']); |
||
152 | if ($newVersion === false) { |
||
153 | unset($apps[$key]); |
||
154 | } |
||
155 | } |
||
156 | return $apps; |
||
157 | } |
||
158 | |||
159 | private function getBundles() { |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Get all available categories |
||
174 | * |
||
175 | * @return JSONResponse |
||
176 | */ |
||
177 | public function listCategories(): JSONResponse { |
||
178 | return new JSONResponse($this->getAllCategories()); |
||
179 | } |
||
180 | |||
181 | private function getAllCategories() { |
||
182 | $currentLanguage = substr($this->l10nFactory->findLanguage(), 0, 2); |
||
183 | |||
184 | $formattedCategories = []; |
||
185 | $categories = $this->categoryFetcher->get(); |
||
186 | foreach ($categories as $category) { |
||
187 | $formattedCategories[] = [ |
||
188 | 'id' => $category['id'], |
||
189 | 'ident' => $category['id'], |
||
190 | 'displayName' => $category['translations'][$currentLanguage]['name'] ?? $category['translations']['en']['name'], |
||
191 | ]; |
||
192 | } |
||
193 | |||
194 | return $formattedCategories; |
||
195 | } |
||
196 | |||
197 | private function fetchApps() { |
||
198 | $appClass = new \OC_App(); |
||
199 | $apps = $appClass->listAllApps(); |
||
200 | foreach ($apps as $app) { |
||
201 | $app['installed'] = true; |
||
202 | $this->allApps[$app['id']] = $app; |
||
203 | } |
||
204 | |||
205 | $apps = $this->getAppsForCategory(''); |
||
206 | $supportedApps = $appClass->getSupportedApps(); |
||
207 | foreach ($apps as $app) { |
||
208 | $app['appstore'] = true; |
||
209 | if (!array_key_exists($app['id'], $this->allApps)) { |
||
210 | $this->allApps[$app['id']] = $app; |
||
211 | } else { |
||
212 | $this->allApps[$app['id']] = array_merge($app, $this->allApps[$app['id']]); |
||
213 | } |
||
214 | |||
215 | if (in_array($app['id'], $supportedApps)) { |
||
216 | $this->allApps[$app['id']]['level'] = \OC_App::supportedApp; |
||
217 | } |
||
218 | } |
||
219 | |||
220 | // add bundle information |
||
221 | $bundles = $this->bundleFetcher->getBundles(); |
||
222 | foreach ($bundles as $bundle) { |
||
223 | foreach ($bundle->getAppIdentifiers() as $identifier) { |
||
224 | foreach ($this->allApps as &$app) { |
||
225 | if ($app['id'] === $identifier) { |
||
226 | $app['bundleIds'][] = $bundle->getIdentifier(); |
||
227 | continue; |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | |||
234 | private function getAllApps() { |
||
235 | return $this->allApps; |
||
236 | } |
||
237 | /** |
||
238 | * Get all available apps in a category |
||
239 | * |
||
240 | * @return JSONResponse |
||
241 | * @throws \Exception |
||
242 | */ |
||
243 | public function listApps(): JSONResponse { |
||
244 | $this->fetchApps(); |
||
245 | $apps = $this->getAllApps(); |
||
246 | |||
247 | $dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n); |
||
248 | |||
249 | $ignoreMaxApps = $this->config->getSystemValue('app_install_overwrite', []); |
||
250 | if (!is_array($ignoreMaxApps)) { |
||
251 | $this->logger->warning('The value given for app_install_overwrite is not an array. Ignoring...'); |
||
252 | $ignoreMaxApps = []; |
||
253 | } |
||
254 | |||
255 | // Extend existing app details |
||
256 | $apps = array_map(function (array $appData) use ($dependencyAnalyzer, $ignoreMaxApps) { |
||
257 | if (isset($appData['appstoreData'])) { |
||
258 | $appstoreData = $appData['appstoreData']; |
||
259 | $appData['screenshot'] = isset($appstoreData['screenshots'][0]['url']) ? 'https://usercontent.apps.nextcloud.com/' . base64_encode($appstoreData['screenshots'][0]['url']) : ''; |
||
260 | $appData['category'] = $appstoreData['categories']; |
||
261 | $appData['releases'] = $appstoreData['releases']; |
||
262 | } |
||
263 | |||
264 | $newVersion = $this->installer->isUpdateAvailable($appData['id']); |
||
265 | if ($newVersion) { |
||
266 | $appData['update'] = $newVersion; |
||
267 | } |
||
268 | |||
269 | // fix groups to be an array |
||
270 | $groups = []; |
||
271 | if (is_string($appData['groups'])) { |
||
272 | $groups = json_decode($appData['groups']); |
||
273 | } |
||
274 | $appData['groups'] = $groups; |
||
275 | $appData['canUnInstall'] = !$appData['active'] && $appData['removable']; |
||
276 | |||
277 | // fix licence vs license |
||
278 | if (isset($appData['license']) && !isset($appData['licence'])) { |
||
279 | $appData['licence'] = $appData['license']; |
||
280 | } |
||
281 | |||
282 | $ignoreMax = in_array($appData['id'], $ignoreMaxApps); |
||
283 | |||
284 | // analyse dependencies |
||
285 | $missing = $dependencyAnalyzer->analyze($appData, $ignoreMax); |
||
286 | $appData['canInstall'] = empty($missing); |
||
287 | $appData['missingDependencies'] = $missing; |
||
288 | |||
289 | $appData['missingMinOwnCloudVersion'] = !isset($appData['dependencies']['nextcloud']['@attributes']['min-version']); |
||
290 | $appData['missingMaxOwnCloudVersion'] = !isset($appData['dependencies']['nextcloud']['@attributes']['max-version']); |
||
291 | $appData['isCompatible'] = $dependencyAnalyzer->isMarkedCompatible($appData); |
||
292 | |||
293 | return $appData; |
||
294 | }, $apps); |
||
295 | |||
296 | usort($apps, [$this, 'sortApps']); |
||
297 | |||
298 | return new JSONResponse(['apps' => $apps, 'status' => 'success']); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Get all apps for a category from the app store |
||
303 | * |
||
304 | * @param string $requestedCategory |
||
305 | * @return array |
||
306 | * @throws \Exception |
||
307 | */ |
||
308 | private function getAppsForCategory($requestedCategory = ''): array { |
||
309 | $versionParser = new VersionParser(); |
||
310 | $formattedApps = []; |
||
311 | $apps = $this->appFetcher->get(); |
||
312 | foreach ($apps as $app) { |
||
313 | // Skip all apps not in the requested category |
||
314 | if ($requestedCategory !== '') { |
||
315 | $isInCategory = false; |
||
316 | foreach ($app['categories'] as $category) { |
||
317 | if ($category === $requestedCategory) { |
||
318 | $isInCategory = true; |
||
319 | } |
||
320 | } |
||
321 | if (!$isInCategory) { |
||
322 | continue; |
||
323 | } |
||
324 | } |
||
325 | |||
326 | if (!isset($app['releases'][0]['rawPlatformVersionSpec'])) { |
||
327 | continue; |
||
328 | } |
||
329 | $nextCloudVersion = $versionParser->getVersion($app['releases'][0]['rawPlatformVersionSpec']); |
||
330 | $nextCloudVersionDependencies = []; |
||
331 | if ($nextCloudVersion->getMinimumVersion() !== '') { |
||
332 | $nextCloudVersionDependencies['nextcloud']['@attributes']['min-version'] = $nextCloudVersion->getMinimumVersion(); |
||
333 | } |
||
334 | if ($nextCloudVersion->getMaximumVersion() !== '') { |
||
335 | $nextCloudVersionDependencies['nextcloud']['@attributes']['max-version'] = $nextCloudVersion->getMaximumVersion(); |
||
336 | } |
||
337 | $phpVersion = $versionParser->getVersion($app['releases'][0]['rawPhpVersionSpec']); |
||
338 | $existsLocally = \OC_App::getAppPath($app['id']) !== false; |
||
339 | $phpDependencies = []; |
||
340 | if ($phpVersion->getMinimumVersion() !== '') { |
||
341 | $phpDependencies['php']['@attributes']['min-version'] = $phpVersion->getMinimumVersion(); |
||
342 | } |
||
343 | if ($phpVersion->getMaximumVersion() !== '') { |
||
344 | $phpDependencies['php']['@attributes']['max-version'] = $phpVersion->getMaximumVersion(); |
||
345 | } |
||
346 | if (isset($app['releases'][0]['minIntSize'])) { |
||
347 | $phpDependencies['php']['@attributes']['min-int-size'] = $app['releases'][0]['minIntSize']; |
||
348 | } |
||
349 | $authors = ''; |
||
350 | foreach ($app['authors'] as $key => $author) { |
||
351 | $authors .= $author['name']; |
||
352 | if ($key !== count($app['authors']) - 1) { |
||
353 | $authors .= ', '; |
||
354 | } |
||
355 | } |
||
356 | |||
357 | $currentLanguage = substr(\OC::$server->getL10NFactory()->findLanguage(), 0, 2); |
||
358 | $enabledValue = $this->config->getAppValue($app['id'], 'enabled', 'no'); |
||
359 | $groups = null; |
||
360 | if ($enabledValue !== 'no' && $enabledValue !== 'yes') { |
||
361 | $groups = $enabledValue; |
||
362 | } |
||
363 | |||
364 | $currentVersion = ''; |
||
365 | if ($this->appManager->isInstalled($app['id'])) { |
||
366 | $currentVersion = $this->appManager->getAppVersion($app['id']); |
||
367 | } else { |
||
368 | $currentLanguage = $app['releases'][0]['version']; |
||
369 | } |
||
370 | |||
371 | $formattedApps[] = [ |
||
372 | 'id' => $app['id'], |
||
373 | 'name' => $app['translations'][$currentLanguage]['name'] ?? $app['translations']['en']['name'], |
||
374 | 'description' => $app['translations'][$currentLanguage]['description'] ?? $app['translations']['en']['description'], |
||
375 | 'summary' => $app['translations'][$currentLanguage]['summary'] ?? $app['translations']['en']['summary'], |
||
376 | 'license' => $app['releases'][0]['licenses'], |
||
377 | 'author' => $authors, |
||
378 | 'shipped' => false, |
||
379 | 'version' => $currentVersion, |
||
380 | 'default_enable' => '', |
||
381 | 'types' => [], |
||
382 | 'documentation' => [ |
||
383 | 'admin' => $app['adminDocs'], |
||
384 | 'user' => $app['userDocs'], |
||
385 | 'developer' => $app['developerDocs'] |
||
386 | ], |
||
387 | 'website' => $app['website'], |
||
388 | 'bugs' => $app['issueTracker'], |
||
389 | 'detailpage' => $app['website'], |
||
390 | 'dependencies' => array_merge( |
||
391 | $nextCloudVersionDependencies, |
||
392 | $phpDependencies |
||
393 | ), |
||
394 | 'level' => ($app['isFeatured'] === true) ? 200 : 100, |
||
395 | 'missingMaxOwnCloudVersion' => false, |
||
396 | 'missingMinOwnCloudVersion' => false, |
||
397 | 'canInstall' => true, |
||
398 | 'screenshot' => isset($app['screenshots'][0]['url']) ? 'https://usercontent.apps.nextcloud.com/'.base64_encode($app['screenshots'][0]['url']) : '', |
||
399 | 'score' => $app['ratingOverall'], |
||
400 | 'ratingNumOverall' => $app['ratingNumOverall'], |
||
401 | 'ratingNumThresholdReached' => $app['ratingNumOverall'] > 5, |
||
402 | 'removable' => $existsLocally, |
||
403 | 'active' => $this->appManager->isEnabledForUser($app['id']), |
||
404 | 'needsDownload' => !$existsLocally, |
||
405 | 'groups' => $groups, |
||
406 | 'fromAppStore' => true, |
||
407 | 'appstoreData' => $app, |
||
408 | ]; |
||
409 | } |
||
410 | |||
411 | return $formattedApps; |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * @PasswordConfirmationRequired |
||
416 | * |
||
417 | * @param string $appId |
||
418 | * @param array $groups |
||
419 | * @return JSONResponse |
||
420 | */ |
||
421 | public function enableApp(string $appId, array $groups = []): JSONResponse { |
||
422 | return $this->enableApps([$appId], $groups); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Enable one or more apps |
||
427 | * |
||
428 | * apps will be enabled for specific groups only if $groups is defined |
||
429 | * |
||
430 | * @PasswordConfirmationRequired |
||
431 | * @param array $appIds |
||
432 | * @param array $groups |
||
433 | * @return JSONResponse |
||
434 | */ |
||
435 | public function enableApps(array $appIds, array $groups = []): JSONResponse { |
||
436 | try { |
||
437 | $updateRequired = false; |
||
438 | |||
439 | foreach ($appIds as $appId) { |
||
440 | $appId = OC_App::cleanAppId($appId); |
||
441 | |||
442 | // Check if app is already downloaded |
||
443 | /** @var Installer $installer */ |
||
444 | $installer = \OC::$server->query(Installer::class); |
||
445 | $isDownloaded = $installer->isDownloaded($appId); |
||
446 | |||
447 | if (!$isDownloaded) { |
||
448 | $installer->downloadApp($appId); |
||
449 | } |
||
450 | |||
451 | $installer->installApp($appId); |
||
452 | |||
453 | if (count($groups) > 0) { |
||
454 | $this->appManager->enableAppForGroups($appId, $this->getGroupList($groups)); |
||
455 | } else { |
||
456 | $this->appManager->enableApp($appId); |
||
457 | } |
||
458 | if (\OC_App::shouldUpgrade($appId)) { |
||
459 | $updateRequired = true; |
||
460 | } |
||
461 | } |
||
462 | return new JSONResponse(['data' => ['update_required' => $updateRequired]]); |
||
463 | } catch (\Exception $e) { |
||
464 | $this->logger->error('could not enable apps', ['exception' => $e]); |
||
465 | return new JSONResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_INTERNAL_SERVER_ERROR); |
||
466 | } |
||
467 | } |
||
468 | |||
469 | private function getGroupList(array $groups) { |
||
470 | $groupManager = \OC::$server->getGroupManager(); |
||
471 | $groupsList = []; |
||
472 | foreach ($groups as $group) { |
||
473 | $groupItem = $groupManager->get($group); |
||
474 | if ($groupItem instanceof \OCP\IGroup) { |
||
475 | $groupsList[] = $groupManager->get($group); |
||
476 | } |
||
477 | } |
||
478 | return $groupsList; |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * @PasswordConfirmationRequired |
||
483 | * |
||
484 | * @param string $appId |
||
485 | * @return JSONResponse |
||
486 | */ |
||
487 | public function disableApp(string $appId): JSONResponse { |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * @PasswordConfirmationRequired |
||
493 | * |
||
494 | * @param array $appIds |
||
495 | * @return JSONResponse |
||
496 | */ |
||
497 | public function disableApps(array $appIds): JSONResponse { |
||
498 | try { |
||
499 | foreach ($appIds as $appId) { |
||
500 | $appId = OC_App::cleanAppId($appId); |
||
501 | $this->appManager->disableApp($appId); |
||
502 | } |
||
503 | return new JSONResponse([]); |
||
504 | } catch (\Exception $e) { |
||
505 | $this->logger->error('could not disable app', ['exception' => $e]); |
||
506 | return new JSONResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_INTERNAL_SERVER_ERROR); |
||
507 | } |
||
508 | } |
||
509 | |||
510 | /** |
||
511 | * @PasswordConfirmationRequired |
||
512 | * |
||
513 | * @param string $appId |
||
514 | * @return JSONResponse |
||
515 | */ |
||
516 | public function uninstallApp(string $appId): JSONResponse { |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * @param string $appId |
||
528 | * @return JSONResponse |
||
529 | */ |
||
530 | public function updateApp(string $appId): JSONResponse { |
||
546 | } |
||
547 | |||
548 | private function sortApps($a, $b) { |
||
549 | $a = (string)$a['name']; |
||
550 | $b = (string)$b['name']; |
||
551 | if ($a === $b) { |
||
552 | return 0; |
||
553 | } |
||
555 | } |
||
556 | |||
557 | public function force(string $appId): JSONResponse { |
||
561 | } |
||
562 | } |
||
563 |