| Total Complexity | 44 |
| Total Lines | 283 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like OnlyofficeSettingsFormBuilder 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 OnlyofficeSettingsFormBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class OnlyofficeSettingsFormBuilder |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Directory with layouts for banners/partials. |
||
| 23 | */ |
||
| 24 | private const ONLYOFFICE_LAYOUT_DIR = '/Onlyoffice/layout/'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Build OnlyOffice plugin settings form. |
||
| 28 | * |
||
| 29 | * Adds: |
||
| 30 | * - "Connect to demo" checkbox |
||
| 31 | * - "Test connection" submit button (server-side check) |
||
| 32 | * - Quick test LINKS (healthcheck, API, preloader) opening in a new tab |
||
| 33 | * |
||
| 34 | * @param OnlyofficeAppsettings $settingsManager |
||
| 35 | * |
||
| 36 | * @return FormValidator |
||
| 37 | */ |
||
| 38 | public static function buildSettingsForm(OnlyofficeAppsettings $settingsManager) |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Validate OnlyOffice plugin settings form and persist values. |
||
| 148 | * |
||
| 149 | * @param OnlyofficeAppsettings $settingsManager |
||
| 150 | * |
||
| 151 | * @return OnlyofficePlugin |
||
| 152 | */ |
||
| 153 | public static function validateSettingsForm(OnlyofficeAppsettings $settingsManager) |
||
| 154 | { |
||
| 155 | $plugin = $settingsManager->plugin; |
||
| 156 | $plugin_info = $plugin->get_info(); |
||
| 157 | $form = $plugin_info['settings_form']; |
||
| 158 | |||
| 159 | // Read submitted values from the form. |
||
| 160 | $result = $form->getSubmitValues(); |
||
| 161 | unset($result['submit_button']); |
||
| 162 | |||
| 163 | // Make posted values available as runtime overrides for SettingsManager. |
||
| 164 | $settingsManager->newSettings = $result; |
||
| 165 | |||
| 166 | // Detect "Test connection" (non-destructive). |
||
| 167 | $testing = isset($result['test_connection']); |
||
| 168 | |||
| 169 | // Checkbox may be absent in POST when unchecked. |
||
| 170 | $connectDemo = (bool) ($result['connect_demo'] ?? false); |
||
| 171 | |||
| 172 | if ($testing) { |
||
| 173 | $outcome = self::runSelfTest($settingsManager); |
||
| 174 | |||
| 175 | if ($outcome['ok']) { |
||
| 176 | Display::addFlash( |
||
| 177 | Display::return_message('[OnlyOffice] Connection test passed: '.$outcome['message'], 'confirmation') |
||
| 178 | ); |
||
| 179 | } else { |
||
| 180 | Display::addFlash( |
||
| 181 | Display::return_message('[OnlyOffice] Connection test failed: '.$outcome['message'], 'error') |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | header('Location: '.$plugin->getConfigLink()); |
||
| 186 | exit; |
||
| 187 | } |
||
| 188 | |||
| 189 | // Persisted flow: toggle demo with vendor trial rules. |
||
| 190 | if (!$settingsManager->selectDemo($connectDemo)) { |
||
| 191 | $errorMsg = $plugin->get_lang('demoPeriodIsOver'); |
||
| 192 | self::displayError($errorMsg, $plugin->getConfigLink()); |
||
| 193 | } |
||
| 194 | |||
| 195 | // Validate connectivity if a custom server is set and demo is not used. |
||
| 196 | $docUrl = $settingsManager->getDocumentServerUrl(); |
||
| 197 | if (!empty($docUrl) && !$connectDemo) { |
||
| 198 | $httpClient = new OnlyofficeHttpClient(); |
||
| 199 | $jwtManager = new OnlyofficeJwtManager($settingsManager); |
||
| 200 | $requestService = new OnlyofficeAppRequests($settingsManager, $httpClient, $jwtManager); |
||
| 201 | |||
| 202 | // checkDocServiceUrl() returns [error|null, version|null] |
||
| 203 | [$error, $version] = $requestService->checkDocServiceUrl(); |
||
| 204 | if (!empty($error)) { |
||
| 205 | $versionStr = !empty($version) ? '(Version '.$version.')' : ''; |
||
| 206 | $errorMsg = $plugin->get_lang('connectionError').'('.$error.')'.$versionStr; |
||
| 207 | self::displayError($errorMsg); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | return $plugin; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Run a minimal self-test using the vendor SettingsManager + existing request service. |
||
| 216 | * |
||
| 217 | * @param OnlyofficeAppsettings $settings |
||
| 218 | * |
||
| 219 | * @return array{ok: bool, message: string} |
||
| 220 | */ |
||
| 221 | private static function runSelfTest(OnlyofficeAppsettings $settings): array |
||
| 222 | { |
||
| 223 | try { |
||
| 224 | $docUrl = $settings->getDocumentServerUrl(); |
||
| 225 | if (empty($docUrl) && !$settings->useDemo()) { |
||
| 226 | return [ |
||
| 227 | 'ok' => false, |
||
| 228 | 'message' => 'No Document Server URL set and demo mode is disabled.', |
||
| 229 | ]; |
||
| 230 | } |
||
| 231 | |||
| 232 | $httpClient = new OnlyofficeHttpClient(); |
||
| 233 | $jwtManager = new OnlyofficeJwtManager($settings); |
||
| 234 | $requestService = new OnlyofficeAppRequests($settings, $httpClient, $jwtManager); |
||
| 235 | |||
| 236 | [$error, $version] = $requestService->checkDocServiceUrl(); |
||
| 237 | |||
| 238 | if (!empty($error)) { |
||
| 239 | $versionStr = !empty($version) ? ' (Version '.$version.')' : ''; |
||
| 240 | return [ |
||
| 241 | 'ok' => false, |
||
| 242 | 'message' => 'checkDocServiceUrl() returned error: '.$error.$versionStr, |
||
| 243 | ]; |
||
| 244 | } |
||
| 245 | |||
| 246 | $healthUrl = method_exists($settings, 'getDocumentServerHealthcheckUrl') |
||
| 247 | ? $settings->getDocumentServerHealthcheckUrl() |
||
| 248 | : null; |
||
| 249 | |||
| 250 | $versionStr = !empty($version) ? 'Version '.$version : 'Version unknown'; |
||
| 251 | $extra = $healthUrl ? ' | Healthcheck: '.$healthUrl : ''; |
||
| 252 | |||
| 253 | return [ |
||
| 254 | 'ok' => true, |
||
| 255 | 'message' => 'Document Server is reachable. '.$versionStr.$extra, |
||
| 256 | ]; |
||
| 257 | } catch (\Throwable $e) { |
||
| 258 | return [ |
||
| 259 | 'ok' => false, |
||
| 260 | 'message' => 'Unexpected exception: '.$e->getMessage(), |
||
| 261 | ]; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Build an HTML template chunk from plugin templates directory. |
||
| 267 | * |
||
| 268 | * @param string $templateName |
||
| 269 | * @param array $params |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | private static function buildTemplate($templateName, $params = []) |
||
| 274 | { |
||
| 275 | // Template is used outside of full-page context; disable auto-wrapping. |
||
| 276 | $tpl = new Template('', false, false, false, false, false, false); |
||
| 277 | if (!empty($params)) { |
||
| 278 | foreach ($params as $key => $param) { |
||
| 279 | $tpl->assign($key, $param); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | return $tpl->fetch(self::ONLYOFFICE_LAYOUT_DIR.$templateName.'.tpl'); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Show an error flash and optionally redirect. |
||
| 288 | * |
||
| 289 | * @param string $errorMessage |
||
| 290 | * @param string|null $location |
||
| 291 | * |
||
| 292 | * @return void |
||
| 293 | */ |
||
| 294 | private static function displayError($errorMessage, $location = null) |
||
| 302 | } |
||
| 303 | } |
||
| 304 | } |
||
| 305 |