Total Complexity | 78 |
Total Lines | 691 |
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 |
||
17 | class HandlerController extends TrapsController |
||
18 | { |
||
19 | |||
20 | /** index : list existing rules |
||
21 | */ |
||
22 | public function indexAction() |
||
23 | { |
||
24 | $this->checkReadPermission(); |
||
25 | $this->prepareTabs()->activate('status'); |
||
26 | |||
27 | $dbConn = $this->getUIDatabase()->getDb(); |
||
28 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
29 | |||
30 | $handlerTable = new HandlerTable( |
||
31 | $this->moduleConfig->getTrapRuleName(), |
||
32 | $this->moduleConfig->getHandlerListTitles(), |
||
33 | $this->moduleConfig->getHandlerListDisplayColumns(), |
||
34 | $this->moduleConfig->getHandlerColumns(), |
||
35 | $dbConn->getConnection(), |
||
36 | $this->view, |
||
37 | $this->moduleConfig->urlPath()); |
||
38 | |||
39 | $handlerTable->setMaxPerPage($this->itemListDisplay()); |
||
|
|||
40 | |||
41 | $handlerTable->setMibloader($this->getMIB()); |
||
42 | |||
43 | $getVars = $this->getRequest()->getParams(); |
||
44 | $handlerTable->getParams($getVars); |
||
45 | |||
46 | if ($handlerTable->isOrderSet() == FALSE) |
||
47 | { // Id no order set order and grouping to categories |
||
48 | $handlerTable->setGrouping('rule_type'); |
||
49 | $handlerTable->setCategoriesArray($this->getHandlersCategory()); |
||
50 | $handlerTable->setOrder(array('rule_type'=>'DESC')); |
||
51 | |||
52 | } |
||
53 | |||
54 | $this->view->handlerTable = $handlerTable; |
||
55 | |||
56 | |||
57 | // TODO : Obsolete remove after new table validation. |
||
58 | |||
59 | /** |
||
60 | $this->getHandlerListTable()->setConnection($dbConn); |
||
61 | $this->getHandlerListTable()->setMibloader($this->getMIB()); |
||
62 | // Apply pagination limits |
||
63 | $this->view->table=$this->applyPaginationLimits($this->getHandlerListTable(),$this->getModuleConfig()->itemListDisplay()); |
||
64 | |||
65 | // Set Filter |
||
66 | $this->view->filterEditor = $this->getHandlerListTable()->getFilterEditor($this->getRequest()); |
||
67 | |||
68 | //$this->displayExitError('Handler/indexAction','Not implemented'); |
||
69 | */ |
||
70 | } |
||
71 | |||
72 | /** test_rule : test a rule |
||
73 | */ |
||
74 | public function testruleAction() |
||
75 | { |
||
76 | $this->checkReadPermission(); |
||
77 | $this->getTabs()->add('get',array( |
||
78 | 'active' => true, |
||
79 | 'label' => $this->translate('Test Rule'), |
||
80 | 'url' => Url::fromRequest() |
||
81 | )); |
||
82 | |||
83 | |||
84 | if ($this->params->get('rule') !== null) |
||
85 | { |
||
86 | $this->view->rule= $this->params->get('rule'); |
||
87 | } |
||
88 | else |
||
89 | { |
||
90 | $this->view->rule=''; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Setup default veiw values for add action |
||
96 | */ |
||
97 | private function add_setup_vars() |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Setup new handler display from existing trap |
||
128 | * @param integer $trapid : trap id in DB |
||
129 | */ |
||
130 | private function add_from_existing($trapid) |
||
131 | { |
||
132 | /********** Setup from existing trap ***************/ |
||
133 | // Get the full trap info |
||
134 | $trapDetail=$this->getTrapDetail($trapid); |
||
135 | |||
136 | $hostfilter=$trapDetail->source_ip; |
||
137 | |||
138 | // Get host |
||
139 | try |
||
140 | { |
||
141 | $hosts=$this->getIdoConn()->getHostByIP($hostfilter); |
||
142 | } |
||
143 | catch (Exception $e) |
||
144 | { |
||
145 | $this->displayExitError('Add handler : get host by IP/Name ',$e->getMessage()); |
||
146 | } |
||
147 | |||
148 | |||
149 | // if one unique host found -> put id text input |
||
150 | if (count($hosts)==1) { |
||
151 | $this->view->hostname=$hosts[0]->name; |
||
152 | //$hostid=$hosts[0]->id; |
||
153 | // Tell JS to get services when page is loaded |
||
154 | $this->view->serviceGet=true; |
||
155 | |||
156 | } |
||
157 | else |
||
158 | { |
||
159 | foreach($hosts as $key=>$val) |
||
160 | { |
||
161 | array_push($this->view->hostlist,$hosts[$key]->name); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | // set up trap oid and objects received by the trap |
||
166 | |||
167 | $this->view->mainoid=$trapDetail->trap_oid; |
||
168 | if ($trapDetail->trap_name_mib != null) |
||
169 | { |
||
170 | $this->view->mib=$trapDetail->trap_name_mib; |
||
171 | $this->view->name=$trapDetail->trap_name; |
||
172 | $this->view->trapListForMIB=$this->getMIB() |
||
173 | ->getTrapList($trapDetail->trap_name_mib); |
||
174 | } |
||
175 | |||
176 | // Get all objects that can be in trap from MIB |
||
177 | $allObjects=$this->getMIB()->getObjectList($trapDetail->trap_oid); |
||
178 | // Get all objects in current Trap |
||
179 | $currentTrapObjects=$this->getTrapobjects($trapid); |
||
180 | $oid_index=1; |
||
181 | foreach ($currentTrapObjects as $key => $val) |
||
182 | { |
||
183 | $currentObjectType='Unknown'; |
||
184 | $currentObjectTypeEnum='Unknown'; |
||
185 | if (isset($allObjects[$val->oid]['type'])) |
||
186 | { |
||
187 | $currentObjectType=$allObjects[$val->oid]['type']; |
||
188 | $currentObjectTypeEnum=$allObjects[$val->oid]['type_enum']; |
||
189 | } |
||
190 | $currentObject=array( |
||
191 | $oid_index, |
||
192 | $val->oid, |
||
193 | $val->oid_name_mib, |
||
194 | $val->oid_name, |
||
195 | $val->value, |
||
196 | $currentObjectType, |
||
197 | $currentObjectTypeEnum |
||
198 | ); |
||
199 | $oid_index++; |
||
200 | array_push($this->view->objectList,$currentObject); |
||
201 | // set currrent object to null in allObjects |
||
202 | if (isset($allObjects[$val->oid])) |
||
203 | { |
||
204 | $allObjects[$val->oid]=null; |
||
205 | } |
||
206 | } |
||
207 | if ($allObjects!=null) // in case trap doesn't have objects or is not resolved |
||
208 | { |
||
209 | foreach ($allObjects as $key => $val) |
||
210 | { |
||
211 | if ($val==null) { continue; } |
||
212 | array_push($this->view->objectList, array( |
||
213 | $oid_index, |
||
214 | $key, |
||
215 | $allObjects[$key]['mib'], |
||
216 | $allObjects[$key]['name'], |
||
217 | '', |
||
218 | $allObjects[$key]['type'], |
||
219 | $allObjects[$key]['type_enum'] |
||
220 | )); |
||
221 | $oid_index++; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | // Add a simple display |
||
226 | $this->view->display='Trap '.$trapDetail->trap_name.' received'; |
||
227 | $this->view->create_basic_rule=true; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Check if host & service still exists or set warning message |
||
232 | * @param object $ruleDetail |
||
233 | */ |
||
234 | private function add_check_host_exists($ruleDetail) |
||
235 | { |
||
236 | // Check if hostname still exists |
||
237 | $host_get=$this->getIdoConn()->getHostByName($this->view->hostname); |
||
238 | |||
239 | if (count($host_get)==0) |
||
240 | { |
||
241 | $this->view->warning_message='Host '.$this->view->hostname. ' doesn\'t exists anymore'; |
||
242 | $this->view->serviceGet=false; |
||
243 | } |
||
244 | else |
||
245 | { |
||
246 | // Tell JS to get services when page is loaded |
||
247 | $this->view->serviceGet=true; |
||
248 | // get service id for form to set : |
||
249 | $serviceID=$this->getIdoConn()->getServiceIDByName($this->view->hostname,$ruleDetail->service_name); |
||
250 | if (count($serviceID) ==0) |
||
251 | { |
||
252 | $this->view->warning_message=' Service '.$ruleDetail->service_name. ' doesn\'t exists anymore'; |
||
253 | } |
||
254 | else |
||
255 | { |
||
256 | $this->view->serviceSet=$serviceID[0]->id; |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Check if hostgroup & service still exists or set warning message |
||
263 | * @param array $ruleDetail |
||
264 | */ |
||
265 | private function add_check_hostgroup_exists($ruleDetail) |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Create object list array with all OIDs in rule & display |
||
298 | * Replace in rule & display by $<n>$ |
||
299 | * @param string $display |
||
300 | * @param string $rule |
||
301 | * @return array |
||
302 | */ |
||
303 | private function add_create_trap_object_list(&$display, &$rule) |
||
304 | { |
||
305 | $curObjectList=array(); |
||
306 | $index=1; |
||
307 | // check in display & rule for : OID(<oid>) |
||
308 | $matches=array(); |
||
309 | while ( preg_match('/_OID\(([\.0-9\*]+)\)/',$display,$matches) || |
||
310 | preg_match('/_OID\(([\.0-9\*]+)\)/',$rule,$matches)) |
||
311 | { |
||
312 | $curOid=$matches[1]; |
||
313 | |||
314 | if ( (preg_match('/\*/',$curOid) == 0 ) |
||
315 | && ($object=$this->getMIB()->translateOID($curOid)) != null) |
||
316 | { |
||
317 | array_push($curObjectList, array( |
||
318 | $index, |
||
319 | $curOid, |
||
320 | $object['mib'], |
||
321 | $object['name'], |
||
322 | '', |
||
323 | $object['type'], |
||
324 | $object['type_enum'] |
||
325 | )); |
||
326 | } |
||
327 | else |
||
328 | { |
||
329 | array_push($curObjectList, array( |
||
330 | $index, |
||
331 | $curOid, |
||
332 | 'not found', |
||
333 | 'not found', |
||
334 | '', |
||
335 | 'not found', |
||
336 | 'not found' |
||
337 | )); |
||
338 | } |
||
339 | $curOid = preg_replace('/\*/','\*',$curOid); |
||
340 | $display=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$display); |
||
341 | $rule=preg_replace('/_OID\('.$curOid.'\)/','\$'.$index.'\$',$rule); |
||
342 | $index++; |
||
343 | } |
||
344 | return $curObjectList; |
||
345 | } |
||
346 | |||
347 | /** Add a handler |
||
348 | * Get params fromid : setup from existing trap (id of trap table) |
||
349 | * Get param ruleid : edit from existing handler (id of rule table) |
||
350 | */ |
||
351 | public function addAction() |
||
433 | } |
||
434 | } |
||
435 | |||
436 | /** Validate form and output message to user |
||
437 | * @param in postdata |
||
438 | * @return string status : OK / <Message> |
||
439 | **/ |
||
440 | protected function handlerformAction() |
||
594 | |||
595 | } |
||
596 | |||
597 | /** Get trap detail by trapid. |
||
598 | * @param integer $trapid : id of trap in received table |
||
599 | * @return array (objects) |
||
600 | */ |
||
601 | protected function getTrapDetail($trapid) |
||
633 | |||
634 | } |
||
635 | |||
636 | /** Get trap objects |
||
637 | * @param integer $trapid : trap id |
||
638 | * @return array : full column in db of trap id |
||
639 | */ |
||
640 | protected function getTrapobjects($trapid) |
||
641 | { |
||
642 | if (!preg_match('/^[0-9]+$/',$trapid)) { throw new Exception('Invalid id'); } |
||
643 | $queryArrayData=$this->getModuleConfig()->trapDataDetailQuery(); |
||
644 | |||
645 | $dbConn = $this->getUIDatabase()->getDbConn(); |
||
646 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
647 | // *************** Get object data |
||
648 | // extract columns and titles; |
||
649 | $data_elmts=NULL; |
||
650 | foreach ($queryArrayData as $key => $val) { |
||
651 | $data_elmts[$key]=$val[1]; |
||
652 | } |
||
653 | try |
||
654 | { |
||
655 | $query = $dbConn->select() |
||
656 | ->from($this->moduleConfig->getTrapDataTableName(),$data_elmts) |
||
657 | ->where('trap_id=?',$trapid); |
||
658 | $trapDetail=$dbConn->fetchAll($query); |
||
659 | // if ( $trapDetail == null ) throw new Exception('No traps was found with id = '.$trapid); |
||
660 | } |
||
661 | catch (Exception $e) |
||
662 | { |
||
663 | $this->displayExitError('Add handler : get trap data detail : ',$e->getMessage()); |
||
664 | return array(); |
||
665 | } |
||
666 | |||
667 | return $trapDetail; |
||
668 | } |
||
669 | |||
670 | /** Get rule detail by ruleid. |
||
671 | * @param integer $ruleid int id of rule in rule table |
||
672 | * @return object|array : column objects in db |
||
673 | * |
||
674 | */ |
||
675 | protected function getRuleDetail($ruleid) |
||
676 | { |
||
677 | if (!preg_match('/^[0-9]+$/',$ruleid)) { throw new Exception('Invalid id'); } |
||
678 | $queryArray=$this->getModuleConfig()->ruleDetailQuery(); |
||
679 | |||
680 | $dbConn = $this->getUIDatabase()->getDbConn(); |
||
681 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
682 | // *************** Get main data |
||
683 | try |
||
684 | { |
||
685 | $query = $dbConn->select() |
||
686 | ->from($this->getModuleConfig()->getTrapRuleName(),$queryArray) |
||
687 | ->where('id=?',$ruleid); |
||
688 | $ruleDetail=$dbConn->fetchRow($query); |
||
689 | if ( $ruleDetail == null ) throw new Exception('No rule was found with id = '.$ruleid); |
||
690 | } |
||
691 | catch (Exception $e) |
||
692 | { |
||
693 | $this->displayExitError('Update handler : get rule detail',$e->getMessage()); |
||
694 | throw new Exception('Error : ' . $e->getMessage()); |
||
695 | } |
||
696 | |||
697 | return $ruleDetail; |
||
698 | |||
699 | } |
||
700 | |||
701 | /** Setup tabs for rules |
||
702 | */ |
||
703 | protected function prepareTabs() |
||
708 | ); |
||
709 | } |
||
710 | |||
711 | } |