@@ -43,17 +43,17 @@ class ApiController extends OCSController {
4343
4444 public function __construct (
4545 IRequest $ request ,
46- private IConfig $ config ,
47- private IAppConfig $ appConfig ,
48- private AppManager $ appManager ,
49- private DependencyAnalyzer $ dependencyAnalyzer ,
50- private CategoryFetcher $ categoryFetcher ,
51- private AppFetcher $ appFetcher ,
52- private IFactory $ l10nFactory ,
53- private BundleFetcher $ bundleFetcher ,
54- private Installer $ installer ,
55- private IRegistry $ subscriptionRegistry ,
56- private LoggerInterface $ logger ,
46+ private readonly IConfig $ config ,
47+ private readonly IAppConfig $ appConfig ,
48+ private readonly AppManager $ appManager ,
49+ private readonly DependencyAnalyzer $ dependencyAnalyzer ,
50+ private readonly CategoryFetcher $ categoryFetcher ,
51+ private readonly AppFetcher $ appFetcher ,
52+ private readonly IFactory $ l10nFactory ,
53+ private readonly BundleFetcher $ bundleFetcher ,
54+ private readonly Installer $ installer ,
55+ private readonly IRegistry $ subscriptionRegistry ,
56+ private readonly LoggerInterface $ logger ,
5757 ) {
5858 parent ::__construct (Application::APP_ID , $ request );
5959 }
@@ -70,34 +70,35 @@ public function listCategories(): DataResponse {
7070 $ currentLanguage = substr ($ this ->l10nFactory ->findLanguage (), 0 , 2 );
7171
7272 $ categories = $ this ->categoryFetcher ->get ();
73- $ categories = array_map (fn ($ category ) => [
73+ $ categories = array_map (fn (array $ category ): array => [
7474 'id ' => $ category ['id ' ],
7575 'displayName ' => $ category ['translations ' ][$ currentLanguage ]['name ' ] ?? $ category ['translations ' ]['en ' ]['name ' ],
7676 ], $ categories );
7777
78- return new DataResponse (array_values ( $ categories) );
78+ return new DataResponse ($ categories );
7979 }
8080
8181 /**
8282 * Get all available apps
8383 *
8484 * @param bool $details - Whether to include detailed appstore information about the app
85- * @return DataResponse<Http::STATUS_OK, list<array{id: string, name: string, description: string, ...}>, array{}>
85+ * @return DataResponse<Http::STATUS_OK, list<array{id: string, name: string, groups: list< string>, internal: bool, isCompatible: bool, missingDependencies?: list<string>, missingMaxNextcloudVersion: bool, missingMinNextcloudVersion: bool, ...<array-key, mixed> }>, array{}>
8686 *
8787 * 200: The apps were found successfully
8888 */
8989 #[ApiRoute(verb: 'GET ' , url: '/api/v1/apps ' )]
9090 public function listApps (bool $ details = false ): DataResponse {
9191 $ apps = $ this ->getAllApps ();
9292
93+ /** @var array<string>|mixed $ignoreMaxApps */
9394 $ ignoreMaxApps = $ this ->config ->getSystemValue ('app_install_overwrite ' , []);
9495 if (!is_array ($ ignoreMaxApps )) {
9596 $ this ->logger ->warning ('The value given for app_install_overwrite is not an array. Ignoring... ' );
9697 $ ignoreMaxApps = [];
9798 }
9899
99100 // Extend existing app details
100- $ apps = array_map (function (array $ appData ) use ($ ignoreMaxApps , $ details ) {
101+ $ apps = array_map (function (array $ appData ) use ($ ignoreMaxApps , $ details ): array {
101102 if (isset ($ appData ['appstoreData ' ])) {
102103 $ appstoreData = $ appData ['appstoreData ' ];
103104 $ appData ['screenshot ' ] = $ this ->createProxyPreviewUrl ($ appstoreData ['screenshots ' ][0 ]['url ' ] ?? '' );
@@ -110,7 +111,7 @@ public function listApps(bool $details = false): DataResponse {
110111 }
111112
112113 $ newVersion = $ this ->installer ->isUpdateAvailable ($ appData ['id ' ]);
113- if ($ newVersion ) {
114+ if ($ newVersion !== false ) {
114115 $ appData ['update ' ] = $ newVersion ;
115116 }
116117
@@ -124,6 +125,7 @@ public function listApps(bool $details = false): DataResponse {
124125 $ groups = [$ groups ];
125126 }
126127 }
128+
127129 $ appData ['groups ' ] = $ groups ;
128130 // analyze dependencies
129131 $ ignoreMax = in_array ($ appData ['id ' ], $ ignoreMaxApps );
@@ -135,6 +137,7 @@ public function listApps(bool $details = false): DataResponse {
135137 $ appData ['isCompatible ' ] = $ this ->dependencyAnalyzer ->isMarkedCompatible ($ appData );
136138 $ appData ['internal ' ] = in_array ($ appData ['id ' ], $ this ->appManager ->getAlwaysEnabledApps ());
137139
140+ /** @var array{id: string, name: string, groups: list<string>, internal: bool, isCompatible: bool, missingDependencies?: list<string>, missingMaxNextcloudVersion: bool, missingMinNextcloudVersion: bool, ...<array-key, mixed>} */
138141 return $ appData ;
139142 }, $ apps );
140143
@@ -169,16 +172,17 @@ public function enableApp(string $appId, array $groups = []): DataResponse {
169172
170173 $ this ->installer ->installApp ($ appId );
171174
172- if (count ( $ groups) > 0 ) {
175+ if ($ groups !== [] ) {
173176 $ this ->appManager ->enableAppForGroups ($ appId , $ this ->getGroupList ($ groups ));
174177 } else {
175178 $ this ->appManager ->enableApp ($ appId );
176179 }
180+
177181 $ updateRequired = $ this ->appManager ->isUpgradeRequired ($ appId );
178182 return new DataResponse (['update_required ' => $ updateRequired ]);
179- } catch (\Throwable $ e ) {
180- $ this ->logger ->error ('could not enable app ' , ['exception ' => $ e ]);
181- throw new OCSException ('could not enable app ' , Http::STATUS_INTERNAL_SERVER_ERROR , $ e );
183+ } catch (\Throwable $ throwable ) {
184+ $ this ->logger ->error ('could not enable app ' , ['exception ' => $ throwable ]);
185+ throw new OCSException ('could not enable app ' , Http::STATUS_INTERNAL_SERVER_ERROR , $ throwable );
182186 }
183187 }
184188
@@ -200,9 +204,9 @@ public function disableApp(string $appId): DataResponse {
200204 $ this ->appManager ->removeOverwriteNextcloudRequirement ($ appId );
201205 $ this ->appManager ->disableApp ($ appId );
202206 return new DataResponse ([]);
203- } catch (\Exception $ e ) {
204- $ this ->logger ->error ('could not disable app ' , ['exception ' => $ e ]);
205- throw new OCSException ('could not disable app ' , Http::STATUS_INTERNAL_SERVER_ERROR , $ e );
207+ } catch (\Exception $ exception ) {
208+ $ this ->logger ->error ('could not disable app ' , ['exception ' => $ exception ]);
209+ throw new OCSException ('could not disable app ' , Http::STATUS_INTERNAL_SERVER_ERROR , $ exception );
206210 }
207211 }
208212
@@ -231,6 +235,7 @@ public function uninstallApp(string $appId): DataResponse {
231235 $ this ->appManager ->clearAppsCache ();
232236 return new DataResponse ([]);
233237 }
238+
234239 throw new OCSException ('could not remove app ' , Http::STATUS_INTERNAL_SERVER_ERROR );
235240 }
236241
@@ -255,13 +260,14 @@ public function updateApp(string $appId): DataResponse {
255260 if ($ result === false ) {
256261 throw new \Exception ('Update failed ' );
257262 }
258- } catch (\Exception $ ex ) {
263+ } catch (\Exception $ exception ) {
259264 $ this ->config ->setSystemValue ('maintenance ' , false );
260- throw new OCSException ('could not update app ' , Http::STATUS_INTERNAL_SERVER_ERROR , $ ex );
265+ throw new OCSException ('could not update app ' , Http::STATUS_INTERNAL_SERVER_ERROR , $ exception );
261266 }
262267
263268 return new DataResponse ([]);
264269 }
270+
265271 /**
266272 * Force enable an app.
267273 * This will override the nextcloud version requirement for an app
@@ -286,10 +292,11 @@ private function createProxyPreviewUrl(string $url): string {
286292 if ($ url === '' ) {
287293 return '' ;
288294 }
295+
289296 return 'https://usercontent.apps.nextcloud.com/ ' . base64_encode ($ url );
290297 }
291298
292- private function fetchApps () {
299+ private function fetchApps (): void {
293300 $ appClass = new \OC_App ();
294301 $ apps = $ appClass ->listAllApps ();
295302 foreach ($ apps as $ app ) {
@@ -304,6 +311,7 @@ private function fetchApps() {
304311
305312 $ app ['screenshot ' ] = $ this ->createProxyPreviewUrl ($ appScreenshot );
306313 }
314+
307315 $ this ->allApps [$ app ['id ' ]] = $ app ;
308316 }
309317
@@ -340,17 +348,16 @@ private function getAllApps() {
340348 if (empty ($ this ->allApps )) {
341349 $ this ->fetchApps ();
342350 }
351+
343352 return $ this ->allApps ;
344353 }
345354
346355 /**
347356 * Get all apps for a category from the app store
348357 *
349- * @param string $requestedCategory
350- * @return array
351358 * @throws \Exception
352359 */
353- private function getAppsForCategory ($ requestedCategory = '' ): array {
360+ private function getAppsForCategory (string $ requestedCategory = '' ): array {
354361 $ versionParser = new VersionParser ();
355362 $ formattedApps = [];
356363 $ apps = $ this ->appFetcher ->get ();
@@ -363,6 +370,7 @@ private function getAppsForCategory($requestedCategory = ''): array {
363370 $ isInCategory = true ;
364371 }
365372 }
373+
366374 if (!$ isInCategory ) {
367375 continue ;
368376 }
@@ -371,14 +379,17 @@ private function getAppsForCategory($requestedCategory = ''): array {
371379 if (!isset ($ app ['releases ' ][0 ]['rawPlatformVersionSpec ' ])) {
372380 continue ;
373381 }
382+
374383 $ nextcloudVersion = $ versionParser ->getVersion ($ app ['releases ' ][0 ]['rawPlatformVersionSpec ' ]);
375384 $ nextcloudVersionDependencies = [];
376385 if ($ nextcloudVersion ->getMinimumVersion () !== '' ) {
377386 $ nextcloudVersionDependencies ['nextcloud ' ]['@attributes ' ]['min-version ' ] = $ nextcloudVersion ->getMinimumVersion ();
378387 }
388+
379389 if ($ nextcloudVersion ->getMaximumVersion () !== '' ) {
380390 $ nextcloudVersionDependencies ['nextcloud ' ]['@attributes ' ]['max-version ' ] = $ nextcloudVersion ->getMaximumVersion ();
381391 }
392+
382393 $ phpVersion = $ versionParser ->getVersion ($ app ['releases ' ][0 ]['rawPhpVersionSpec ' ]);
383394
384395 try {
@@ -392,12 +403,15 @@ private function getAppsForCategory($requestedCategory = ''): array {
392403 if ($ phpVersion ->getMinimumVersion () !== '' ) {
393404 $ phpDependencies ['php ' ]['@attributes ' ]['min-version ' ] = $ phpVersion ->getMinimumVersion ();
394405 }
406+
395407 if ($ phpVersion ->getMaximumVersion () !== '' ) {
396408 $ phpDependencies ['php ' ]['@attributes ' ]['max-version ' ] = $ phpVersion ->getMaximumVersion ();
397409 }
410+
398411 if (isset ($ app ['releases ' ][0 ]['minIntSize ' ])) {
399412 $ phpDependencies ['php ' ]['@attributes ' ]['min-int-size ' ] = $ app ['releases ' ][0 ]['minIntSize ' ];
400413 }
414+
401415 $ authors = '' ;
402416 foreach ($ app ['authors ' ] as $ key => $ author ) {
403417 $ authors .= $ author ['name ' ];
@@ -461,24 +475,34 @@ private function getAppsForCategory($requestedCategory = ''): array {
461475 return $ formattedApps ;
462476 }
463477
464- private function getGroupList (array $ groups ) {
478+ /**
479+ * @param string[] $groups - The group ids to fetch
480+ * @return list<IGroup> - The list of groups matching the given group ids
481+ */
482+ private function getGroupList (array $ groups ): array {
465483 $ groupManager = Server::get (IGroupManager::class);
466484 $ groupsList = [];
467485 foreach ($ groups as $ group ) {
468486 $ groupItem = $ groupManager ->get ($ group );
469487 if ($ groupItem instanceof IGroup) {
470- $ groupsList [] = $ groupManager -> get ( $ group ) ;
488+ $ groupsList [] = $ groupItem ;
471489 }
472490 }
491+
473492 return $ groupsList ;
474493 }
475494
476- private function sortApps ($ a , $ b ) {
477- $ a = (string )$ a ['name ' ];
478- $ b = (string )$ b ['name ' ];
495+ /**
496+ * @param array{name: string, ...} $a
497+ * @param array{name: string, ...} $b
498+ */
499+ private function sortApps (array $ a , array $ b ): int {
500+ $ a = $ a ['name ' ];
501+ $ b = $ b ['name ' ];
479502 if ($ a === $ b ) {
480503 return 0 ;
481504 }
505+
482506 return ($ a < $ b ) ? -1 : 1 ;
483507 }
484508}
0 commit comments