| Total Complexity | 50 |
| Total Lines | 284 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 33 | class System extends Di\Injectable |
||
| 34 | { |
||
| 35 | private MikoPBXConfig $mikoPBXConfig; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * System constructor |
||
| 39 | */ |
||
| 40 | public function __construct() |
||
| 41 | { |
||
| 42 | $this->mikoPBXConfig = new MikoPBXConfig(); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Returns logs dir |
||
| 47 | * |
||
| 48 | * @return string |
||
| 49 | */ |
||
| 50 | public static function getLogDir(): string |
||
| 51 | { |
||
| 52 | $di = Di::getDefault(); |
||
| 53 | if ($di !== null) { |
||
| 54 | return $di->getConfig()->path('core.logsDir'); |
||
| 55 | } |
||
| 56 | |||
| 57 | return '/var/log'; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Refresh networks configs and restarts network daemon |
||
| 62 | */ |
||
| 63 | public static function networkReload(): void |
||
| 64 | { |
||
| 65 | $network = new Network(); |
||
| 66 | $network->hostnameConfigure(); |
||
| 67 | $network->resolvConfGenerate(); |
||
| 68 | $network->loConfigure(); |
||
| 69 | $network->lanConfigure(); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Updates custom changes in config files |
||
| 74 | */ |
||
| 75 | public static function updateCustomFiles():void |
||
| 76 | { |
||
| 77 | $actions = []; |
||
| 78 | /** @var CustomFiles $res_data */ |
||
| 79 | $res_data = CustomFiles::find("changed = '1'"); |
||
| 80 | foreach ($res_data as $file_data) { |
||
| 81 | // Always restart asterisk after any custom file change |
||
| 82 | $actions['asterisk_core_reload'] = 100; |
||
| 83 | $filename = basename($file_data->filepath); |
||
| 84 | switch ($filename) { |
||
| 85 | case 'manager.conf': |
||
| 86 | $actions['manager'] = 10; |
||
| 87 | break; |
||
| 88 | case 'musiconhold.conf': |
||
| 89 | $actions['musiconhold'] = 100; |
||
| 90 | break; |
||
| 91 | case 'modules.conf': |
||
| 92 | $actions['modules'] = 10; |
||
| 93 | break; |
||
| 94 | case 'http.conf': |
||
| 95 | $actions['manager'] = 10; // |
||
| 96 | break; |
||
| 97 | case 'root': // crontabs |
||
| 98 | $actions['cron'] = 10; |
||
| 99 | break; |
||
| 100 | case 'queues.conf': |
||
| 101 | $actions['queues'] = 10; |
||
| 102 | break; |
||
| 103 | case 'features.conf': |
||
| 104 | $actions['features'] = 10; |
||
| 105 | break; |
||
| 106 | case 'ntp.conf': |
||
| 107 | $actions['ntp'] = 100; |
||
| 108 | break; |
||
| 109 | case 'rtp.conf': |
||
| 110 | $actions['rtp'] = 10; |
||
| 111 | break; |
||
| 112 | case 'static-routes': |
||
| 113 | case 'openvpn.ovpn': |
||
| 114 | $actions['network'] = 100; |
||
| 115 | break; |
||
| 116 | case 'jail.local': // fail2ban |
||
| 117 | $actions['firewall'] = 100; |
||
| 118 | break; |
||
| 119 | default: |
||
| 120 | break; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | asort($actions); |
||
| 124 | self::invokeActions($actions); |
||
| 125 | foreach ($res_data as $file_data) { |
||
| 126 | /** @var CustomFiles $file_data */ |
||
| 127 | $file_data->writeAttribute("changed", '0'); |
||
| 128 | $file_data->save(); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Batch module restart |
||
| 134 | * |
||
| 135 | * @param $actions |
||
| 136 | * |
||
| 137 | */ |
||
| 138 | public static function invokeActions($actions): void |
||
| 139 | { |
||
| 140 | foreach ($actions as $action => $value) { |
||
| 141 | switch ($action) { |
||
| 142 | case 'manager': |
||
| 143 | PBX::managerReload(); |
||
| 144 | break; |
||
| 145 | case 'musiconhold': |
||
| 146 | PBX::musicOnHoldReload(); |
||
| 147 | break; |
||
| 148 | case 'rtp': |
||
| 149 | PBX::rtpReload(); |
||
| 150 | break; |
||
| 151 | case 'modules': |
||
| 152 | PBX::modulesReload(); |
||
| 153 | break; |
||
| 154 | case 'cron': |
||
| 155 | $cron = new CronConf(); |
||
| 156 | $cron->reStart(); |
||
| 157 | break; |
||
| 158 | case 'queues': |
||
| 159 | QueueConf::queueReload(); |
||
| 160 | break; |
||
| 161 | case 'features': |
||
| 162 | PBX::managerReload(); // |
||
| 163 | break; |
||
| 164 | case 'ntp': |
||
| 165 | NTPConf::configure(); |
||
| 166 | break; |
||
| 167 | case 'firewall': |
||
| 168 | IptablesConf::reloadFirewall(); |
||
| 169 | break; |
||
| 170 | case 'network': |
||
| 171 | self::networkReload(); |
||
| 172 | break; |
||
| 173 | case 'asterisk_core_reload': |
||
| 174 | PBX::sipReload(); |
||
| 175 | PBX::iaxReload(); |
||
| 176 | PBX::dialplanReload(); |
||
| 177 | PBX::coreReload(); |
||
| 178 | break; |
||
| 179 | default: |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Setup system time |
||
| 186 | * |
||
| 187 | * @param int $timeStamp |
||
| 188 | * @param string $remote_tz |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | * @throws \Exception |
||
| 192 | */ |
||
| 193 | public static function setDate(int $timeStamp, string $remote_tz): bool |
||
| 194 | { |
||
| 195 | $datePath = Util::which('date'); |
||
| 196 | $db_tz = PbxSettings::getValueByKey('PBXTimezone'); |
||
| 197 | $origin_tz = ''; |
||
| 198 | if (file_exists('/etc/TZ')) { |
||
| 199 | $origin_tz = file_get_contents("/etc/TZ"); |
||
| 200 | } |
||
| 201 | if ($origin_tz !== $db_tz){ |
||
| 202 | self::timezoneConfigure(); |
||
| 203 | } |
||
| 204 | $origin_tz = $db_tz; |
||
| 205 | $origin_dtz = new DateTimeZone($origin_tz); |
||
| 206 | $remote_dtz = new DateTimeZone($remote_tz); |
||
| 207 | $origin_dt = new DateTime('now', $origin_dtz); |
||
| 208 | $remote_dt = new DateTime('now', $remote_dtz); |
||
| 209 | $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt); |
||
| 210 | $timeStamp = $timeStamp - $offset; |
||
| 211 | Processes::mwExec("{$datePath} +%s -s @{$timeStamp}"); |
||
| 212 | // Для 1 января должно быть передано 1577829662 |
||
| 213 | // Установлено 1577818861 |
||
| 214 | |||
| 215 | return true; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Reboots the system after calling system_reboot_cleanup() |
||
| 220 | */ |
||
| 221 | public static function rebootSync(): void |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Reboots the system after calling system_reboot_cleanup() |
||
| 229 | */ |
||
| 230 | public static function rebootSyncBg(): void |
||
| 231 | { |
||
| 232 | $mikopbx_rebootPath = Util::which('mikopbx_reboot'); |
||
| 233 | Processes::mwExecBg("{$mikopbx_rebootPath} > /dev/null 2>&1"); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Shutdown the system |
||
| 238 | */ |
||
| 239 | public static function shutdown(): void |
||
| 240 | { |
||
| 241 | $shutdownPath = Util::which('shutdown'); |
||
| 242 | Processes::mwExec("{$shutdownPath} > /dev/null 2>&1"); |
||
| 243 | } |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * Populates /etc/TZ with an appropriate time zone |
||
| 248 | */ |
||
| 249 | public static function timezoneConfigure(): void |
||
| 250 | { |
||
| 251 | $timezone = PbxSettings::getValueByKey('PBXTimezone'); |
||
| 252 | if (file_exists('/etc/TZ')) { |
||
| 253 | unlink("/etc/TZ"); |
||
| 254 | } |
||
| 255 | if (file_exists('/etc/localtime')) { |
||
| 256 | unlink("/etc/localtime"); |
||
| 257 | } |
||
| 258 | if ($timezone) { |
||
| 259 | $zone_file = "/usr/share/zoneinfo/{$timezone}"; |
||
| 260 | if ( ! file_exists($zone_file)) { |
||
| 261 | return; |
||
| 262 | } |
||
| 263 | $cpPath = Util::which('cp'); |
||
| 264 | Processes::mwExec("{$cpPath} {$zone_file} /etc/localtime"); |
||
| 265 | file_put_contents('/etc/TZ', $timezone); |
||
| 266 | putenv("TZ={$timezone}"); |
||
| 267 | Processes::mwExec("export TZ;"); |
||
| 268 | |||
| 269 | PHPConf::phpTimeZoneConfigure(); |
||
| 270 | } |
||
| 271 | |||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Loads additional kernel modules |
||
| 276 | */ |
||
| 277 | public function loadKernelModules(): bool |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Вычисляет хэш сертификатов SSL и распаковывает их из ca-certificates.crt. |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | public static function sslRehash(): void |
||
| 320 |