Total Complexity | 43 |
Total Lines | 339 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Complex classes like AdvicesProcessor 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 AdvicesProcessor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class AdvicesProcessor extends Injectable |
||
41 | { |
||
42 | |||
43 | /** |
||
44 | * Processes Advices request |
||
45 | * |
||
46 | * @param array $request |
||
47 | * |
||
48 | * @return PBXApiResult |
||
49 | */ |
||
50 | public static function callBack(array $request): PBXApiResult |
||
63 | } |
||
64 | |||
65 | |||
66 | /** |
||
67 | * Makes list of notifications about system, firewall, passwords, wrong settings |
||
68 | * |
||
69 | * @return PBXApiResult |
||
70 | */ |
||
71 | private function getAdvicesAction(): PBXApiResult |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Check passwords quality |
||
134 | * |
||
135 | * @return array |
||
136 | * @noinspection PhpUnusedPrivateMethodInspection |
||
137 | */ |
||
138 | private function checkPasswords(): array |
||
139 | { |
||
140 | $messages = [ |
||
141 | 'warning' => [], |
||
142 | 'needUpdate' => [] |
||
143 | ]; |
||
144 | $arrOfDefaultValues = PbxSettings::getDefaultArrayValues(); |
||
145 | $fields = [ |
||
146 | 'WebAdminPassword' => [ |
||
147 | 'url' => 'general-settings/modify/#/passwords', |
||
148 | 'type' => 'gs_WebPasswordFieldName', |
||
149 | 'value'=> PbxSettings::getValueByKey('WebAdminPassword') |
||
150 | ], |
||
151 | 'SSHPassword' => [ |
||
152 | 'url' => 'general-settings/modify/#/ssh', |
||
153 | 'type' => 'gs_SshPasswordFieldName', |
||
154 | 'value'=> PbxSettings::getValueByKey('SSHPassword') |
||
155 | ], |
||
156 | ]; |
||
157 | if ($arrOfDefaultValues['WebAdminPassword'] === PbxSettings::getValueByKey('WebAdminPassword')) { |
||
158 | $messages['warning'][] = $this->translation->_( |
||
159 | 'adv_YouUseDefaultWebPassword', |
||
160 | ['url' => $this->url->get('general-settings/modify/#/passwords')] |
||
161 | ); |
||
162 | unset($fields['WebAdminPassword']); |
||
163 | $messages['needUpdate'][] = 'WebAdminPassword'; |
||
164 | } |
||
165 | if ($arrOfDefaultValues['SSHPassword'] === PbxSettings::getValueByKey('SSHPassword')) { |
||
166 | $messages['warning'][] = $this->translation->_( |
||
167 | 'adv_YouUseDefaultSSHPassword', |
||
168 | ['url' => $this->url->get('general-settings/modify/#/ssh')] |
||
169 | ); |
||
170 | unset($fields['SSHPassword']); |
||
171 | $messages['needUpdate'][] = 'SSHPassword'; |
||
172 | }elseif(PbxSettings::getValueByKey('SSHPasswordHash') !== md5_file('/etc/passwd')){ |
||
173 | $messages['warning'][] = $this->translation->_( |
||
174 | 'gs_SSHPPasswordCorrupt', |
||
175 | ['url' => $this->url->get('general-settings/modify/#/ssh')] |
||
176 | ); |
||
177 | } |
||
178 | |||
179 | $peersData = Sip::find([ |
||
180 | "type = 'peer' AND ( disabled <> '1')", |
||
181 | 'columns' => 'id,extension,secret'] |
||
182 | ); |
||
183 | foreach ($peersData as $peer){ |
||
184 | $fields[$peer['extension']] = [ |
||
185 | 'url' => '/admin-cabinet/extensions/modify/'.$peer['id'], |
||
186 | 'type' => 'gs_UserPasswordFieldName', |
||
187 | 'value'=> $peer['secret'] |
||
188 | ]; |
||
189 | } |
||
190 | |||
191 | $cloudInstanceId = PbxSettings::getValueByKey('CloudInstanceId'); |
||
192 | foreach ($fields as $key => $value){ |
||
193 | if($cloudInstanceId !== $value['value'] && !Util::isSimplePassword($value['value'])){ |
||
194 | continue; |
||
195 | } |
||
196 | |||
197 | if(in_array($key, ['WebAdminPassword', 'SSHPassword'], true)){ |
||
198 | $messages['needUpdate'][] = $key; |
||
199 | } |
||
200 | $messages['warning'][] = $this->translation->_( |
||
201 | 'adv_isSimplePassword', |
||
202 | [ |
||
203 | 'type' => $this->translation->_($value['type']), |
||
204 | 'url' => $this->url->get($value['url']) |
||
205 | ] |
||
206 | ); |
||
207 | } |
||
208 | return $messages; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Check passwords quality |
||
213 | * |
||
214 | * @return array |
||
215 | * @noinspection PhpUnusedPrivateMethodInspection |
||
216 | */ |
||
217 | private function checkCorruptedFiles(): array |
||
218 | { |
||
219 | $messages = []; |
||
220 | $files = Main::checkForCorruptedFiles(); |
||
221 | if (count($files) !== 0) { |
||
222 | $messages['warning'] = $this->translation->_('The integrity of the system is broken', ['url' => '']).'. '.$this->translation->_('systemBrokenComment', ['url' => '']); |
||
223 | } |
||
224 | |||
225 | return $messages; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Check firewall status |
||
230 | * |
||
231 | * @return array |
||
232 | * @noinspection PhpUnusedPrivateMethodInspection |
||
233 | */ |
||
234 | private function checkFirewalls(): array |
||
235 | { |
||
236 | $messages = []; |
||
237 | if (PbxSettings::getValueByKey('PBXFirewallEnabled') === '0') { |
||
238 | $messages['warning'] = $this->translation->_( |
||
239 | 'adv_FirewallDisabled', |
||
240 | ['url' => $this->url->get('firewall/index/')] |
||
241 | ); |
||
242 | } |
||
243 | if (NetworkFilters::count() === 0) { |
||
244 | $messages['warning'] = $this->translation->_( |
||
245 | 'adv_NetworksNotConfigured', |
||
246 | ['url' => $this->url->get('firewall/index/')] |
||
247 | ); |
||
248 | } |
||
249 | |||
250 | return $messages; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Check storage is mount and how many space available |
||
255 | * |
||
256 | * @return array |
||
257 | * |
||
258 | * @noinspection PhpUnusedPrivateMethodInspection |
||
259 | */ |
||
260 | private function checkStorage(): array |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Check new version PBX |
||
287 | * |
||
288 | * @return array |
||
289 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
290 | * @noinspection PhpUnusedPrivateMethodInspection |
||
291 | */ |
||
292 | private function checkUpdates(): array |
||
293 | { |
||
294 | $messages = []; |
||
295 | $PBXVersion = PbxSettings::getValueByKey('PBXVersion'); |
||
296 | |||
297 | $client = new GuzzleHttp\Client(); |
||
298 | $res = $client->request( |
||
299 | 'POST', |
||
300 | 'https://releases.mikopbx.com/releases/v1/mikopbx/ifNewReleaseAvailable', |
||
301 | [ |
||
302 | 'form_params' => [ |
||
303 | 'PBXVER' => $PBXVersion, |
||
304 | ], |
||
305 | ] |
||
306 | ); |
||
307 | if ($res->getStatusCode() !== 200) { |
||
308 | return []; |
||
309 | } |
||
310 | |||
311 | $answer = json_decode($res->getBody(), false); |
||
312 | if ($answer !== null && $answer->newVersionAvailable === true) { |
||
313 | $messages['info'] = $this->translation->_( |
||
314 | 'adv_AvailableNewVersionPBX', |
||
315 | [ |
||
316 | 'url' => $this->url->get('update/index/'), |
||
317 | 'ver' => $answer->version, |
||
318 | ] |
||
319 | ); |
||
320 | } |
||
321 | |||
322 | return $messages; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Check mikopbx license status |
||
327 | * |
||
328 | * @noinspection PhpUnusedPrivateMethodInspection |
||
329 | */ |
||
330 | private function checkRegistration(): array |
||
331 | { |
||
332 | $messages = []; |
||
333 | $licKey = PbxSettings::getValueByKey('PBXLicense'); |
||
334 | $language = PbxSettings::getValueByKey('WebAdminLanguage'); |
||
335 | |||
336 | if ( ! empty($licKey)) { |
||
337 | $checkBaseFeature = $this->license->featureAvailable(33); |
||
338 | if ($checkBaseFeature['success'] === false) { |
||
339 | if ($language === 'ru') { |
||
340 | $url = 'https://wiki.mikopbx.com/licensing#faq_chavo'; |
||
341 | } else { |
||
342 | $url = "https://wiki.mikopbx.com/{$language}:licensing#faq_chavo"; |
||
343 | } |
||
344 | $textError = (string)($checkBaseFeature['error']??''); |
||
345 | $messages['warning'] = $this->translation->_( |
||
346 | 'adv_ThisCopyHasLicensingTroubles', |
||
347 | [ |
||
348 | 'url' => $url, |
||
349 | 'error' => $this->license->translateLicenseErrorMessage($textError), |
||
350 | ] |
||
351 | ); |
||
352 | } |
||
353 | |||
354 | $licenseInfo = $this->license->getLicenseInfo($licKey); |
||
355 | if ($licenseInfo instanceof SimpleXMLElement) { |
||
356 | file_put_contents('/tmp/licenseInfo', json_encode($licenseInfo->attributes())); |
||
357 | } |
||
358 | } |
||
359 | |||
360 | return $messages; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Checks whether internet connection is available or not |
||
365 | * |
||
366 | * @return array |
||
367 | * @noinspection PhpUnusedPrivateMethodInspection |
||
368 | */ |
||
369 | private function isConnected(): array |
||
379 | } |
||
380 | } |