| Conditions | 50 |
| Paths | 16105 |
| Total Lines | 129 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 117 | public function getSetting($settingName) |
||
| 118 | { |
||
| 119 | try { |
||
| 120 | $em = Database::getManager(); |
||
| 121 | $repo = $em->getRepository(PluginEntity::class); |
||
| 122 | |||
| 123 | // Find plugin entity (handle common casings) |
||
| 124 | $pluginEntity = $repo->findOneBy(['title' => 'Onlyoffice']) |
||
| 125 | ?: $repo->findOneBy(['title' => 'OnlyOffice']); |
||
| 126 | |||
| 127 | // Load configuration array for current access URL (null-safe) |
||
| 128 | $configuration = null; |
||
| 129 | if ($pluginEntity) { |
||
| 130 | $currentUrl = Container::getAccessUrlUtil()->getCurrent(); |
||
| 131 | $rel = $pluginEntity->getConfigurationsByAccessUrl($currentUrl); // might be null |
||
| 132 | $configuration = $rel ? $rel->getConfiguration() : null; // might be null |
||
| 133 | } |
||
| 134 | |||
| 135 | // 1) Runtime overrides take precedence (posted form values) |
||
| 136 | if (null !== $this->newSettings) { |
||
| 137 | // Exact key |
||
| 138 | if (array_key_exists($settingName, $this->newSettings)) { |
||
| 139 | $val = $this->newSettings[$settingName]; |
||
| 140 | if ($this->isSettingUrl($settingName) && is_string($val)) { |
||
| 141 | $val = $this->processUrl($val); |
||
| 142 | } |
||
| 143 | if ($val !== '' && $val !== null) { |
||
| 144 | return $val; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | // Allow prefix-less variant (e.g. 'document_server_url' when setting is 'Onlyoffice_document_server_url') |
||
| 149 | $prefix = $this->plugin->get_name(); |
||
| 150 | if (0 === strpos((string) $settingName, $prefix.'_')) { |
||
| 151 | $stripped = substr($settingName, strlen($prefix) + 1); |
||
| 152 | if (array_key_exists($stripped, $this->newSettings)) { |
||
| 153 | $val = $this->newSettings[$stripped]; |
||
| 154 | if ($this->isSettingUrl($stripped) && is_string($val)) { |
||
| 155 | $val = $this->processUrl($val); |
||
| 156 | } |
||
| 157 | if ($val !== '' && $val !== null) { |
||
| 158 | return $val; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | // Try alternate key mappings (legacy ↔ short names) |
||
| 164 | foreach ($this->getAlternateKeys($settingName) as $alt) { |
||
| 165 | if (array_key_exists($alt, $this->newSettings)) { |
||
| 166 | $val = $this->newSettings[$alt]; |
||
| 167 | if ($this->isSettingUrl($alt) && is_string($val)) { |
||
| 168 | $val = $this->processUrl($val); |
||
| 169 | } |
||
| 170 | if ($val !== '' && $val !== null) { |
||
| 171 | return $val; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | // 2) Persisted configuration in access_url_rel_plugin |
||
| 178 | if (is_array($configuration)) { |
||
| 179 | // Exact key |
||
| 180 | if (array_key_exists($settingName, $configuration)) { |
||
| 181 | $val = $configuration[$settingName]; |
||
| 182 | if ($this->isSettingUrl($settingName) && is_string($val)) { |
||
| 183 | $val = $this->processUrl($val); |
||
| 184 | } |
||
| 185 | if ($val !== '' && $val !== null) { |
||
| 186 | return $val; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | // Alternate keys |
||
| 190 | foreach ($this->getAlternateKeys($settingName) as $alt) { |
||
| 191 | if (array_key_exists($alt, $configuration)) { |
||
| 192 | $val = $configuration[$alt]; |
||
| 193 | if ($this->isSettingUrl($alt) && is_string($val)) { |
||
| 194 | $val = $this->processUrl($val); |
||
| 195 | } |
||
| 196 | if ($val !== '' && $val !== null) { |
||
| 197 | return $val; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | // 3) Special cases / defaults |
||
| 204 | switch ($settingName) { |
||
| 205 | case $this->jwtHeader: |
||
| 206 | // Legacy platform aggregated setting (array by plugin name) |
||
| 207 | $settings = api_get_setting($settingName); |
||
| 208 | $val = is_array($settings) && array_key_exists($this->plugin->get_name(), $settings) |
||
| 209 | ? $settings[$this->plugin->get_name()] |
||
| 210 | : null; |
||
| 211 | return $val ?: 'Authorization'; |
||
| 212 | |||
| 213 | case $this->documentServerInternalUrl: |
||
| 214 | // Legacy platform aggregated setting (array by plugin name) |
||
| 215 | $settings = api_get_setting($settingName); |
||
| 216 | $val = is_array($settings) ? ($settings[$this->plugin->get_name()] ?? null) : null; |
||
| 217 | return $val; |
||
| 218 | |||
| 219 | case $this->useDemoName: |
||
| 220 | // Explicit fallback from per-URL configuration |
||
| 221 | return (is_array($configuration) && array_key_exists($settingName, $configuration)) |
||
| 222 | ? $configuration[$settingName] |
||
| 223 | : null; |
||
| 224 | |||
| 225 | // Note: $this->jwtPrefix may be declared by the parent SDK class. |
||
| 226 | case $this->jwtPrefix: |
||
| 227 | return 'Bearer '; |
||
| 228 | } |
||
| 229 | |||
| 230 | // 4) Legacy plugin storage as last resort |
||
| 231 | if (!empty($this->plugin) && method_exists($this->plugin, 'get')) { |
||
| 232 | $val = $this->plugin->get($settingName); |
||
| 233 | if ($val !== '' && $val !== null) { |
||
| 234 | return $val; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | // 5) Global configuration fallback |
||
| 239 | $val = api_get_configuration_value($settingName); |
||
| 240 | return $val !== false ? $val : null; |
||
| 241 | |||
| 242 | } catch (\Throwable $e) { |
||
| 243 | // Log and return null to avoid fatal errors on admin UI |
||
| 244 | error_log('[OnlyOffice] getSetting error: '.$e->getMessage()); |
||
| 245 | return null; |
||
| 246 | } |
||
| 386 |