From 1db2ae96ce96ddfdec91e13c9d029169aba32c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 16 Oct 2023 17:23:47 +0200 Subject: [PATCH] Change SetupResult API to named constructors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../NeedsSystemAddressBookSync.php | 4 +-- .../lib/SetupChecks/CheckUserCertificates.php | 8 ++--- .../lib/SetupChecks/DefaultPhoneRegionSet.php | 2 +- .../lib/SetupChecks/InternetConnectivity.php | 6 ++-- .../lib/SetupChecks/LegacySSEKeyFormat.php | 4 +-- .../lib/SetupChecks/PhpDefaultCharset.php | 4 +-- apps/settings/lib/SetupChecks/PhpOutdated.php | 4 +-- .../lib/SetupChecks/PhpOutputBuffering.php | 4 +-- .../lib/SetupChecks/ReadOnlyConfig.php | 4 +-- .../lib/SetupChecks/SupportedDatabase.php | 8 ++--- .../lib/SetupChecks/LdapInvalidUuids.php | 4 +-- lib/public/SetupCheck/SetupResult.php | 35 ++++++++++++++++++- 12 files changed, 60 insertions(+), 27 deletions(-) diff --git a/apps/dav/lib/SetupChecks/NeedsSystemAddressBookSync.php b/apps/dav/lib/SetupChecks/NeedsSystemAddressBookSync.php index 78f8a1bff696e..476b37c160245 100644 --- a/apps/dav/lib/SetupChecks/NeedsSystemAddressBookSync.php +++ b/apps/dav/lib/SetupChecks/NeedsSystemAddressBookSync.php @@ -49,9 +49,9 @@ public function getCategory(): string { public function run(): SetupResult { if ($this->config->getAppValue('dav', 'needs_system_address_book_sync', 'no') === 'no') { - return new SetupResult(SetupResult::SUCCESS, $this->l10n->t('No outstanding DAV system address book sync.')); + return SetupResult::success($this->l10n->t('No outstanding DAV system address book sync.')); } else { - return new SetupResult(SetupResult::WARNING, $this->l10n->t('The DAV system address book sync has not run yet as your instance has more than 1000 users or because an error occurred. Please run it manually by calling occ dav:sync-system-addressbook.')); + return SetupResult::warning($this->l10n->t('The DAV system address book sync has not run yet as your instance has more than 1000 users or because an error occurred. Please run it manually by calling occ dav:sync-system-addressbook.')); } } } diff --git a/apps/settings/lib/SetupChecks/CheckUserCertificates.php b/apps/settings/lib/SetupChecks/CheckUserCertificates.php index 909ba9e149bd0..e09ef267e07a6 100644 --- a/apps/settings/lib/SetupChecks/CheckUserCertificates.php +++ b/apps/settings/lib/SetupChecks/CheckUserCertificates.php @@ -38,7 +38,7 @@ public function __construct( private IL10N $l10n, IConfig $config, ) { - $this->configValue = $config->getAppValue('files_external', 'user_certificate_scan', ''); + $this->configValue = $config->getAppValue('files_external', 'user_certificate_scan', ''); } public function getCategory(): string { @@ -52,11 +52,11 @@ public function getName(): string { public function run(): SetupResult { // all fine if neither "not-run-yet" nor a result if ($this->configValue === '') { - return new SetupResult(SetupResult::SUCCESS); + return SetupResult::success(); } if ($this->configValue === 'not-run-yet') { - return new SetupResult(SetupResult::INFO, $this->l10n->t('A background job is pending that checks for user imported SSL certificates. Please check back later.')); + return SetupResult::info($this->l10n->t('A background job is pending that checks for user imported SSL certificates. Please check back later.')); } - return new SetupResult(SetupResult::ERROR, $this->l10n->t('There are some user imported SSL certificates present, that are not used anymore with Nextcloud 21. They can be imported on the command line via "occ security:certificates:import" command. Their paths inside the data directory are shown below.')); + return SetupResult::error($this->l10n->t('There are some user imported SSL certificates present, that are not used anymore with Nextcloud 21. They can be imported on the command line via "occ security:certificates:import" command. Their paths inside the data directory are shown below.')); } } diff --git a/apps/settings/lib/SetupChecks/DefaultPhoneRegionSet.php b/apps/settings/lib/SetupChecks/DefaultPhoneRegionSet.php index 6cc37b08abe76..430c6ecd489d5 100644 --- a/apps/settings/lib/SetupChecks/DefaultPhoneRegionSet.php +++ b/apps/settings/lib/SetupChecks/DefaultPhoneRegionSet.php @@ -47,7 +47,7 @@ public function getCategory(): string { public function run(): SetupResult { if ($this->config->getSystemValueString('default_phone_region', '') !== '') { - return new SetupResult(SetupResult::SUCCESS, $this->config->getSystemValueString('default_phone_region', '')); + return SetupResult::success($this->config->getSystemValueString('default_phone_region', '')); } else { return new SetupResult( SetupResult::INFO, diff --git a/apps/settings/lib/SetupChecks/InternetConnectivity.php b/apps/settings/lib/SetupChecks/InternetConnectivity.php index 697dddc935a38..e27c5e2e981d3 100644 --- a/apps/settings/lib/SetupChecks/InternetConnectivity.php +++ b/apps/settings/lib/SetupChecks/InternetConnectivity.php @@ -57,7 +57,7 @@ public function getName(): string { public function run(): SetupResult { if ($this->config->getSystemValue('has_internet_connection', true) === false) { - return new SetupResult(SetupResult::SUCCESS, $this->l10n->t('Internet connectivity is disabled in configuration file.')); + return SetupResult::success($this->l10n->t('Internet connectivity is disabled in configuration file.')); } $siteArray = $this->config->getSystemValue('connectivity_check_domains', [ @@ -66,10 +66,10 @@ public function run(): SetupResult { foreach ($siteArray as $site) { if ($this->isSiteReachable($site)) { - return new SetupResult(SetupResult::SUCCESS); + return SetupResult::success(); } } - return new SetupResult(SetupResult::WARNING, $this->l10n->t('This server has no working internet connection: Multiple endpoints could not be reached. This means that some of the features like mounting external storage, notifications about updates or installation of third-party apps will not work. Accessing files remotely and sending of notification emails might not work, either. Establish a connection from this server to the internet to enjoy all features.')); + return SetupResult::warning($this->l10n->t('This server has no working internet connection: Multiple endpoints could not be reached. This means that some of the features like mounting external storage, notifications about updates or installation of third-party apps will not work. Accessing files remotely and sending of notification emails might not work, either. Establish a connection from this server to the internet to enjoy all features.')); } /** diff --git a/apps/settings/lib/SetupChecks/LegacySSEKeyFormat.php b/apps/settings/lib/SetupChecks/LegacySSEKeyFormat.php index 68964b3fdab0d..72300ede2b0d5 100644 --- a/apps/settings/lib/SetupChecks/LegacySSEKeyFormat.php +++ b/apps/settings/lib/SetupChecks/LegacySSEKeyFormat.php @@ -49,8 +49,8 @@ public function getName(): string { public function run(): SetupResult { if ($this->config->getSystemValueBool('encryption.legacy_format_support', false) === false) { - return new SetupResult(SetupResult::SUCCESS); + return SetupResult::success(); } - return new SetupResult(SetupResult::WARNING, $this->l10n->t('The old server-side-encryption format is enabled. We recommend disabling this.'), $this->urlGenerator->linkToDocs('admin-sse-legacy-format')); + return SetupResult::warning($this->l10n->t('The old server-side-encryption format is enabled. We recommend disabling this.'), $this->urlGenerator->linkToDocs('admin-sse-legacy-format')); } } diff --git a/apps/settings/lib/SetupChecks/PhpDefaultCharset.php b/apps/settings/lib/SetupChecks/PhpDefaultCharset.php index 5c14e9a6734e5..3f7a8c58e52f2 100644 --- a/apps/settings/lib/SetupChecks/PhpDefaultCharset.php +++ b/apps/settings/lib/SetupChecks/PhpDefaultCharset.php @@ -45,9 +45,9 @@ public function getCategory(): string { public function run(): SetupResult { if (strtoupper(trim(ini_get('default_charset'))) === 'UTF-8') { - return new SetupResult(SetupResult::SUCCESS); + return SetupResult::success(); } else { - return new SetupResult(SetupResult::WARNING, $this->l10n->t('PHP configuration option default_charset should be UTF-8')); + return SetupResult::warning($this->l10n->t('PHP configuration option default_charset should be UTF-8')); } } } diff --git a/apps/settings/lib/SetupChecks/PhpOutdated.php b/apps/settings/lib/SetupChecks/PhpOutdated.php index 51b479ec4b96e..2d1616493152f 100644 --- a/apps/settings/lib/SetupChecks/PhpOutdated.php +++ b/apps/settings/lib/SetupChecks/PhpOutdated.php @@ -47,8 +47,8 @@ public function getName(): string { public function run(): SetupResult { if (PHP_VERSION_ID < 80100) { - return new SetupResult(SetupResult::WARNING, $this->l10n->t('You are currently running PHP %s. PHP 8.0 is now deprecated in Nextcloud 27. Nextcloud 28 may require at least PHP 8.1. Please upgrade to one of the officially supported PHP versions provided by the PHP Group as soon as possible.', [PHP_VERSION]), 'https://secure.php.net/supported-versions.php'); + return SetupResult::warning($this->l10n->t('You are currently running PHP %s. PHP 8.0 is now deprecated in Nextcloud 27. Nextcloud 28 may require at least PHP 8.1. Please upgrade to one of the officially supported PHP versions provided by the PHP Group as soon as possible.', [PHP_VERSION]), 'https://secure.php.net/supported-versions.php'); } - return new SetupResult(SetupResult::SUCCESS, $this->l10n->t('You are currently running PHP %s.', [PHP_VERSION])); + return SetupResult::success($this->l10n->t('You are currently running PHP %s.', [PHP_VERSION])); } } diff --git a/apps/settings/lib/SetupChecks/PhpOutputBuffering.php b/apps/settings/lib/SetupChecks/PhpOutputBuffering.php index f3a21418c1c4f..58321e9f5dc81 100644 --- a/apps/settings/lib/SetupChecks/PhpOutputBuffering.php +++ b/apps/settings/lib/SetupChecks/PhpOutputBuffering.php @@ -46,9 +46,9 @@ public function getName(): string { public function run(): SetupResult { $value = trim(ini_get('output_buffering')); if ($value === '' || $value === '0') { - return new SetupResult(SetupResult::SUCCESS); + return SetupResult::success(); } else { - return new SetupResult(SetupResult::ERROR, $this->l10n->t('PHP configuration option output_buffering must be disabled')); + return SetupResult::error($this->l10n->t('PHP configuration option output_buffering must be disabled')); } } } diff --git a/apps/settings/lib/SetupChecks/ReadOnlyConfig.php b/apps/settings/lib/SetupChecks/ReadOnlyConfig.php index 4c81a410449b0..56f86ba9ab442 100644 --- a/apps/settings/lib/SetupChecks/ReadOnlyConfig.php +++ b/apps/settings/lib/SetupChecks/ReadOnlyConfig.php @@ -47,9 +47,9 @@ public function getCategory(): string { public function run(): SetupResult { if ($this->config->getSystemValueBool('config_is_read_only', false)) { - return new SetupResult(SetupResult::INFO, $this->l10n->t('The read-only config has been enabled. This prevents setting some configurations via the web-interface. Furthermore, the file needs to be made writable manually for every update.')); + return SetupResult::info($this->l10n->t('The read-only config has been enabled. This prevents setting some configurations via the web-interface. Furthermore, the file needs to be made writable manually for every update.')); } else { - return new SetupResult(SetupResult::SUCCESS, $this->l10n->t('Nextcloud configuration file is writable')); + return SetupResult::success($this->l10n->t('Nextcloud configuration file is writable')); } } } diff --git a/apps/settings/lib/SetupChecks/SupportedDatabase.php b/apps/settings/lib/SetupChecks/SupportedDatabase.php index 0cbaa5b114ed6..3d10798a7dabd 100644 --- a/apps/settings/lib/SetupChecks/SupportedDatabase.php +++ b/apps/settings/lib/SetupChecks/SupportedDatabase.php @@ -68,11 +68,11 @@ public function run(): SetupResult { if (str_contains($version, 'mariadb')) { if (version_compare($version, '10.2', '<')) { - return new SetupResult(SetupResult::WARNING, $this->l10n->t('MariaDB version "%s" is used. Nextcloud 21 and higher do not support this version and require MariaDB 10.2 or higher.', $row['Value'])); + return SetupResult::warning($this->l10n->t('MariaDB version "%s" is used. Nextcloud 21 and higher do not support this version and require MariaDB 10.2 or higher.', $row['Value'])); } } else { if (version_compare($version, '8', '<')) { - return new SetupResult(SetupResult::WARNING, $this->l10n->t('MySQL version "%s" is used. Nextcloud 21 and higher do not support this version and require MySQL 8.0 or MariaDB 10.2 or higher.', $row['Value'])); + return SetupResult::warning($this->l10n->t('MySQL version "%s" is used. Nextcloud 21 and higher do not support this version and require MySQL 8.0 or MariaDB 10.2 or higher.', $row['Value'])); } } break; @@ -84,13 +84,13 @@ public function run(): SetupResult { $result->execute(); $row = $result->fetch(); if (version_compare($row['server_version'], '9.6', '<')) { - return new SetupResult(SetupResult::WARNING, $this->l10n->t('PostgreSQL version "%s" is used. Nextcloud 21 and higher do not support this version and require PostgreSQL 9.6 or higher.', $row['server_version'])); + return SetupResult::warning($this->l10n->t('PostgreSQL version "%s" is used. Nextcloud 21 and higher do not support this version and require PostgreSQL 9.6 or higher.', $row['server_version'])); } break; case OraclePlatform::class: break; } // TODO still show db and version on success? - return new SetupResult(SetupResult::SUCCESS); + return SetupResult::success(); } } diff --git a/apps/user_ldap/lib/SetupChecks/LdapInvalidUuids.php b/apps/user_ldap/lib/SetupChecks/LdapInvalidUuids.php index a11246fd1420e..d6de11d35b9d5 100644 --- a/apps/user_ldap/lib/SetupChecks/LdapInvalidUuids.php +++ b/apps/user_ldap/lib/SetupChecks/LdapInvalidUuids.php @@ -52,9 +52,9 @@ public function getName(): string { public function run(): SetupResult { if (count($this->userMapping->getList(0, 1, true)) === 0 && count($this->groupMapping->getList(0, 1, true)) === 0) { - return new SetupResult(SetupResult::SUCCESS); + return SetupResult::success(); } else { - return new SetupResult(SetupResult::WARNING, $this->l10n->t('Invalid UUIDs of LDAP users or groups have been found. Please review your "Override UUID detection" settings in the Expert part of the LDAP configuration and use "occ ldap:update-uuid" to update them.')); + return SetupResult::warning($this->l10n->t('Invalid UUIDs of LDAP users or groups have been found. Please review your "Override UUID detection" settings in the Expert part of the LDAP configuration and use "occ ldap:update-uuid" to update them.')); } } } diff --git a/lib/public/SetupCheck/SetupResult.php b/lib/public/SetupCheck/SetupResult.php index ce3c3dd892729..37f3595dc0320 100644 --- a/lib/public/SetupCheck/SetupResult.php +++ b/lib/public/SetupCheck/SetupResult.php @@ -38,10 +38,11 @@ class SetupResult implements \JsonSerializable { public const ERROR = 'error'; /** + * @brief Private constructor, use success()/info()/warning()/error() instead * @param self::SUCCESS|self::INFO|self::WARNING|self::ERROR $severity * @since 28.0.0 */ - public function __construct( + private function __construct( private string $severity, private ?string $description = null, private ?string $linkToDoc = null, @@ -51,6 +52,38 @@ public function __construct( $this->linkToDoc = $linkToDoc; } + /** + * @brief Create a success result object + * @since 28.0.0 + */ + public static function success(?string $description = null, ?string $linkToDoc = null): self { + return new self(self::SUCCESS, $description, $linkToDoc); + } + + /** + * @brief Create an info result object + * @since 28.0.0 + */ + public static function info(?string $description = null, ?string $linkToDoc = null): self { + return new self(self::INFO, $description, $linkToDoc); + } + + /** + * @brief Create a warning result object + * @since 28.0.0 + */ + public static function warning(?string $description = null, ?string $linkToDoc = null): self { + return new self(self::WARNING, $description, $linkToDoc); + } + + /** + * @brief Create an error result object + * @since 28.0.0 + */ + public static function error(?string $description = null, ?string $linkToDoc = null): self { + return new self(self::ERROR, $description, $linkToDoc); + } + /** * @brief Get the severity for the setup check result *