| Total Complexity | 61 |
| Total Lines | 377 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like System 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 System, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class System extends Di\Injectable |
||
| 46 | { |
||
| 47 | private MikoPBXConfig $mikoPBXConfig; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * System constructor - Instantiates MikoPBXConfig. |
||
| 51 | */ |
||
| 52 | public function __construct() |
||
| 53 | { |
||
| 54 | $this->mikoPBXConfig = new MikoPBXConfig(); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Is the configuration default? |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | public function isDefaultConf():bool |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Trying to restore the backup |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | public function tryRestoreConf():void |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns the directory where logs are stored. |
||
| 125 | * |
||
| 126 | * @return string - Directory path where logs are stored. |
||
| 127 | */ |
||
| 128 | public static function getLogDir(): string |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Updates custom changes in config files |
||
| 141 | * |
||
| 142 | * @return void |
||
| 143 | */ |
||
| 144 | public static function updateCustomFiles():void |
||
| 145 | { |
||
| 146 | $actions = []; |
||
| 147 | |||
| 148 | // Find all custom files marked as changed |
||
| 149 | /** @var CustomFiles $res_data */ |
||
| 150 | $res_data = CustomFiles::find("changed = '1'"); |
||
| 151 | |||
| 152 | // Process each changed file |
||
| 153 | foreach ($res_data as $file_data) { |
||
| 154 | // Always restart asterisk after any custom file change |
||
| 155 | $actions['asterisk_core_reload'] = 100; |
||
| 156 | $filename = basename($file_data->filepath); |
||
| 157 | |||
| 158 | // Process based on file name |
||
| 159 | switch ($filename) { |
||
| 160 | // Set actions based on the name of the changed file |
||
| 161 | case 'manager.conf': |
||
| 162 | $actions['manager'] = 10; |
||
| 163 | break; |
||
| 164 | case 'musiconhold.conf': |
||
| 165 | $actions['musiconhold'] = 100; |
||
| 166 | break; |
||
| 167 | case 'modules.conf': |
||
| 168 | $actions['modules'] = 10; |
||
| 169 | break; |
||
| 170 | case 'http.conf': |
||
| 171 | $actions['manager'] = 10; // |
||
| 172 | break; |
||
| 173 | case 'hep.conf': |
||
| 174 | $actions['hep'] = 10; // |
||
| 175 | break; |
||
| 176 | case 'root': // crontabs |
||
| 177 | $actions['cron'] = 10; |
||
| 178 | break; |
||
| 179 | case 'queues.conf': |
||
| 180 | $actions['queues'] = 10; |
||
| 181 | break; |
||
| 182 | case 'features.conf': |
||
| 183 | $actions['features'] = 10; |
||
| 184 | break; |
||
| 185 | case 'ntp.conf': |
||
| 186 | $actions['ntp'] = 100; |
||
| 187 | break; |
||
| 188 | case 'ooh323.conf': |
||
| 189 | $actions['h323'] = 100; |
||
| 190 | break; |
||
| 191 | case 'rtp.conf': |
||
| 192 | $actions['rtp'] = 10; |
||
| 193 | break; |
||
| 194 | case 'static-routes': |
||
| 195 | case 'openvpn.ovpn': |
||
| 196 | $actions['network'] = 100; |
||
| 197 | break; |
||
| 198 | case 'firewall_additional': |
||
| 199 | case 'jail.local': |
||
| 200 | $actions['firewall'] = 100; |
||
| 201 | break; |
||
| 202 | default: |
||
| 203 | break; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | // Sort actions and invoke them |
||
| 208 | asort($actions); |
||
| 209 | self::invokeActions($actions); |
||
| 210 | |||
| 211 | // After actions are invoked, reset the changed status and save the file data |
||
| 212 | foreach ($res_data as $file_data) { |
||
| 213 | /** @var CustomFiles $file_data */ |
||
| 214 | $file_data->writeAttribute("changed", '0'); |
||
| 215 | $file_data->save(); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Restart modules or services based on the provided actions. |
||
| 221 | * |
||
| 222 | * @param array $actions - The actions to be performed. |
||
| 223 | * |
||
| 224 | * @return void |
||
| 225 | */ |
||
| 226 | public static function invokeActions(array $actions): void |
||
| 227 | { |
||
| 228 | |||
| 229 | // Process each action |
||
| 230 | foreach ($actions as $action => $value) { |
||
| 231 | // Restart modules or services based on action |
||
| 232 | switch ($action) { |
||
| 233 | case 'manager': |
||
| 234 | PBX::managerReload(); |
||
| 235 | break; |
||
| 236 | case 'musiconhold': |
||
| 237 | PBX::musicOnHoldReload(); |
||
| 238 | break; |
||
| 239 | case 'rtp': |
||
| 240 | PBX::rtpReload(); |
||
| 241 | break; |
||
| 242 | case 'modules': |
||
| 243 | PBX::modulesReload(); |
||
| 244 | break; |
||
| 245 | case 'cron': |
||
| 246 | $cron = new CronConf(); |
||
| 247 | $cron->reStart(); |
||
| 248 | break; |
||
| 249 | case 'queues': |
||
| 250 | QueueConf::queueReload(); |
||
| 251 | break; |
||
| 252 | case 'features': |
||
| 253 | PBX::managerReload(); // |
||
| 254 | break; |
||
| 255 | case 'ntp': |
||
| 256 | NTPConf::configure(); |
||
| 257 | break; |
||
| 258 | case 'firewall': |
||
| 259 | IptablesConf::reloadFirewall(); |
||
| 260 | break; |
||
| 261 | case 'hep': |
||
| 262 | HepConf::reload(); |
||
| 263 | break; |
||
| 264 | case 'h323': |
||
| 265 | H323Conf::reload(); |
||
| 266 | break; |
||
| 267 | case 'network': |
||
| 268 | self::networkReload(); |
||
|
|
|||
| 269 | break; |
||
| 270 | case 'asterisk_core_reload': |
||
| 271 | PBX::sipReload(); |
||
| 272 | PBX::iaxReload(); |
||
| 273 | PBX::dialplanReload(); |
||
| 274 | PBX::coreReload(); |
||
| 275 | break; |
||
| 276 | default: |
||
| 277 | } |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Sets the system date and time based on timestamp and timezone. |
||
| 283 | * |
||
| 284 | * @param int $timeStamp - Unix timestamp. |
||
| 285 | * @param string $remote_tz - Timezone string. |
||
| 286 | * |
||
| 287 | * @return bool |
||
| 288 | * @throws \Exception |
||
| 289 | */ |
||
| 290 | public static function setDate(int $timeStamp, string $remote_tz): bool |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Reboots the system after calling system_reboot_cleanup() |
||
| 325 | * |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | public static function rebootSync(): void |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Reboots the system after calling system_reboot_cleanup() |
||
| 336 | */ |
||
| 337 | public static function rebootSyncBg(): void |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Shutdown the system. |
||
| 345 | */ |
||
| 346 | public static function shutdown(): void |
||
| 347 | { |
||
| 348 | $shutdownPath = Util::which('shutdown'); |
||
| 349 | Processes::mwExec("{$shutdownPath} > /dev/null 2>&1"); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Configures the system timezone according to the PBXTimezone setting. |
||
| 354 | * |
||
| 355 | * @return void |
||
| 356 | */ |
||
| 357 | public static function timezoneConfigure(): void |
||
| 358 | { |
||
| 359 | |||
| 360 | // Get the timezone setting from the database |
||
| 361 | $timezone = PbxSettings::getValueByKey(PbxSettingsConstants::PBX_TIMEZONE); |
||
| 362 | |||
| 363 | // If /etc/TZ or /etc/localtime exist, delete them |
||
| 364 | if (file_exists('/etc/TZ')) { |
||
| 365 | unlink("/etc/TZ"); |
||
| 366 | } |
||
| 367 | if (file_exists('/etc/localtime')) { |
||
| 368 | unlink("/etc/localtime"); |
||
| 369 | } |
||
| 370 | |||
| 371 | // If a timezone is set, configure it |
||
| 372 | if ($timezone) { |
||
| 373 | |||
| 374 | // The path to the zone file |
||
| 375 | $zone_file = "/usr/share/zoneinfo/{$timezone}"; |
||
| 376 | |||
| 377 | // If the zone file exists, copy it to /etc/localtime |
||
| 378 | if ( ! file_exists($zone_file)) { |
||
| 379 | return; |
||
| 380 | } |
||
| 381 | $cpPath = Util::which('cp'); |
||
| 382 | Processes::mwExec("{$cpPath} {$zone_file} /etc/localtime"); |
||
| 383 | |||
| 384 | // Write the timezone to /etc/TZ and set the TZ environment variable |
||
| 385 | file_put_contents('/etc/TZ', $timezone); |
||
| 386 | putenv("TZ={$timezone}"); |
||
| 387 | |||
| 388 | // Execute the export TZ command and configure PHP's timezone |
||
| 389 | Processes::mwExec("export TZ;"); |
||
| 390 | PHPConf::phpTimeZoneConfigure(); |
||
| 391 | } |
||
| 392 | |||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Calculate the hash of SSL certificates and extract them from ca-certificates.crt. |
||
| 397 | * |
||
| 398 | * @return void |
||
| 399 | */ |
||
| 400 | public static function sslRehash(): void |
||
| 422 | } |
||
| 423 | } |
||
| 424 | } |
||
| 425 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.