Total Complexity | 42 |
Total Lines | 253 |
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 |
||
23 | class System extends Di\Injectable |
||
24 | { |
||
25 | private MikoPBXConfig $mikoPBXConfig; |
||
26 | |||
27 | /** |
||
28 | * System constructor |
||
29 | */ |
||
30 | public function __construct() |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Returns logs dir |
||
37 | * |
||
38 | * @return string |
||
39 | */ |
||
40 | public static function getLogDir(): string |
||
41 | { |
||
42 | $di = Di::getDefault(); |
||
43 | if ($di !== null) { |
||
44 | return $di->getConfig()->path('core.logsDir'); |
||
45 | } |
||
46 | |||
47 | return '/var/log'; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Refresh networks configs and restarts network daemon |
||
52 | */ |
||
53 | public static function networkReload(): void |
||
54 | { |
||
55 | $network = new Network(); |
||
56 | $network->hostnameConfigure(); |
||
57 | $network->resolvConfGenerate(); |
||
58 | $network->loConfigure(); |
||
59 | $network->lanConfigure(); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Updates custom changes in config files |
||
64 | */ |
||
65 | public static function updateCustomFiles() |
||
66 | { |
||
67 | $actions = []; |
||
68 | /** @var \MikoPBX\Common\Models\CustomFiles $res_data */ |
||
69 | $res_data = CustomFiles::find("changed = '1'"); |
||
70 | foreach ($res_data as $file_data) { |
||
71 | // Always restart asterisk after any custom file change |
||
72 | $actions['asterisk_core_reload'] = 100; |
||
73 | $filename = basename($file_data->filepath); |
||
74 | switch ($filename) { |
||
75 | case 'manager.conf': |
||
76 | $actions['manager'] = 10; |
||
77 | break; |
||
78 | case 'musiconhold.conf': |
||
79 | $actions['musiconhold'] = 100; |
||
80 | break; |
||
81 | case 'modules.conf': |
||
82 | $actions['modules'] = 10; |
||
83 | break; |
||
84 | case 'http.conf': |
||
85 | $actions['manager'] = 10; // |
||
86 | break; |
||
87 | case 'root': // crontabs |
||
88 | $actions['cron'] = 10; |
||
89 | break; |
||
90 | case 'queues.conf': |
||
91 | $actions['queues'] = 10; |
||
92 | break; |
||
93 | case 'features.conf': |
||
94 | $actions['features'] = 10; |
||
95 | break; |
||
96 | case 'ntp.conf': |
||
97 | $actions['ntp'] = 100; |
||
98 | break; |
||
99 | case 'jail.local': // fail2ban |
||
100 | $actions['firewall'] = 100; |
||
101 | break; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | asort($actions); |
||
106 | self::invokeActions($actions); |
||
107 | foreach ($res_data as $file_data) { |
||
108 | /** @var \MikoPBX\Common\Models\CustomFiles $file_data */ |
||
109 | $file_data->writeAttribute("changed", '0'); |
||
110 | $file_data->save(); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Batch module restart |
||
116 | * |
||
117 | * @param $actions |
||
118 | * |
||
119 | */ |
||
120 | public static function invokeActions($actions): void |
||
121 | { |
||
122 | foreach ($actions as $action => $value) { |
||
123 | switch ($action) { |
||
124 | case 'manager': |
||
125 | PBX::managerReload(); |
||
126 | break; |
||
127 | case 'musiconhold': |
||
128 | PBX::musicOnHoldReload(); |
||
129 | break; |
||
130 | case 'modules': |
||
131 | PBX::modulesReload(); |
||
132 | break; |
||
133 | case 'cron': |
||
134 | $cron = new CronConf(); |
||
135 | $cron->reStart(); |
||
136 | break; |
||
137 | case 'queues': |
||
138 | QueueConf::queueReload(); |
||
139 | break; |
||
140 | case 'features': |
||
141 | PBX::managerReload(); // |
||
142 | break; |
||
143 | case 'ntp': |
||
144 | NTPConf::configure(); |
||
145 | break; |
||
146 | case 'firewall': |
||
147 | IptablesConf::reloadFirewall(); |
||
148 | break; |
||
149 | case 'asterisk_core_reload': |
||
150 | PBX::sipReload(); |
||
151 | PBX::iaxReload(); |
||
152 | PBX::dialplanReload(); |
||
153 | PBX::coreReload(); |
||
154 | break; |
||
155 | default: |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Setup system time |
||
162 | * |
||
163 | * @param int $timeStamp |
||
164 | * @param string $remote_tz |
||
165 | * |
||
166 | * @return bool |
||
167 | * @throws \Exception |
||
168 | */ |
||
169 | public static function setDate(int $timeStamp, string $remote_tz): bool |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Reboots the system after calling system_reboot_cleanup() |
||
196 | */ |
||
197 | public static function rebootSync(): void |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Reboots the system after calling system_reboot_cleanup() |
||
205 | */ |
||
206 | public static function rebootSyncBg(): void |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Shutdown the system |
||
214 | */ |
||
215 | public static function shutdown(): void |
||
219 | } |
||
220 | |||
221 | |||
222 | /** |
||
223 | * Populates /etc/TZ with an appropriate time zone |
||
224 | */ |
||
225 | public static function timezoneConfigure(): void |
||
226 | { |
||
227 | $timezone = PbxSettings::getValueByKey('PBXTimezone'); |
||
228 | if (file_exists('/etc/TZ')) { |
||
229 | unlink("/etc/TZ"); |
||
230 | } |
||
231 | if (file_exists('/etc/localtime')) { |
||
232 | unlink("/etc/localtime"); |
||
233 | } |
||
234 | if ($timezone) { |
||
235 | $zone_file = "/usr/share/zoneinfo/{$timezone}"; |
||
236 | if ( ! file_exists($zone_file)) { |
||
237 | return; |
||
238 | } |
||
239 | $cpPath = Util::which('cp'); |
||
240 | Processes::mwExec("{$cpPath} {$zone_file} /etc/localtime"); |
||
241 | file_put_contents('/etc/TZ', $timezone); |
||
242 | putenv("TZ={$timezone}"); |
||
243 | Processes::mwExec("export TZ;"); |
||
244 | |||
245 | PHPConf::phpTimeZoneConfigure(); |
||
246 | } |
||
247 | |||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Loads additional kernel modules |
||
252 | */ |
||
253 | public function loadKernelModules(): void |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Restart asterisk processor |
||
266 | */ |
||
267 | public function onAfterPbxStarted(): void |
||
276 | } |
||
277 | } |
||
280 |