@@ -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,33 +70,34 @@ 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 *
84- * @return DataResponse<Http::STATUS_OK, list<array{id: string, name: string, description: string, ...}>, array{}>
84+ * @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{}>
8585 *
8686 * 200: The apps were found successfully
8787 */
8888 #[ApiRoute(verb: 'GET ' , url: '/api/v1/apps ' )]
8989 public function listApps (): DataResponse {
9090 $ apps = $ this ->getAllApps ();
9191
92+ /** @var array<string>|mixed $ignoreMaxApps */
9293 $ ignoreMaxApps = $ this ->config ->getSystemValue ('app_install_overwrite ' , []);
9394 if (!is_array ($ ignoreMaxApps )) {
9495 $ this ->logger ->warning ('The value given for app_install_overwrite is not an array. Ignoring... ' );
9596 $ ignoreMaxApps = [];
9697 }
9798
9899 // Extend existing app details
99- $ apps = array_map (function (array $ appData ) use ($ ignoreMaxApps ) {
100+ $ apps = array_map (function (array $ appData ) use ($ ignoreMaxApps ): array {
100101 if (isset ($ appData ['appstoreData ' ])) {
101102 $ appstoreData = $ appData ['appstoreData ' ];
102103 $ appData ['screenshot ' ] = $ this ->createProxyPreviewUrl ($ appstoreData ['screenshots ' ][0 ]['url ' ] ?? '' );
@@ -105,7 +106,7 @@ public function listApps(): DataResponse {
105106 }
106107
107108 $ newVersion = $ this ->installer ->isUpdateAvailable ($ appData ['id ' ]);
108- if ($ newVersion ) {
109+ if ($ newVersion !== false ) {
109110 $ appData ['update ' ] = $ newVersion ;
110111 }
111112
@@ -119,6 +120,7 @@ public function listApps(): DataResponse {
119120 $ groups = [$ groups ];
120121 }
121122 }
123+
122124 $ appData ['groups ' ] = $ groups ;
123125 $ appData ['canUninstall ' ] = !$ appData ['active ' ] && $ appData ['removable ' ];
124126
@@ -132,6 +134,7 @@ public function listApps(): DataResponse {
132134 $ appData ['missingMaxNextcloudVersion ' ] = !isset ($ appData ['dependencies ' ]['nextcloud ' ]['@attributes ' ]['max-version ' ]);
133135 $ appData ['isCompatible ' ] = $ this ->dependencyAnalyzer ->isMarkedCompatible ($ appData );
134136
137+ /** @var array{id: string, name: string, groups: list<string>, internal: bool, isCompatible: bool, missingDependencies?: list<string>, missingMaxNextcloudVersion: bool, missingMinNextcloudVersion: bool, ...<array-key, mixed>} $appData */
135138 return $ appData ;
136139 }, $ apps );
137140
@@ -166,16 +169,17 @@ public function enableApp(string $appId, array $groups = []): DataResponse {
166169
167170 $ this ->installer ->installApp ($ appId );
168171
169- if (count ( $ groups) > 0 ) {
172+ if ($ groups !== [] ) {
170173 $ this ->appManager ->enableAppForGroups ($ appId , $ this ->getGroupList ($ groups ));
171174 } else {
172175 $ this ->appManager ->enableApp ($ appId );
173176 }
177+
174178 $ updateRequired = $ this ->appManager ->isUpgradeRequired ($ appId );
175179 return new DataResponse (['update_required ' => $ updateRequired ]);
176- } catch (\Throwable $ e ) {
177- $ this ->logger ->error ('could not enable app ' , ['exception ' => $ e ]);
178- throw new OCSException ('could not enable app ' , Http::STATUS_INTERNAL_SERVER_ERROR , $ e );
180+ } catch (\Throwable $ throwable ) {
181+ $ this ->logger ->error ('could not enable app ' , ['exception ' => $ throwable ]);
182+ throw new OCSException ('could not enable app ' , Http::STATUS_INTERNAL_SERVER_ERROR , $ throwable );
179183 }
180184 }
181185
@@ -196,9 +200,9 @@ public function disableApp(string $appId): DataResponse {
196200 $ appId = $ this ->appManager ->cleanAppId ($ appId );
197201 $ this ->appManager ->disableApp ($ appId );
198202 return new DataResponse ([]);
199- } catch (\Exception $ e ) {
200- $ this ->logger ->error ('could not disable app ' , ['exception ' => $ e ]);
201- throw new OCSException ('could not disable app ' , Http::STATUS_INTERNAL_SERVER_ERROR , $ e );
203+ } catch (\Exception $ exception ) {
204+ $ this ->logger ->error ('could not disable app ' , ['exception ' => $ exception ]);
205+ throw new OCSException ('could not disable app ' , Http::STATUS_INTERNAL_SERVER_ERROR , $ exception );
202206 }
203207 }
204208
@@ -223,6 +227,7 @@ public function uninstallApp(string $appId): DataResponse {
223227 $ this ->appManager ->clearAppsCache ();
224228 return new DataResponse ([]);
225229 }
230+
226231 throw new OCSException ('could not remove app ' , Http::STATUS_INTERNAL_SERVER_ERROR );
227232 }
228233
@@ -247,13 +252,14 @@ public function updateApp(string $appId): DataResponse {
247252 if ($ result === false ) {
248253 throw new \Exception ('Update failed ' );
249254 }
250- } catch (\Exception $ ex ) {
255+ } catch (\Exception $ exception ) {
251256 $ this ->config ->setSystemValue ('maintenance ' , false );
252- throw new OCSException ('could not update app ' , Http::STATUS_INTERNAL_SERVER_ERROR , $ ex );
257+ throw new OCSException ('could not update app ' , Http::STATUS_INTERNAL_SERVER_ERROR , $ exception );
253258 }
254259
255260 return new DataResponse ([]);
256261 }
262+
257263 /**
258264 * Force enable an app.
259265 * This will override the nextcloud version requirement for an app
@@ -278,10 +284,11 @@ private function createProxyPreviewUrl(string $url): string {
278284 if ($ url === '' ) {
279285 return '' ;
280286 }
287+
281288 return 'https://usercontent.apps.nextcloud.com/ ' . base64_encode ($ url );
282289 }
283290
284- private function fetchApps () {
291+ private function fetchApps (): void {
285292 $ appClass = new \OC_App ();
286293 $ apps = $ appClass ->listAllApps ();
287294 foreach ($ apps as $ app ) {
@@ -296,6 +303,7 @@ private function fetchApps() {
296303
297304 $ app ['screenshot ' ] = $ this ->createProxyPreviewUrl ($ appScreenshot );
298305 }
306+
299307 $ this ->allApps [$ app ['id ' ]] = $ app ;
300308 }
301309
@@ -332,17 +340,16 @@ private function getAllApps() {
332340 if (empty ($ this ->allApps )) {
333341 $ this ->fetchApps ();
334342 }
343+
335344 return $ this ->allApps ;
336345 }
337346
338347 /**
339348 * Get all apps for a category from the app store
340349 *
341- * @param string $requestedCategory
342- * @return array
343350 * @throws \Exception
344351 */
345- private function getAppsForCategory ($ requestedCategory = '' ): array {
352+ private function getAppsForCategory (string $ requestedCategory = '' ): array {
346353 $ versionParser = new VersionParser ();
347354 $ formattedApps = [];
348355 $ apps = $ this ->appFetcher ->get ();
@@ -355,6 +362,7 @@ private function getAppsForCategory($requestedCategory = ''): array {
355362 $ isInCategory = true ;
356363 }
357364 }
365+
358366 if (!$ isInCategory ) {
359367 continue ;
360368 }
@@ -363,14 +371,17 @@ private function getAppsForCategory($requestedCategory = ''): array {
363371 if (!isset ($ app ['releases ' ][0 ]['rawPlatformVersionSpec ' ])) {
364372 continue ;
365373 }
374+
366375 $ nextcloudVersion = $ versionParser ->getVersion ($ app ['releases ' ][0 ]['rawPlatformVersionSpec ' ]);
367376 $ nextcloudVersionDependencies = [];
368377 if ($ nextcloudVersion ->getMinimumVersion () !== '' ) {
369378 $ nextcloudVersionDependencies ['nextcloud ' ]['@attributes ' ]['min-version ' ] = $ nextcloudVersion ->getMinimumVersion ();
370379 }
380+
371381 if ($ nextcloudVersion ->getMaximumVersion () !== '' ) {
372382 $ nextcloudVersionDependencies ['nextcloud ' ]['@attributes ' ]['max-version ' ] = $ nextcloudVersion ->getMaximumVersion ();
373383 }
384+
374385 $ phpVersion = $ versionParser ->getVersion ($ app ['releases ' ][0 ]['rawPhpVersionSpec ' ]);
375386
376387 try {
@@ -384,12 +395,15 @@ private function getAppsForCategory($requestedCategory = ''): array {
384395 if ($ phpVersion ->getMinimumVersion () !== '' ) {
385396 $ phpDependencies ['php ' ]['@attributes ' ]['min-version ' ] = $ phpVersion ->getMinimumVersion ();
386397 }
398+
387399 if ($ phpVersion ->getMaximumVersion () !== '' ) {
388400 $ phpDependencies ['php ' ]['@attributes ' ]['max-version ' ] = $ phpVersion ->getMaximumVersion ();
389401 }
402+
390403 if (isset ($ app ['releases ' ][0 ]['minIntSize ' ])) {
391404 $ phpDependencies ['php ' ]['@attributes ' ]['min-int-size ' ] = $ app ['releases ' ][0 ]['minIntSize ' ];
392405 }
406+
393407 $ authors = '' ;
394408 foreach ($ app ['authors ' ] as $ key => $ author ) {
395409 $ authors .= $ author ['name ' ];
@@ -454,24 +468,28 @@ private function getAppsForCategory($requestedCategory = ''): array {
454468 return $ formattedApps ;
455469 }
456470
457- private function getGroupList (array $ groups ) {
471+ /**
472+ * @param string[] $groups - The group ids to fetch
473+ * @return list<IGroup> - The list of groups matching the given group ids
474+ */
475+ private function getGroupList (array $ groups ): array {
458476 $ groupManager = Server::get (IGroupManager::class);
459477 $ groupsList = [];
460478 foreach ($ groups as $ group ) {
461479 $ groupItem = $ groupManager ->get ($ group );
462480 if ($ groupItem instanceof IGroup) {
463- $ groupsList [] = $ groupManager -> get ( $ group ) ;
481+ $ groupsList [] = $ groupItem ;
464482 }
465483 }
484+
466485 return $ groupsList ;
467486 }
468487
469- private function sortApps ($ a , $ b ) {
470- $ a = (string )$ a ['name ' ];
471- $ b = (string )$ b ['name ' ];
472- if ($ a === $ b ) {
473- return 0 ;
474- }
475- return ($ a < $ b ) ? -1 : 1 ;
488+ /**
489+ * @param array{name: string, ...} $a
490+ * @param array{name: string, ...} $b
491+ */
492+ private function sortApps (array $ a , array $ b ): int {
493+ return $ a ['name ' ] <=> $ b ['name ' ];
476494 }
477495}
0 commit comments