| Conditions | 17 |
| Paths | 216 |
| Total Lines | 93 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 210 | public static function getInfoMessage(bool $showCredentials=false): string |
||
| 211 | { |
||
| 212 | // Assuming a total width of 53 characters for each line |
||
| 213 | $lineWidth = 53; |
||
| 214 | |||
| 215 | $info = PHP_EOL . "┌────────────────────────────────────────────────┐"; |
||
| 216 | $info .= PHP_EOL . "│ │"; |
||
| 217 | $headerSpace = $lineWidth - 3 - 5; // 3 for "│ 🌟 MikoPBX - All services are fully loaded 🌟 " and 5 for " │" at the end |
||
| 218 | $headerLine = sprintf("│ %-{$headerSpace}s │", "🌟 MikoPBX - All services are fully loaded 🌟"); |
||
| 219 | $info .= PHP_EOL . $headerLine; |
||
| 220 | $info .= PHP_EOL . "│ │"; |
||
| 221 | $info .= PHP_EOL . "├────────────────────────────────────────────────┤"; |
||
| 222 | |||
| 223 | $addresses = [ |
||
| 224 | 'local' => [], |
||
| 225 | 'external' => [] |
||
| 226 | ]; |
||
| 227 | /** @var LanInterfaces $interface */ |
||
| 228 | $interfaces = LanInterfaces::find("disabled='0'"); |
||
| 229 | foreach ($interfaces as $interface) { |
||
| 230 | if (!empty($interface->ipaddr)) { |
||
| 231 | $addresses['local'][] = $interface->ipaddr; |
||
| 232 | } |
||
| 233 | if (!empty($interface->exthostname) && !in_array($interface->exthostname, $addresses['local'], true)) { |
||
| 234 | $addresses['external'][] = explode(':', $interface->exthostname)[0] ?? ''; |
||
| 235 | } |
||
| 236 | if (!empty($interface->extipaddr) && !in_array($interface->extipaddr, $addresses['local'], true)) { |
||
| 237 | $addresses['external'][] = explode(':', $interface->extipaddr)[0] ?? ''; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | unset($interfaces); |
||
| 241 | |||
| 242 | |||
| 243 | // Local network |
||
| 244 | $port = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_HTTPS_PORT); |
||
| 245 | $info .= PHP_EOL . "│ │"; |
||
| 246 | $headerSpace = $lineWidth - 13 - 1; // 13 for "│ 🌐 Web Interface Access 🌐 " and 1 for " │" |
||
| 247 | $headerLine = sprintf("│ %-{$headerSpace}s │", "🌐 Web Interface Access 🌐"); |
||
| 248 | $info .= PHP_EOL . $headerLine; |
||
| 249 | $info .= PHP_EOL . "│ │"; |
||
| 250 | $info .= PHP_EOL . "│ Local Network Address: │"; |
||
| 251 | |||
| 252 | $addressSpace = $lineWidth - 7 - 5; // 7 for "│ ➜ " and 5 for " │" at the end |
||
| 253 | foreach ($addresses['local'] as $address) { |
||
| 254 | if (empty($address)) { |
||
| 255 | continue; |
||
| 256 | } |
||
| 257 | $formattedAddress = $port === '443' ? "https://$address" : "https://$address:$port"; |
||
| 258 | // Use sprintf to format the string with padding to ensure constant length |
||
| 259 | $info .= PHP_EOL . sprintf("│ ➜ %-{$addressSpace}s │", $formattedAddress); |
||
| 260 | |||
| 261 | } |
||
| 262 | $info .= PHP_EOL . "│ │"; |
||
| 263 | |||
| 264 | // External web address info |
||
| 265 | if (!empty($addresses['external'])) { |
||
| 266 | $info .= PHP_EOL . "│ External Network Address: │"; |
||
| 267 | foreach ($addresses['external'] as $address) { |
||
| 268 | if (empty($address)) { |
||
| 269 | continue; |
||
| 270 | } |
||
| 271 | $formattedAddress = $port === '443' ? "https://$address" : "https://$address:$port"; |
||
| 272 | // Use sprintf to format the string with padding to ensure constant length |
||
| 273 | $info .= PHP_EOL . sprintf("│ ➜ %-{$addressSpace}s │", $formattedAddress); |
||
| 274 | |||
| 275 | } |
||
| 276 | $info .= PHP_EOL . "│ │"; |
||
| 277 | } |
||
| 278 | |||
| 279 | if ($showCredentials) { |
||
| 280 | // Default web user info |
||
| 281 | $cloudInstanceId = PbxSettings::getValueByKey(PbxSettingsConstants::CLOUD_INSTANCE_ID); |
||
| 282 | $webAdminPassword = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_ADMIN_PASSWORD); |
||
| 283 | $defaultPassword = PbxSettings::getDefaultArrayValues()[PbxSettingsConstants::WEB_ADMIN_PASSWORD]; |
||
| 284 | if ($cloudInstanceId === $webAdminPassword || $webAdminPassword === $defaultPassword) { |
||
| 285 | $adminUser = PbxSettings::getValueByKey(PbxSettingsConstants::WEB_ADMIN_LOGIN); |
||
| 286 | |||
| 287 | $credentialSpace = $lineWidth - 5 - 3; // 5 for "│ 🔑 Default Credentials: " and 3 for " │" |
||
| 288 | $credentialLine = sprintf("│ %-{$credentialSpace}s │", "🔑 Default web credentials:"); |
||
| 289 | $info .= PHP_EOL . $credentialLine; |
||
| 290 | // Login |
||
| 291 | $loginSpace = $lineWidth - 12 - 5; // 12 for "│ Login: " and 5 for " │" at the end |
||
| 292 | $loginLine = sprintf("│ Login: %-{$loginSpace}s │", $adminUser); // Format the login line |
||
| 293 | $info .= PHP_EOL . $loginLine; |
||
| 294 | |||
| 295 | // Password |
||
| 296 | $passwordSpace = $lineWidth - 15 - 5; // 15 for "│ Password: " and 5 for " │" at the end |
||
| 297 | $passwordLine = sprintf("│ Password: %-{$passwordSpace}s │", $cloudInstanceId); // Format the password line |
||
| 298 | $info .= PHP_EOL . $passwordLine; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | $info .= PHP_EOL . "└────────────────────────────────────────────────┘" . PHP_EOL . PHP_EOL; |
||
| 302 | return $info; |
||
| 303 | } |
||
| 304 | } |