| Total Complexity | 40 |
| Total Lines | 326 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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 |
||
| 51 | { |
||
| 52 | $action = $request['action']; |
||
| 53 | if('getList' === $action){ |
||
| 54 | $proc = new self(); |
||
| 55 | $res = $proc->getAdvicesAction(); |
||
| 56 | }else{ |
||
| 57 | $res = new PBXApiResult(); |
||
| 58 | $res->processor = __METHOD__; |
||
| 59 | $res->messages[] = "Unknown action - {$action} in advicesCallBack"; |
||
| 60 | } |
||
| 61 | $res->function = $action; |
||
| 62 | return $res; |
||
| 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 |
||
| 72 | { |
||
| 73 | $res = new PBXApiResult(); |
||
| 74 | $res->processor = __METHOD__; |
||
| 75 | |||
| 76 | $arrMessages = []; |
||
| 77 | $arrAdvicesTypes = [ |
||
| 78 | ['type' => 'isConnected', 'cacheTime' => 15], |
||
| 79 | ['type' => 'checkCorruptedFiles', 'cacheTime' => 15], |
||
| 80 | ['type' => 'checkPasswords', 'cacheTime' => 15], |
||
| 81 | ['type' => 'checkFirewalls', 'cacheTime' => 15], |
||
| 82 | ['type' => 'checkStorage', 'cacheTime' => 120], |
||
| 83 | ['type' => 'checkUpdates', 'cacheTime' => 3600], |
||
| 84 | ['type' => 'checkRegistration', 'cacheTime' => 86400], |
||
| 85 | ]; |
||
| 86 | |||
| 87 | $managedCache = $this->getDI()->getShared(ManagedCacheProvider::SERVICE_NAME); |
||
| 88 | $language = PbxSettings::getValueByKey('WebAdminLanguage'); |
||
| 89 | |||
| 90 | foreach ($arrAdvicesTypes as $adviceType) { |
||
| 91 | $currentAdvice = $adviceType['type']; |
||
| 92 | $cacheTime = $adviceType['cacheTime']; |
||
| 93 | $cacheKey = 'AdvicesProcessor:getAdvicesAction:'.$currentAdvice; |
||
| 94 | if ($managedCache->has($cacheKey)) { |
||
| 95 | $oldResult = json_decode($managedCache->get($cacheKey), true, 512, JSON_THROW_ON_ERROR); |
||
| 96 | if ($language === $oldResult['LastLanguage']) { |
||
| 97 | $arrMessages[] = $oldResult['LastMessage']; |
||
| 98 | continue; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | $newResult = $this->$currentAdvice(); |
||
| 102 | if ( ! empty($newResult)) { |
||
| 103 | $arrMessages[] = $newResult; |
||
| 104 | } |
||
| 105 | $managedCache->set( |
||
| 106 | $cacheKey, |
||
| 107 | json_encode([ |
||
| 108 | 'LastLanguage' => $language, |
||
| 109 | 'LastMessage' => $newResult, |
||
| 110 | ], JSON_THROW_ON_ERROR), |
||
| 111 | $cacheTime |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | $res->success = true; |
||
| 115 | $result = []; |
||
| 116 | foreach ($arrMessages as $message) { |
||
| 117 | foreach ($message as $key => $value) { |
||
| 118 | if(is_array($value)){ |
||
| 119 | $result[$key] = array_merge($result[$key], $value); |
||
| 120 | }elseif ( ! empty($value)) { |
||
| 121 | $result[$key][] = $value; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | $res->data['advices'] = $result; |
||
| 126 | |||
| 127 | return $res; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Check passwords quality |
||
| 132 | * |
||
| 133 | * @return array |
||
| 134 | * @noinspection PhpUnusedPrivateMethodInspection |
||
| 135 | */ |
||
| 136 | private function checkPasswords(): array |
||
| 137 | { |
||
| 138 | $messages = []; |
||
| 139 | $arrOfDefaultValues = PbxSettings::getDefaultArrayValues(); |
||
| 140 | $fields = [ |
||
| 141 | 'WebAdminPassword' => [ |
||
| 142 | 'url' => 'general-settings/modify/#/passwords', |
||
| 143 | 'type' => 'gs_WebPasswordFieldName', |
||
| 144 | 'value'=> $arrOfDefaultValues['WebAdminPassword'] |
||
| 145 | ], |
||
| 146 | 'SSHPassword' => [ |
||
| 147 | 'url' => 'general-settings/modify/#/ssh', |
||
| 148 | 'type' => 'gs_SshPasswordFieldName', |
||
| 149 | 'value'=> $arrOfDefaultValues['SSHPassword'] |
||
| 150 | ], |
||
| 151 | ]; |
||
| 152 | if ($arrOfDefaultValues['WebAdminPassword'] === PbxSettings::getValueByKey('WebAdminPassword')) { |
||
| 153 | $messages['warning'][] = $this->translation->_( |
||
| 154 | 'adv_YouUseDefaultWebPassword', |
||
| 155 | ['url' => $this->url->get('general-settings/modify/#/passwords')] |
||
| 156 | ); |
||
| 157 | unset($fields['WebAdminPassword']); |
||
| 158 | } |
||
| 159 | if ($arrOfDefaultValues['SSHPassword'] === PbxSettings::getValueByKey('SSHPassword')) { |
||
| 160 | $messages['warning'][] = $this->translation->_( |
||
| 161 | 'adv_YouUseDefaultSSHPassword', |
||
| 162 | ['url' => $this->url->get('general-settings/modify/#/ssh')] |
||
| 163 | ); |
||
| 164 | unset($fields['SSHPassword']); |
||
| 165 | }elseif(PbxSettings::getValueByKey('SSHPasswordHash') !== md5_file('/etc/passwd')){ |
||
| 166 | $messages['warning'][] = $this->translation->_( |
||
| 167 | 'gs_SSHPPasswordCorrupt', |
||
| 168 | ['url' => $this->url->get('general-settings/modify/#/ssh')] |
||
| 169 | ); |
||
| 170 | } |
||
| 171 | |||
| 172 | $peersData = Sip::find([ |
||
| 173 | "type = 'peer' AND ( disabled <> '1')", |
||
| 174 | 'columns' => 'id,extension,secret'] |
||
| 175 | ); |
||
| 176 | foreach ($peersData as $peer){ |
||
| 177 | $fields[$peer['extension']] = [ |
||
| 178 | 'url' => '/admin-cabinet/extensions/modify/'.$peer['id'], |
||
| 179 | 'type' => 'gs_UserPasswordFieldName', |
||
| 180 | 'value'=> $peer['secret'] |
||
| 181 | ]; |
||
| 182 | } |
||
| 183 | foreach ($fields as $value){ |
||
| 184 | if(!Util::isSimplePassword($value['value'])){ |
||
| 185 | continue; |
||
| 186 | } |
||
| 187 | $messages['warning'][] = $this->translation->_( |
||
| 188 | 'adv_isSimplePassword', |
||
| 189 | [ |
||
| 190 | 'type' => $this->translation->_($value['type']), |
||
| 191 | 'url' => $this->url->get($value['url']) |
||
| 192 | ] |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | return $messages; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Check passwords quality |
||
| 200 | * |
||
| 201 | * @return array |
||
| 202 | * @noinspection PhpUnusedPrivateMethodInspection |
||
| 203 | */ |
||
| 204 | private function checkCorruptedFiles(): array |
||
| 205 | { |
||
| 206 | $messages = []; |
||
| 207 | $files = Main::checkForCorruptedFiles(); |
||
| 208 | if (count($files) !== 0) { |
||
| 209 | $messages['warning'] = $this->translation->_('The integrity of the system is broken', ['url' => '']).'. '.$this->translation->_('systemBrokenComment', ['url' => '']); |
||
| 210 | } |
||
| 211 | |||
| 212 | return $messages; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Check firewall status |
||
| 217 | * |
||
| 218 | * @return array |
||
| 219 | * @noinspection PhpUnusedPrivateMethodInspection |
||
| 220 | */ |
||
| 221 | private function checkFirewalls(): array |
||
| 222 | { |
||
| 223 | $messages = []; |
||
| 224 | if (PbxSettings::getValueByKey('PBXFirewallEnabled') === '0') { |
||
| 225 | $messages['warning'] = $this->translation->_( |
||
| 226 | 'adv_FirewallDisabled', |
||
| 227 | ['url' => $this->url->get('firewall/index/')] |
||
| 228 | ); |
||
| 229 | } |
||
| 230 | if (NetworkFilters::count() === 0) { |
||
| 231 | $messages['warning'] = $this->translation->_( |
||
| 232 | 'adv_NetworksNotConfigured', |
||
| 233 | ['url' => $this->url->get('firewall/index/')] |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | |||
| 237 | return $messages; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Check storage is mount and how many space available |
||
| 242 | * |
||
| 243 | * @return array |
||
| 244 | * |
||
| 245 | * @noinspection PhpUnusedPrivateMethodInspection |
||
| 246 | */ |
||
| 247 | private function checkStorage(): array |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Check new version PBX |
||
| 274 | * |
||
| 275 | * @return array |
||
| 276 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
| 277 | * @noinspection PhpUnusedPrivateMethodInspection |
||
| 278 | */ |
||
| 279 | private function checkUpdates(): array |
||
| 280 | { |
||
| 281 | $messages = []; |
||
| 282 | $PBXVersion = PbxSettings::getValueByKey('PBXVersion'); |
||
| 283 | |||
| 284 | $client = new GuzzleHttp\Client(); |
||
| 285 | $res = $client->request( |
||
| 286 | 'POST', |
||
| 287 | 'https://releases.mikopbx.com/releases/v1/mikopbx/ifNewReleaseAvailable', |
||
| 288 | [ |
||
| 289 | 'form_params' => [ |
||
| 290 | 'PBXVER' => $PBXVersion, |
||
| 291 | ], |
||
| 292 | ] |
||
| 293 | ); |
||
| 294 | if ($res->getStatusCode() !== 200) { |
||
| 295 | return []; |
||
| 296 | } |
||
| 297 | |||
| 298 | $answer = json_decode($res->getBody(), false); |
||
| 299 | if ($answer !== null && $answer->newVersionAvailable === true) { |
||
| 300 | $messages['info'] = $this->translation->_( |
||
| 301 | 'adv_AvailableNewVersionPBX', |
||
| 302 | [ |
||
| 303 | 'url' => $this->url->get('update/index/'), |
||
| 304 | 'ver' => $answer->version, |
||
| 305 | ] |
||
| 306 | ); |
||
| 307 | } |
||
| 308 | |||
| 309 | return $messages; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Check mikopbx license status |
||
| 314 | * |
||
| 315 | * @noinspection PhpUnusedPrivateMethodInspection |
||
| 316 | */ |
||
| 317 | private function checkRegistration(): array |
||
| 318 | { |
||
| 319 | $messages = []; |
||
| 320 | $licKey = PbxSettings::getValueByKey('PBXLicense'); |
||
| 321 | $language = PbxSettings::getValueByKey('WebAdminLanguage'); |
||
| 322 | |||
| 323 | if ( ! empty($licKey)) { |
||
| 324 | $checkBaseFeature = $this->license->featureAvailable(33); |
||
| 325 | if ($checkBaseFeature['success'] === false) { |
||
| 326 | if ($language === 'ru') { |
||
| 327 | $url = 'https://wiki.mikopbx.com/licensing#faq_chavo'; |
||
| 328 | } else { |
||
| 329 | $url = "https://wiki.mikopbx.com/{$language}:licensing#faq_chavo"; |
||
| 330 | } |
||
| 331 | $textError = (string)($checkBaseFeature['error']??''); |
||
| 332 | $messages['warning'] = $this->translation->_( |
||
| 333 | 'adv_ThisCopyHasLicensingTroubles', |
||
| 334 | [ |
||
| 335 | 'url' => $url, |
||
| 336 | 'error' => $this->license->translateLicenseErrorMessage($textError), |
||
| 337 | ] |
||
| 338 | ); |
||
| 339 | } |
||
| 340 | |||
| 341 | $licenseInfo = $this->license->getLicenseInfo($licKey); |
||
| 342 | if ($licenseInfo instanceof SimpleXMLElement) { |
||
| 343 | file_put_contents('/tmp/licenseInfo', json_encode($licenseInfo->attributes())); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | return $messages; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Checks whether internet connection is available or not |
||
| 352 | * |
||
| 353 | * @return array |
||
| 354 | * @noinspection PhpUnusedPrivateMethodInspection |
||
| 355 | */ |
||
| 356 | private function isConnected(): array |
||
| 366 | } |
||
| 367 | } |