Simplify backend code

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2018-05-29 17:52:50 +02:00
parent a646389cff
commit bf7cef10be
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
1 changed files with 27 additions and 59 deletions

View File

@ -131,17 +131,11 @@ class AppSettingsController extends Controller {
/** /**
* @NoCSRFRequired * @NoCSRFRequired
* *
* @param string $category
* @return TemplateResponse * @return TemplateResponse
*/ */
public function viewApps($category = '') { public function viewApps(): TemplateResponse {
if ($category === '') {
$category = 'installed';
}
\OC_Util::addVendorScript('core', 'marked/marked.min'); \OC_Util::addVendorScript('core', 'marked/marked.min');
$params = []; $params = [];
$params['category'] = $category;
$params['appstoreEnabled'] = $this->config->getSystemValue('appstoreenabled', true) === true; $params['appstoreEnabled'] = $this->config->getSystemValue('appstoreenabled', true) === true;
$params['urlGenerator'] = $this->urlGenerator; $params['urlGenerator'] = $this->urlGenerator;
$params['updateCount'] = count($this->getAppsWithUpdates()); $params['updateCount'] = count($this->getAppsWithUpdates());
@ -154,7 +148,6 @@ class AppSettingsController extends Controller {
$templateResponse->setContentSecurityPolicy($policy); $templateResponse->setContentSecurityPolicy($policy);
return $templateResponse; return $templateResponse;
} }
private function getAllCategories() { private function getAllCategories() {
@ -178,7 +171,7 @@ class AppSettingsController extends Controller {
* *
* @return JSONResponse * @return JSONResponse
*/ */
public function listCategories() { public function listCategories(): JSONResponse {
return new JSONResponse($this->getAllCategories()); return new JSONResponse($this->getAllCategories());
} }
@ -187,8 +180,9 @@ class AppSettingsController extends Controller {
* *
* @param string $requestedCategory * @param string $requestedCategory
* @return array * @return array
* @throws \Exception
*/ */
private function getAppsForCategory($requestedCategory = '') { private function getAppsForCategory($requestedCategory = ''): array {
$versionParser = new VersionParser(); $versionParser = new VersionParser();
$formattedApps = []; $formattedApps = [];
$apps = $this->appFetcher->get(); $apps = $this->appFetcher->get();
@ -247,7 +241,7 @@ class AppSettingsController extends Controller {
$currentVersion = ''; $currentVersion = '';
if($this->appManager->isInstalled($app['id'])) { if($this->appManager->isInstalled($app['id'])) {
$currentVersion = \OC_App::getAppVersion($app['id']); $currentVersion = $this->appManager->getAppVersion($app['id']);
} else { } else {
$currentLanguage = $app['releases'][0]['version']; $currentLanguage = $app['releases'][0]['version'];
} }
@ -315,15 +309,17 @@ class AppSettingsController extends Controller {
unset($apps[$key]); unset($apps[$key]);
} }
} }
usort($apps, function ($a, $b) { usort($apps, [$this, 'sortApps']);
return $apps;
}
private function sortApps($a, $b) {
$a = (string)$a['name']; $a = (string)$a['name'];
$b = (string)$b['name']; $b = (string)$b['name'];
if ($a === $b) { if ($a === $b) {
return 0; return 0;
} }
return ($a < $b) ? -1 : 1; return ($a < $b) ? -1 : 1;
});
return $apps;
} }
/** /**
@ -331,6 +327,7 @@ class AppSettingsController extends Controller {
* *
* @param string $category * @param string $category
* @return JSONResponse * @return JSONResponse
* @throws \Exception
*/ */
public function listApps($category = '') { public function listApps($category = '') {
$appClass = new \OC_App(); $appClass = new \OC_App();
@ -345,14 +342,7 @@ class AppSettingsController extends Controller {
$apps[$key]['update'] = $newVersion; $apps[$key]['update'] = $newVersion;
} }
usort($apps, function ($a, $b) { usort($apps, [$this, 'sortApps']);
$a = (string)$a['name'];
$b = (string)$b['name'];
if ($a === $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
});
break; break;
// updates // updates
case 'updates': case 'updates':
@ -370,14 +360,7 @@ class AppSettingsController extends Controller {
$apps[$key]['update'] = $newVersion; $apps[$key]['update'] = $newVersion;
} }
usort($apps, function ($a, $b) { usort($apps, [$this, 'sortApps']);
$a = (string)$a['name'];
$b = (string)$b['name'];
if ($a === $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
});
break; break;
// disabled apps // disabled apps
case 'disabled': case 'disabled':
@ -394,14 +377,7 @@ class AppSettingsController extends Controller {
return $app; return $app;
}, $apps); }, $apps);
usort($apps, function ($a, $b) { usort($apps, [$this, 'sortApps']);
$a = (string)$a['name'];
$b = (string)$b['name'];
if ($a === $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
});
break; break;
case 'app-bundles': case 'app-bundles':
$bundles = $this->bundleFetcher->getBundles(); $bundles = $this->bundleFetcher->getBundles();
@ -440,16 +416,6 @@ class AppSettingsController extends Controller {
break; break;
default: default:
$apps = $this->getAppsForCategory($category); $apps = $this->getAppsForCategory($category);
// sort by score
usort($apps, function ($a, $b) {
$a = (int)$a['score'];
$b = (int)$b['score'];
if ($a === $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
});
break; break;
} }
@ -598,9 +564,7 @@ class AppSettingsController extends Controller {
*/ */
public function uninstallApp(string $appId): JSONResponse { public function uninstallApp(string $appId): JSONResponse {
$appId = OC_App::cleanAppId($appId); $appId = OC_App::cleanAppId($appId);
/** @var Installer $installer */ $result = $this->installer->removeApp($appId);
$installer = \OC::$server->query(\OC\Installer::class);
$result = $installer->removeApp($appId);
if($result !== false) { if($result !== false) {
// FIXME: Clear the cache - move that into some sane helper method // FIXME: Clear the cache - move that into some sane helper method
\OC::$server->getMemCacheFactory()->createDistributed('settings')->remove('listApps-0'); \OC::$server->getMemCacheFactory()->createDistributed('settings')->remove('listApps-0');
@ -610,13 +574,16 @@ class AppSettingsController extends Controller {
return new JSONResponse(['data' => ['message' => $this->l10n->t('Couldn\'t remove app.')]], Http::STATUS_INTERNAL_SERVER_ERROR); return new JSONResponse(['data' => ['message' => $this->l10n->t('Couldn\'t remove app.')]], Http::STATUS_INTERNAL_SERVER_ERROR);
} }
public function updateApp(string $appId) { /**
* @param string $appId
* @return JSONResponse
*/
public function updateApp(string $appId): JSONResponse {
$appId = OC_App::cleanAppId($appId); $appId = OC_App::cleanAppId($appId);
$this->config->setSystemValue('maintenance', true); $this->config->setSystemValue('maintenance', true);
try { try {
$installer = \OC::$server->query(\OC\Installer::class); $result = $this->installer->updateAppstoreApp($appId);
$result = $installer->updateAppstoreApp($appId);
$this->config->setSystemValue('maintenance', false); $this->config->setSystemValue('maintenance', false);
} catch (\Exception $ex) { } catch (\Exception $ex) {
$this->config->setSystemValue('maintenance', false); $this->config->setSystemValue('maintenance', false);
@ -628,4 +595,5 @@ class AppSettingsController extends Controller {
} }
return new JSONResponse(['data' => ['message' => $this->l10n->t('Couldn\'t update app.')]], Http::STATUS_INTERNAL_SERVER_ERROR); return new JSONResponse(['data' => ['message' => $this->l10n->t('Couldn\'t update app.')]], Http::STATUS_INTERNAL_SERVER_ERROR);
} }
} }