Total Complexity | 43 |
Total Lines | 264 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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() |
||
31 | { |
||
32 | $this->mikoPBXConfig = new MikoPBXConfig(); |
||
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 |
||
170 | { |
||
171 | $datePath = Util::which('date'); |
||
172 | $db_tz = PbxSettings::getValueByKey('PBXTimezone'); |
||
173 | $origin_tz = ''; |
||
174 | if (file_exists('/etc/TZ')) { |
||
175 | $origin_tz = file_get_contents("/etc/TZ"); |
||
176 | } |
||
177 | if ($origin_tz !== $db_tz){ |
||
178 | self::timezoneConfigure(); |
||
179 | } |
||
180 | $origin_tz = $db_tz; |
||
181 | $origin_dtz = new DateTimeZone($origin_tz); |
||
182 | $remote_dtz = new DateTimeZone($remote_tz); |
||
183 | $origin_dt = new DateTime('now', $origin_dtz); |
||
184 | $remote_dt = new DateTime('now', $remote_dtz); |
||
185 | $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt); |
||
186 | $timeStamp = $timeStamp - $offset; |
||
187 | Util::mwExec("{$datePath} +%s -s @{$timeStamp}"); |
||
188 | // Для 1 января должно быть передано 1577829662 |
||
189 | // Установлено 1577818861 |
||
190 | |||
191 | return true; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Reboots the system after calling system_reboot_cleanup() |
||
196 | */ |
||
197 | public static function rebootSync(): void |
||
198 | { |
||
199 | $mikopbx_rebootPath = Util::which('mikopbx_reboot'); |
||
200 | Util::mwExec("{$mikopbx_rebootPath} > /dev/null 2>&1"); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Reboots the system after calling system_reboot_cleanup() |
||
205 | */ |
||
206 | public static function rebootSyncBg(): void |
||
207 | { |
||
208 | $mikopbx_rebootPath = Util::which('mikopbx_reboot'); |
||
209 | Util::mwExecBg("{$mikopbx_rebootPath} > /dev/null 2>&1"); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Shutdown the system |
||
214 | */ |
||
215 | public static function shutdown(): void |
||
216 | { |
||
217 | $shutdownPath = Util::which('shutdown'); |
||
218 | Util::mwExec("{$shutdownPath} > /dev/null 2>&1"); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Restart all workers in separate process, |
||
223 | * we use this method after module install or delete |
||
224 | */ |
||
225 | public static function restartAllWorkers(): void |
||
226 | { |
||
227 | $workerSafeScriptsPath = Util::getFilePathByClassName(WorkerSafeScriptsCore::class); |
||
228 | $phpPath = Util::which('php'); |
||
229 | $WorkerSafeScripts = "{$phpPath} -f {$workerSafeScriptsPath} restart > /dev/null 2> /dev/null"; |
||
230 | Util::mwExecBg($WorkerSafeScripts, '/dev/null', 1); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Populates /etc/TZ with an appropriate time zone |
||
235 | */ |
||
236 | public static function timezoneConfigure(): void |
||
237 | { |
||
238 | $timezone = PbxSettings::getValueByKey('PBXTimezone'); |
||
239 | if (file_exists('/etc/TZ')) { |
||
240 | unlink("/etc/TZ"); |
||
241 | } |
||
242 | if (file_exists('/etc/localtime')) { |
||
243 | unlink("/etc/localtime"); |
||
244 | } |
||
245 | if ($timezone) { |
||
246 | $zone_file = "/usr/share/zoneinfo/{$timezone}"; |
||
247 | if ( ! file_exists($zone_file)) { |
||
248 | return; |
||
249 | } |
||
250 | $cpPath = Util::which('cp'); |
||
251 | Util::mwExec("{$cpPath} {$zone_file} /etc/localtime"); |
||
252 | file_put_contents('/etc/TZ', $timezone); |
||
253 | putenv("TZ={$timezone}"); |
||
254 | Util::mwExec("export TZ;"); |
||
255 | |||
256 | PHPConf::phpTimeZoneConfigure(); |
||
257 | } |
||
258 | |||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Loads additional kernel modules |
||
263 | */ |
||
264 | public function loadKernelModules(): void |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Restart asterisk processor |
||
277 | */ |
||
278 | public function onAfterPbxStarted(): void |
||
279 | { |
||
280 | $additionalModules = $this->di->getShared('pbxConfModules'); |
||
281 | foreach ($additionalModules as $appClass) { |
||
282 | try { |
||
283 | /** @var \MikoPBX\Modules\Config\ConfigClass $appClass */ |
||
284 | $appClass->onAfterPbxStarted(); |
||
291 |