Total Complexity | 74 |
Total Lines | 505 |
Duplicated Lines | 0 % |
Changes | 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 |
||
59 | class AppSettingsController extends Controller { |
||
60 | |||
61 | /** @var \OCP\IL10N */ |
||
62 | private $l10n; |
||
63 | /** @var IConfig */ |
||
64 | private $config; |
||
65 | /** @var INavigationManager */ |
||
66 | private $navigationManager; |
||
67 | /** @var IAppManager */ |
||
68 | private $appManager; |
||
69 | /** @var CategoryFetcher */ |
||
70 | private $categoryFetcher; |
||
71 | /** @var AppFetcher */ |
||
72 | private $appFetcher; |
||
73 | /** @var IFactory */ |
||
74 | private $l10nFactory; |
||
75 | /** @var BundleFetcher */ |
||
76 | private $bundleFetcher; |
||
77 | /** @var Installer */ |
||
78 | private $installer; |
||
79 | /** @var IURLGenerator */ |
||
80 | private $urlGenerator; |
||
81 | /** @var ILogger */ |
||
82 | private $logger; |
||
83 | |||
84 | /** @var array */ |
||
85 | private $allApps = []; |
||
86 | |||
87 | /** |
||
88 | * @param string $appName |
||
89 | * @param IRequest $request |
||
90 | * @param IL10N $l10n |
||
91 | * @param IConfig $config |
||
92 | * @param INavigationManager $navigationManager |
||
93 | * @param IAppManager $appManager |
||
94 | * @param CategoryFetcher $categoryFetcher |
||
95 | * @param AppFetcher $appFetcher |
||
96 | * @param IFactory $l10nFactory |
||
97 | * @param BundleFetcher $bundleFetcher |
||
98 | * @param Installer $installer |
||
99 | * @param IURLGenerator $urlGenerator |
||
100 | * @param ILogger $logger |
||
101 | */ |
||
102 | public function __construct(string $appName, |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @NoCSRFRequired |
||
131 | * |
||
132 | * @return TemplateResponse |
||
133 | */ |
||
134 | public function viewApps(): TemplateResponse { |
||
149 | } |
||
150 | |||
151 | private function getAppsWithUpdates() { |
||
152 | $appClass = new \OC_App(); |
||
153 | $apps = $appClass->listAllApps(); |
||
154 | foreach($apps as $key => $app) { |
||
155 | $newVersion = $this->installer->isUpdateAvailable($app['id']); |
||
156 | if($newVersion === false) { |
||
157 | unset($apps[$key]); |
||
158 | } |
||
159 | } |
||
160 | return $apps; |
||
161 | } |
||
162 | |||
163 | private function getBundles() { |
||
164 | $result = []; |
||
165 | $bundles = $this->bundleFetcher->getBundles(); |
||
166 | foreach ($bundles as $bundle) { |
||
167 | $result[] = [ |
||
168 | 'name' => $bundle->getName(), |
||
169 | 'id' => $bundle->getIdentifier(), |
||
170 | 'appIdentifiers' => $bundle->getAppIdentifiers() |
||
171 | ]; |
||
172 | } |
||
173 | return $result; |
||
174 | |||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Get all available categories |
||
179 | * |
||
180 | * @return JSONResponse |
||
181 | */ |
||
182 | public function listCategories(): JSONResponse { |
||
183 | return new JSONResponse($this->getAllCategories()); |
||
184 | } |
||
185 | |||
186 | private function getAllCategories() { |
||
200 | } |
||
201 | |||
202 | private function fetchApps() { |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | |||
234 | private function getAllApps() { |
||
236 | } |
||
237 | /** |
||
238 | * Get all available apps in a category |
||
239 | * |
||
240 | * @param string $category |
||
241 | * @return JSONResponse |
||
242 | * @throws \Exception |
||
243 | */ |
||
244 | public function listApps(): JSONResponse { |
||
245 | |||
246 | $this->fetchApps(); |
||
247 | $apps = $this->getAllApps(); |
||
248 | |||
249 | $dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n); |
||
250 | |||
251 | // Extend existing app details |
||
252 | $apps = array_map(function($appData) use ($dependencyAnalyzer) { |
||
253 | if (isset($appData['appstoreData'])) { |
||
254 | $appstoreData = $appData['appstoreData']; |
||
255 | $appData['screenshot'] = isset($appstoreData['screenshots'][0]['url']) ? 'https://usercontent.apps.nextcloud.com/' . base64_encode($appstoreData['screenshots'][0]['url']) : ''; |
||
256 | $appData['category'] = $appstoreData['categories']; |
||
257 | } |
||
258 | |||
259 | $newVersion = $this->installer->isUpdateAvailable($appData['id']); |
||
260 | if($newVersion) { |
||
261 | $appData['update'] = $newVersion; |
||
262 | } |
||
263 | |||
264 | // fix groups to be an array |
||
265 | $groups = array(); |
||
266 | if (is_string($appData['groups'])) { |
||
267 | $groups = json_decode($appData['groups']); |
||
268 | } |
||
269 | $appData['groups'] = $groups; |
||
270 | $appData['canUnInstall'] = !$appData['active'] && $appData['removable']; |
||
271 | |||
272 | // fix licence vs license |
||
273 | if (isset($appData['license']) && !isset($appData['licence'])) { |
||
274 | $appData['licence'] = $appData['license']; |
||
275 | } |
||
276 | |||
277 | $ignoreMaxApps = $this->config->getSystemValue('app_install_overwrite', []); |
||
278 | $ignoreMax = in_array($appData['id'], $ignoreMaxApps); |
||
279 | |||
280 | // analyse dependencies |
||
281 | $missing = $dependencyAnalyzer->analyze($appData, $ignoreMax); |
||
282 | $appData['canInstall'] = empty($missing); |
||
283 | $appData['missingDependencies'] = $missing; |
||
284 | |||
285 | $appData['missingMinOwnCloudVersion'] = !isset($appData['dependencies']['nextcloud']['@attributes']['min-version']); |
||
286 | $appData['missingMaxOwnCloudVersion'] = !isset($appData['dependencies']['nextcloud']['@attributes']['max-version']); |
||
287 | $appData['isCompatible'] = $dependencyAnalyzer->isMarkedCompatible($appData); |
||
288 | |||
289 | return $appData; |
||
290 | }, $apps); |
||
291 | |||
292 | usort($apps, [$this, 'sortApps']); |
||
293 | |||
294 | return new JSONResponse(['apps' => $apps, 'status' => 'success']); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Get all apps for a category from the app store |
||
299 | * |
||
300 | * @param string $requestedCategory |
||
301 | * @return array |
||
302 | * @throws \Exception |
||
303 | */ |
||
304 | private function getAppsForCategory($requestedCategory = ''): array { |
||
305 | $versionParser = new VersionParser(); |
||
306 | $formattedApps = []; |
||
307 | $apps = $this->appFetcher->get(); |
||
308 | foreach($apps as $app) { |
||
309 | // Skip all apps not in the requested category |
||
310 | if ($requestedCategory !== '') { |
||
311 | $isInCategory = false; |
||
312 | foreach($app['categories'] as $category) { |
||
313 | if($category === $requestedCategory) { |
||
314 | $isInCategory = true; |
||
315 | } |
||
316 | } |
||
317 | if(!$isInCategory) { |
||
318 | continue; |
||
319 | } |
||
320 | } |
||
321 | |||
322 | if (!isset($app['releases'][0]['rawPlatformVersionSpec'])) { |
||
323 | continue; |
||
324 | } |
||
325 | $nextCloudVersion = $versionParser->getVersion($app['releases'][0]['rawPlatformVersionSpec']); |
||
326 | $nextCloudVersionDependencies = []; |
||
327 | if($nextCloudVersion->getMinimumVersion() !== '') { |
||
328 | $nextCloudVersionDependencies['nextcloud']['@attributes']['min-version'] = $nextCloudVersion->getMinimumVersion(); |
||
329 | } |
||
330 | if($nextCloudVersion->getMaximumVersion() !== '') { |
||
331 | $nextCloudVersionDependencies['nextcloud']['@attributes']['max-version'] = $nextCloudVersion->getMaximumVersion(); |
||
332 | } |
||
333 | $phpVersion = $versionParser->getVersion($app['releases'][0]['rawPhpVersionSpec']); |
||
334 | $existsLocally = \OC_App::getAppPath($app['id']) !== false; |
||
335 | $phpDependencies = []; |
||
336 | if($phpVersion->getMinimumVersion() !== '') { |
||
337 | $phpDependencies['php']['@attributes']['min-version'] = $phpVersion->getMinimumVersion(); |
||
338 | } |
||
339 | if($phpVersion->getMaximumVersion() !== '') { |
||
340 | $phpDependencies['php']['@attributes']['max-version'] = $phpVersion->getMaximumVersion(); |
||
341 | } |
||
342 | if(isset($app['releases'][0]['minIntSize'])) { |
||
343 | $phpDependencies['php']['@attributes']['min-int-size'] = $app['releases'][0]['minIntSize']; |
||
344 | } |
||
345 | $authors = ''; |
||
346 | foreach($app['authors'] as $key => $author) { |
||
347 | $authors .= $author['name']; |
||
348 | if($key !== count($app['authors']) - 1) { |
||
349 | $authors .= ', '; |
||
350 | } |
||
351 | } |
||
352 | |||
353 | $currentLanguage = substr(\OC::$server->getL10NFactory()->findLanguage(), 0, 2); |
||
354 | $enabledValue = $this->config->getAppValue($app['id'], 'enabled', 'no'); |
||
355 | $groups = null; |
||
356 | if($enabledValue !== 'no' && $enabledValue !== 'yes') { |
||
357 | $groups = $enabledValue; |
||
358 | } |
||
359 | |||
360 | $currentVersion = ''; |
||
361 | if($this->appManager->isInstalled($app['id'])) { |
||
362 | $currentVersion = $this->appManager->getAppVersion($app['id']); |
||
363 | } else { |
||
364 | $currentLanguage = $app['releases'][0]['version']; |
||
365 | } |
||
366 | |||
367 | $formattedApps[] = [ |
||
368 | 'id' => $app['id'], |
||
369 | 'name' => isset($app['translations'][$currentLanguage]['name']) ? $app['translations'][$currentLanguage]['name'] : $app['translations']['en']['name'], |
||
370 | 'description' => isset($app['translations'][$currentLanguage]['description']) ? $app['translations'][$currentLanguage]['description'] : $app['translations']['en']['description'], |
||
371 | 'summary' => isset($app['translations'][$currentLanguage]['summary']) ? $app['translations'][$currentLanguage]['summary'] : $app['translations']['en']['summary'], |
||
372 | 'license' => $app['releases'][0]['licenses'], |
||
373 | 'author' => $authors, |
||
374 | 'shipped' => false, |
||
375 | 'version' => $currentVersion, |
||
376 | 'default_enable' => '', |
||
377 | 'types' => [], |
||
378 | 'documentation' => [ |
||
379 | 'admin' => $app['adminDocs'], |
||
380 | 'user' => $app['userDocs'], |
||
381 | 'developer' => $app['developerDocs'] |
||
382 | ], |
||
383 | 'website' => $app['website'], |
||
384 | 'bugs' => $app['issueTracker'], |
||
385 | 'detailpage' => $app['website'], |
||
386 | 'dependencies' => array_merge( |
||
387 | $nextCloudVersionDependencies, |
||
388 | $phpDependencies |
||
389 | ), |
||
390 | 'level' => ($app['isFeatured'] === true) ? 200 : 100, |
||
391 | 'missingMaxOwnCloudVersion' => false, |
||
392 | 'missingMinOwnCloudVersion' => false, |
||
393 | 'canInstall' => true, |
||
394 | 'screenshot' => isset($app['screenshots'][0]['url']) ? 'https://usercontent.apps.nextcloud.com/'.base64_encode($app['screenshots'][0]['url']) : '', |
||
395 | 'score' => $app['ratingOverall'], |
||
396 | 'ratingNumOverall' => $app['ratingNumOverall'], |
||
397 | 'ratingNumThresholdReached' => $app['ratingNumOverall'] > 5, |
||
398 | 'removable' => $existsLocally, |
||
399 | 'active' => $this->appManager->isEnabledForUser($app['id']), |
||
400 | 'needsDownload' => !$existsLocally, |
||
401 | 'groups' => $groups, |
||
402 | 'fromAppStore' => true, |
||
403 | 'appstoreData' => $app, |
||
404 | ]; |
||
405 | } |
||
406 | |||
407 | return $formattedApps; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * @PasswordConfirmationRequired |
||
412 | * |
||
413 | * @param string $appId |
||
414 | * @param array $groups |
||
415 | * @return JSONResponse |
||
416 | */ |
||
417 | public function enableApp(string $appId, array $groups = []): JSONResponse { |
||
418 | return $this->enableApps([$appId], $groups); |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * Enable one or more apps |
||
423 | * |
||
424 | * apps will be enabled for specific groups only if $groups is defined |
||
425 | * |
||
426 | * @PasswordConfirmationRequired |
||
427 | * @param array $appIds |
||
428 | * @param array $groups |
||
429 | * @return JSONResponse |
||
430 | */ |
||
431 | public function enableApps(array $appIds, array $groups = []): JSONResponse { |
||
432 | try { |
||
433 | $updateRequired = false; |
||
434 | |||
435 | foreach ($appIds as $appId) { |
||
436 | $appId = OC_App::cleanAppId($appId); |
||
437 | |||
438 | // Check if app is already downloaded |
||
439 | /** @var Installer $installer */ |
||
440 | $installer = \OC::$server->query(Installer::class); |
||
441 | $isDownloaded = $installer->isDownloaded($appId); |
||
442 | |||
443 | if(!$isDownloaded) { |
||
444 | $installer->downloadApp($appId); |
||
445 | } |
||
446 | |||
447 | $installer->installApp($appId); |
||
448 | |||
449 | if (count($groups) > 0) { |
||
450 | $this->appManager->enableAppForGroups($appId, $this->getGroupList($groups)); |
||
451 | } else { |
||
452 | $this->appManager->enableApp($appId); |
||
453 | } |
||
454 | if (\OC_App::shouldUpgrade($appId)) { |
||
455 | $updateRequired = true; |
||
456 | } |
||
457 | } |
||
458 | return new JSONResponse(['data' => ['update_required' => $updateRequired]]); |
||
459 | |||
460 | } catch (\Exception $e) { |
||
461 | $this->logger->logException($e); |
||
462 | return new JSONResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_INTERNAL_SERVER_ERROR); |
||
463 | } |
||
464 | } |
||
465 | |||
466 | private function getGroupList(array $groups) { |
||
467 | $groupManager = \OC::$server->getGroupManager(); |
||
468 | $groupsList = []; |
||
469 | foreach ($groups as $group) { |
||
470 | $groupItem = $groupManager->get($group); |
||
471 | if ($groupItem instanceof \OCP\IGroup) { |
||
472 | $groupsList[] = $groupManager->get($group); |
||
473 | } |
||
474 | } |
||
475 | return $groupsList; |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * @PasswordConfirmationRequired |
||
480 | * |
||
481 | * @param string $appId |
||
482 | * @return JSONResponse |
||
483 | */ |
||
484 | public function disableApp(string $appId): JSONResponse { |
||
485 | return $this->disableApps([$appId]); |
||
486 | } |
||
487 | |||
488 | /** |
||
489 | * @PasswordConfirmationRequired |
||
490 | * |
||
491 | * @param array $appIds |
||
492 | * @return JSONResponse |
||
493 | */ |
||
494 | public function disableApps(array $appIds): JSONResponse { |
||
495 | try { |
||
496 | foreach ($appIds as $appId) { |
||
497 | $appId = OC_App::cleanAppId($appId); |
||
498 | $this->appManager->disableApp($appId); |
||
499 | } |
||
500 | return new JSONResponse([]); |
||
501 | } catch (\Exception $e) { |
||
502 | $this->logger->logException($e); |
||
503 | return new JSONResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_INTERNAL_SERVER_ERROR); |
||
504 | } |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * @PasswordConfirmationRequired |
||
509 | * |
||
510 | * @param string $appId |
||
511 | * @return JSONResponse |
||
512 | */ |
||
513 | public function uninstallApp(string $appId): JSONResponse { |
||
521 | } |
||
522 | |||
523 | /** |
||
524 | * @param string $appId |
||
525 | * @return JSONResponse |
||
526 | */ |
||
527 | public function updateApp(string $appId): JSONResponse { |
||
528 | $appId = OC_App::cleanAppId($appId); |
||
529 | |||
530 | $this->config->setSystemValue('maintenance', true); |
||
531 | try { |
||
532 | $result = $this->installer->updateAppstoreApp($appId); |
||
533 | $this->config->setSystemValue('maintenance', false); |
||
534 | } catch (\Exception $ex) { |
||
535 | $this->config->setSystemValue('maintenance', false); |
||
536 | return new JSONResponse(['data' => ['message' => $ex->getMessage()]], Http::STATUS_INTERNAL_SERVER_ERROR); |
||
537 | } |
||
538 | |||
539 | if ($result !== false) { |
||
540 | return new JSONResponse(['data' => ['appid' => $appId]]); |
||
541 | } |
||
542 | return new JSONResponse(['data' => ['message' => $this->l10n->t('Couldn\'t update app.')]], Http::STATUS_INTERNAL_SERVER_ERROR); |
||
543 | } |
||
544 | |||
545 | private function sortApps($a, $b) { |
||
552 | } |
||
553 | |||
554 | public function force(string $appId): JSONResponse { |
||
555 | $appId = OC_App::cleanAppId($appId); |
||
564 | } |
||
565 | |||
566 | } |
||
567 |