| Total Complexity | 64 |
| Total Lines | 606 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like HandlerController 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 HandlerController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class HandlerController extends TrapsController |
||
| 17 | { |
||
| 18 | |||
| 19 | /** index : list existing rules |
||
| 20 | */ |
||
| 21 | public function indexAction() |
||
| 34 | |||
| 35 | //$this->displayExitError('Handler/indexAction','Not implemented'); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** test_rule : test a rule |
||
| 39 | */ |
||
| 40 | public function testruleAction() |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Setup default veiw values for add action |
||
| 63 | */ |
||
| 64 | private function add_setup_vars() |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Setup new handler display from existing trap |
||
| 95 | * @param integer $trapid : trap id in DB |
||
| 96 | */ |
||
| 97 | private function add_from_existing($trapid) |
||
| 98 | { |
||
| 99 | /********** Setup from existing trap ***************/ |
||
| 100 | // Get the full trap info |
||
| 101 | $trapDetail=$this->getTrapDetail($trapid); |
||
| 102 | |||
| 103 | $hostfilter=$trapDetail->source_ip; |
||
| 104 | |||
| 105 | // Get host |
||
| 106 | try |
||
| 107 | { |
||
| 108 | $hosts=$this->getHostByIP($hostfilter); |
||
| 109 | } |
||
| 110 | catch (Exception $e) |
||
| 111 | { |
||
| 112 | $this->displayExitError('Add handler : get host by IP/Name ',$e->getMessage()); |
||
| 113 | } |
||
| 114 | |||
| 115 | |||
| 116 | // if one unique host found -> put id text input |
||
| 117 | if (count($hosts)==1) { |
||
| 118 | $this->view->hostname=$hosts[0]->name; |
||
| 119 | //$hostid=$hosts[0]->id; |
||
| 120 | // Tell JS to get services when page is loaded |
||
| 121 | $this->view->serviceGet=true; |
||
| 122 | |||
| 123 | } |
||
| 124 | else |
||
| 125 | { |
||
| 126 | foreach($hosts as $key=>$val) |
||
| 127 | { |
||
| 128 | array_push($this->view->hostlist,$hosts[$key]->name); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | // set up trap oid and objects received by the trap |
||
| 133 | |||
| 134 | $this->view->mainoid=$trapDetail->trap_oid; |
||
| 135 | if ($trapDetail->trap_name_mib != null) |
||
| 136 | { |
||
| 137 | $this->view->mib=$trapDetail->trap_name_mib; |
||
| 138 | $this->view->name=$trapDetail->trap_name; |
||
| 139 | $this->view->trapListForMIB=$this->getMIB() |
||
| 140 | ->getTrapList($trapDetail->trap_name_mib); |
||
| 141 | } |
||
| 142 | |||
| 143 | // Get all objects that can be in trap from MIB |
||
| 144 | $allObjects=$this->getMIB()->getObjectList($trapDetail->trap_oid); |
||
| 145 | // Get all objects in current Trap |
||
| 146 | $currentTrapObjects=$this->getTrapobjects($trapid); |
||
| 147 | $oid_index=1; |
||
| 148 | foreach ($currentTrapObjects as $key => $val) |
||
| 149 | { |
||
| 150 | $currentObjectType='Unknown'; |
||
| 151 | $currentObjectTypeEnum='Unknown'; |
||
| 152 | if (isset($allObjects[$val->oid]['type'])) |
||
| 153 | { |
||
| 154 | $currentObjectType=$allObjects[$val->oid]['type']; |
||
| 155 | $currentObjectTypeEnum=$allObjects[$val->oid]['type_enum']; |
||
| 156 | } |
||
| 157 | $currentObject=array( |
||
| 158 | $oid_index, |
||
| 159 | $val->oid, |
||
| 160 | $val->oid_name_mib, |
||
| 161 | $val->oid_name, |
||
| 162 | $val->value, |
||
| 163 | $currentObjectType, |
||
| 164 | $currentObjectTypeEnum |
||
| 165 | ); |
||
| 166 | $oid_index++; |
||
| 167 | array_push($this->view->objectList,$currentObject); |
||
| 168 | // set currrent object to null in allObjects |
||
| 169 | if (isset($allObjects[$val->oid])) |
||
| 170 | { |
||
| 171 | $allObjects[$val->oid]=null; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | if ($allObjects!=null) // in case trap doesn't have objects or is not resolved |
||
| 175 | { |
||
| 176 | foreach ($allObjects as $key => $val) |
||
| 177 | { |
||
| 178 | if ($val==null) { continue; } |
||
| 179 | array_push($this->view->objectList, array( |
||
| 180 | $oid_index, |
||
| 181 | $key, |
||
| 182 | $allObjects[$key]['mib'], |
||
| 183 | $allObjects[$key]['name'], |
||
| 184 | '', |
||
| 185 | $allObjects[$key]['type'], |
||
| 186 | $allObjects[$key]['type_enum'] |
||
| 187 | )); |
||
| 188 | $oid_index++; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | // Add a simple display |
||
| 193 | $this->view->display='Trap '.$trapDetail->trap_name.' received'; |
||
| 194 | $this->view->create_basic_rule=true; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Check if host & service still exists or set warning message |
||
| 199 | * @param object $ruleDetail |
||
| 200 | */ |
||
| 201 | private function add_check_host_exists($ruleDetail) |
||
| 202 | { |
||
| 203 | // Check if hostname still exists |
||
| 204 | $host_get=$this->getHostByName($this->view->hostname); |
||
| 205 | |||
| 206 | if (count($host_get)==0) |
||
| 207 | { |
||
| 208 | $this->view->warning_message='Host '.$this->view->hostname. ' doesn\'t exists anymore'; |
||
| 209 | $this->view->serviceGet=false; |
||
| 210 | } |
||
| 211 | else |
||
| 212 | { |
||
| 213 | // Tell JS to get services when page is loaded |
||
| 214 | $this->view->serviceGet=true; |
||
| 215 | // get service id for form to set : |
||
| 216 | $serviceID=$this->getServiceIDByName($this->view->hostname,$ruleDetail->service_name); |
||
| 217 | if (count($serviceID) ==0) |
||
| 218 | { |
||
| 219 | $this->view->warning_message=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
||
| 220 | } |
||
| 221 | else |
||
| 222 | { |
||
| 223 | $this->view->serviceSet=$serviceID[0]->id; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Check if hostgroup & service still exists or set warning message |
||
| 230 | * @param object $ruleDetail |
||
| 231 | */ |
||
| 232 | private function add_check_hostgroup_exists($ruleDetail) |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Create object list array with all OIDs in rule & display |
||
| 265 | * Replace in rule & display by $<n>$ |
||
| 266 | * @param string $display |
||
| 267 | * @param string $rule |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | private function add_create_trap_object_list(&$display, &$rule) |
||
| 271 | { |
||
| 272 | $curObjectList=array(); |
||
| 273 | $index=1; |
||
| 274 | // check in display & rule for : OID(<oid>) |
||
| 275 | $matches=array(); |
||
| 276 | while ( preg_match('/_OID\(([\.0-9]+)\)/',$display,$matches) || |
||
| 277 | preg_match('/_OID\(([\.0-9]+)\)/',$rule,$matches)) |
||
| 278 | { |
||
| 279 | $curOid=$matches[1]; |
||
| 280 | if (($object=$this->getMIB()->translateOID($curOid)) != null) |
||
| 281 | { |
||
| 282 | array_push($curObjectList, array( |
||
| 283 | $index, |
||
| 284 | $curOid, |
||
| 285 | $object['mib'], |
||
| 286 | $object['name'], |
||
| 287 | '', |
||
| 288 | $object['type'], |
||
| 289 | $object['type_enum'] |
||
| 290 | )); |
||
| 291 | } |
||
| 292 | else |
||
| 293 | { |
||
| 294 | array_push($curObjectList, array( |
||
| 295 | $index, |
||
| 296 | $curOid, |
||
| 297 | 'not found', |
||
| 298 | 'not found', |
||
| 299 | '', |
||
| 300 | 'not found' |
||
| 301 | )); |
||
| 302 | } |
||
| 303 | $display=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$display); |
||
| 304 | $rule=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$rule); |
||
| 305 | $index++; |
||
| 306 | } |
||
| 307 | return $curObjectList; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** Add a handler |
||
| 311 | * Get params fromid : setup from existing trap (id of trap table) |
||
| 312 | * Get param ruleid : edit from existing handler (id of rule table) |
||
| 313 | */ |
||
| 314 | public function addAction() |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | /** Validate form and output message to user |
||
| 393 | * @param in postdata |
||
| 394 | * @return string status : OK / <Message> |
||
| 395 | **/ |
||
| 396 | protected function handlerformAction() |
||
| 516 | |||
| 517 | } |
||
| 518 | |||
| 519 | /** Get trap detail by trapid. |
||
| 520 | * @param integer $trapid : id of trap in received table |
||
| 521 | * @return array (objects) |
||
| 522 | */ |
||
| 523 | protected function getTrapDetail($trapid) |
||
| 550 | |||
| 551 | } |
||
| 552 | |||
| 553 | /** Get trap objects |
||
| 554 | * @param integer $trapid : trap id |
||
| 555 | * @return array : full column in db of trap id |
||
| 556 | */ |
||
| 557 | protected function getTrapobjects($trapid) |
||
| 558 | { |
||
| 559 | if (!preg_match('/^[0-9]+$/',$trapid)) { throw new Exception('Invalid id'); } |
||
| 560 | $queryArrayData=$this->getModuleConfig()->trapDataDetailQuery(); |
||
| 561 | |||
| 562 | $db = $this->getDb()->getConnection(); |
||
| 563 | // *************** Get object data |
||
| 564 | // extract columns and titles; |
||
| 565 | $data_elmts=NULL; |
||
| 566 | foreach ($queryArrayData as $key => $val) { |
||
| 567 | $data_elmts[$key]=$val[1]; |
||
| 568 | } |
||
| 569 | try |
||
| 570 | { |
||
| 571 | $query = $db->select() |
||
| 572 | ->from($this->moduleConfig->getTrapDataTableName(),$data_elmts) |
||
| 573 | ->where('trap_id=?',$trapid); |
||
| 574 | $trapDetail=$db->fetchAll($query); |
||
| 575 | // if ( $trapDetail == null ) throw new Exception('No traps was found with id = '.$trapid); |
||
| 576 | } |
||
| 577 | catch (Exception $e) |
||
| 578 | { |
||
| 579 | $this->displayExitError('Add handler : get trap data detail : ',$e->getMessage()); |
||
| 580 | return array(); |
||
| 581 | } |
||
| 582 | |||
| 583 | return $trapDetail; |
||
| 584 | } |
||
| 585 | |||
| 586 | /** Get rule detail by ruleid. |
||
| 587 | * @param integer $ruleid int id of rule in rule table |
||
| 588 | * @return array : column objects in db |
||
| 589 | */ |
||
| 590 | protected function getRuleDetail($ruleid) |
||
| 591 | { |
||
| 592 | if (!preg_match('/^[0-9]+$/',$ruleid)) { throw new Exception('Invalid id'); } |
||
| 593 | $queryArray=$this->getModuleConfig()->ruleDetailQuery(); |
||
| 594 | |||
| 595 | $db = $this->getDb()->getConnection(); |
||
| 596 | // *************** Get main data |
||
| 597 | try |
||
| 598 | { |
||
| 599 | $query = $db->select() |
||
| 600 | ->from($this->getModuleConfig()->getTrapRuleName(),$queryArray) |
||
| 601 | ->where('id=?',$ruleid); |
||
| 602 | $ruleDetail=$db->fetchRow($query); |
||
| 603 | if ( $ruleDetail == null ) throw new Exception('No rule was found with id = '.$ruleid); |
||
| 604 | } |
||
| 605 | catch (Exception $e) |
||
| 606 | { |
||
| 607 | $this->displayExitError('Update handler : get rule detail',$e->getMessage()); |
||
| 608 | return array(); |
||
| 609 | } |
||
| 610 | |||
| 611 | return $ruleDetail; |
||
| 612 | |||
| 613 | } |
||
| 614 | |||
| 615 | /** Setup tabs for rules |
||
| 616 | */ |
||
| 617 | protected function prepareTabs() |
||
| 622 | ); |
||
| 623 | } |
||
| 624 | |||
| 625 | } |