| Total Complexity | 49 |
| Total Lines | 420 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like StatusController 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 StatusController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class StatusController extends TrapsController |
||
| 16 | { |
||
| 17 | public function indexAction() |
||
| 18 | { |
||
| 19 | $this->prepareTabs()->activate('status'); |
||
| 20 | |||
| 21 | /************ Trapdb ***********/ |
||
| 22 | try |
||
| 23 | { |
||
| 24 | $dbConn = $this->getUIDatabase()->getDbConn(); |
||
| 25 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
| 26 | $query = $dbConn->select()->from( |
||
| 27 | $this->getModuleConfig()->getTrapTableName(), |
||
| 28 | array('COUNT(*)') |
||
| 29 | ); |
||
| 30 | $this->view->trap_count=$dbConn->fetchOne($query); |
||
| 31 | $query = $dbConn->select()->from( |
||
| 32 | $this->getModuleConfig()->getTrapDataTableName(), |
||
| 33 | array('COUNT(*)') |
||
| 34 | ); |
||
| 35 | $this->view->trap_object_count=$dbConn->fetchOne($query); |
||
| 36 | $query = $dbConn->select()->from( |
||
| 37 | $this->getModuleConfig()->getTrapRuleName(), |
||
| 38 | array('COUNT(*)') |
||
| 39 | ); |
||
| 40 | $this->view->rule_count=$dbConn->fetchOne($query); |
||
| 41 | |||
| 42 | $this->view->trap_days_delete=$this->getUIDatabase()->getDBConfigValue('db_remove_days'); |
||
| 43 | |||
| 44 | } |
||
| 45 | catch (Exception $e) |
||
| 46 | { |
||
| 47 | $this->displayExitError('status',$e->getMessage()); |
||
| 48 | } |
||
| 49 | |||
| 50 | /*************** Log destination *******************/ |
||
| 51 | |||
| 52 | try |
||
| 53 | { |
||
| 54 | $this->view->currentLogDestination=$this->getUIDatabase()->getDBConfigValue('log_destination'); |
||
| 55 | $this->view->logDestinations=$this->getModuleConfig()->getLogDestinations(); |
||
| 56 | $this->view->currentLogFile=$this->getUIDatabase()->getDBConfigValue('log_file'); |
||
| 57 | $this->view->logLevels=$this->getModuleConfig()->getlogLevels(); |
||
| 58 | $this->view->currentLogLevel=$this->getUIDatabase()->getDBConfigValue('log_level'); |
||
| 59 | } |
||
| 60 | catch (Exception $e) |
||
| 61 | { |
||
| 62 | $this->displayExitError('status',$e->getMessage()); |
||
| 63 | } |
||
| 64 | |||
| 65 | /*************** SNMP configuration ****************/ |
||
| 66 | try |
||
| 67 | { |
||
| 68 | $this->view->useSnmpTrapAddess= ( $this->getUIDatabase()->getDBConfigValue('use_SnmpTrapAddess') == 1 ) ? TRUE : FALSE; |
||
| 69 | $this->view->SnmpTrapAddressOID=$this->getUIDatabase()->getDBConfigValue('SnmpTrapAddess_oid'); |
||
| 70 | $this->view->SnmpTrapAddressOIDDefault = ($this->view->SnmpTrapAddressOID == $this->getModuleConfig()->getDBConfigDefaults()['SnmpTrapAddess_oid'] ) ? TRUE : FALSE; |
||
| 71 | |||
| 72 | } |
||
| 73 | catch (Exception $e) |
||
| 74 | { |
||
| 75 | $this->displayExitError('status',$e->getMessage()); |
||
| 76 | } |
||
| 77 | |||
| 78 | } |
||
| 79 | |||
| 80 | /** Mib management |
||
| 81 | * Post param : action=update_mib_db : update mib database |
||
| 82 | * Post param : ation=check_update : check if mib update is finished |
||
| 83 | * File post : mibfile -> save mib file |
||
| 84 | */ |
||
| 85 | public function mibAction() |
||
| 231 | //$this->view->form= new Form('upload-form'); |
||
| 232 | |||
| 233 | |||
| 234 | } |
||
| 235 | |||
| 236 | /** UI options */ |
||
| 237 | public function uimgtAction() |
||
| 238 | { |
||
| 239 | $this->prepareTabs()->activate('uimgt'); |
||
| 240 | |||
| 241 | $this->view->setError=''; |
||
| 242 | $this->view->setOKMsg=''; |
||
| 243 | |||
| 244 | //max_rows=25&row_update=update |
||
| 245 | if ( $this->getRequest()->getParam('max_rows',NULL) !== NULL ) |
||
| 246 | { |
||
| 247 | $maxRows = $this->getRequest()->getParam('max_rows'); |
||
| 248 | if (!preg_match('/^[0-9]+$/', $maxRows) || $maxRows < 1) |
||
| 249 | { |
||
| 250 | $this->view->setError='Max rows must be a number'; |
||
| 251 | } |
||
| 252 | else |
||
| 253 | { |
||
| 254 | $this->setitemListDisplay($maxRows); |
||
| 255 | $this->view->setOKMsg='Set max rows to ' . $maxRows; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | if ( $this->getRequest()->getParam('add_category',NULL) !== NULL ) |
||
| 260 | { |
||
| 261 | $addCat = $this->getRequest()->getParam('add_category'); |
||
| 262 | $this->addHandlersCategory($addCat); |
||
| 263 | } |
||
| 264 | |||
| 265 | if ( $this->getRequest()->getPost('type',NULL) !== NULL ) |
||
| 266 | { |
||
| 267 | $type = $this->getRequest()->getPost('type',NULL); |
||
| 268 | $index = $this->getRequest()->getPost('index',NULL); |
||
| 269 | $newname = $this->getRequest()->getPost('newname',NULL); |
||
| 270 | |||
| 271 | if (!preg_match('/^[0-9]+$/', $index) || $index < 1) |
||
| 272 | $this->_helper->json(array('status'=>'Bad index')); |
||
| 273 | |||
| 274 | switch ($type) |
||
| 275 | { |
||
| 276 | case 'delete': |
||
| 277 | $this->delHandlersCategory($index); |
||
| 278 | $this->_helper->json(array('status'=>'OK')); |
||
| 279 | return; |
||
| 280 | break; |
||
|
|
|||
| 281 | case 'rename': |
||
| 282 | $this->renameHandlersCategory($index, $newname); |
||
| 283 | $this->_helper->json(array('status'=>'OK')); |
||
| 284 | return; |
||
| 285 | break; |
||
| 286 | default: |
||
| 287 | $this->_helper->json(array('status'=>'Unknwon command')); |
||
| 288 | return; |
||
| 289 | break; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | $this->view->maxRows = $this->itemListDisplay(); |
||
| 294 | |||
| 295 | $this->view->categories = $this->getHandlersCategory(); |
||
| 296 | |||
| 297 | |||
| 298 | |||
| 299 | } |
||
| 300 | |||
| 301 | /** Create services and templates |
||
| 302 | * Create template for trap service |
||
| 303 | * |
||
| 304 | */ |
||
| 305 | public function servicesAction() |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Plugins display and activation |
||
| 341 | */ |
||
| 342 | public function pluginsAction() |
||
| 384 | } |
||
| 385 | |||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * For testing functions |
||
| 390 | */ |
||
| 391 | public function debugAction() |
||
| 392 | { |
||
| 393 | $this->view->answer='No answer'; |
||
| 394 | |||
| 395 | $postData=$this->getRequest()->getPost(); |
||
| 396 | if (isset($postData['input1'])) |
||
| 397 | { |
||
| 398 | $input1 = $postData['input1']; |
||
| 399 | $input2 = $postData['input2']; |
||
| 400 | $input3 = $postData['input3']; |
||
| 401 | |||
| 402 | //$this->view->answer=$input1 . '/' . $input2 . '/' . $input3; |
||
| 403 | try { |
||
| 404 | $API = $this->getIdoConn(); |
||
| 405 | //$hosts = $API->getHostByIP($input1); |
||
| 406 | $hosts = $API->getHostsIPByHostGroup($input1); |
||
| 407 | $this->view->answer = print_r($hosts,true); |
||
| 408 | |||
| 409 | } catch (Exception $e) |
||
| 410 | { |
||
| 411 | $this->view->answer = "Exception : " . print_r($e->getMessage()); |
||
| 412 | } |
||
| 413 | |||
| 414 | } |
||
| 415 | |||
| 416 | } |
||
| 417 | |||
| 418 | protected function prepareTabs() |
||
| 435 | ); |
||
| 436 | } |
||
| 459 |
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.