| Total Complexity | 53 |
| Total Lines | 453 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like HelperController 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 HelperController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class HelperController extends TrapsController |
||
| 12 | { |
||
| 13 | |||
| 14 | /** Get host list with filter (IP or name) : host=<filter> |
||
| 15 | * returns in JSON : status=>OK/NOK hosts=>array of hosts |
||
| 16 | */ |
||
| 17 | public function gethostsAction() |
||
| 18 | { |
||
| 19 | $postData=$this->getRequest()->getPost(); |
||
| 20 | if (isset($postData['hostFilter'])) |
||
| 21 | { |
||
| 22 | $hostFilter=$postData['hostFilter']; |
||
| 23 | } |
||
| 24 | else |
||
| 25 | { |
||
| 26 | $this->_helper->json(array('status'=>'KO')); |
||
| 27 | return; |
||
| 28 | } |
||
| 29 | |||
| 30 | $retHosts=array('status'=>'OK','hosts' => array()); |
||
| 31 | |||
| 32 | $hosts=$this->getHostByIP($hostFilter); |
||
| 33 | foreach ($hosts as $val) |
||
| 34 | { |
||
| 35 | array_push($retHosts['hosts'],$val->name); |
||
| 36 | } |
||
| 37 | |||
| 38 | $this->_helper->json($retHosts); |
||
| 39 | } |
||
| 40 | |||
| 41 | |||
| 42 | /** Get hostgroup list with filter (name) : hostgroup=<hostFilter> |
||
| 43 | * returns in JSON : status=>OK/NOK hosts=>array of hosts |
||
| 44 | */ |
||
| 45 | public function gethostgroupsAction() |
||
| 46 | { |
||
| 47 | $postData=$this->getRequest()->getPost(); |
||
| 48 | if (isset($postData['hostFilter'])) |
||
| 49 | { |
||
| 50 | $hostFilter=$postData['hostFilter']; |
||
| 51 | } |
||
| 52 | else |
||
| 53 | { |
||
| 54 | $this->_helper->json(array('status'=>'Error : no filter')); |
||
| 55 | return; |
||
| 56 | } |
||
| 57 | |||
| 58 | $retHosts=array('status'=>'OK','hosts' => array()); |
||
| 59 | |||
| 60 | $hosts=$this->getHostGroupByName($hostFilter); |
||
| 61 | foreach ($hosts as $val) |
||
| 62 | { |
||
| 63 | array_push($retHosts['hosts'],$val->name); |
||
| 64 | } |
||
| 65 | |||
| 66 | $this->_helper->json($retHosts); |
||
| 67 | } |
||
| 68 | |||
| 69 | |||
| 70 | /** Get service list by host name ( host=<host> ) |
||
| 71 | * returns in JSON : |
||
| 72 | * status=>OK/No services found/More than one host matches |
||
| 73 | * services=>array of services (name) |
||
| 74 | * hostid = host object id or -1 if not found. |
||
| 75 | */ |
||
| 76 | public function getservicesAction() |
||
| 112 | } |
||
| 113 | |||
| 114 | /** Get service list by host group ( name=<host> ) |
||
| 115 | * returns in JSON : |
||
| 116 | * status=>OK/No services found/More than one host matches |
||
| 117 | * services=>array of services (name) |
||
| 118 | * groupid = group object id or -1 if not found. |
||
| 119 | */ |
||
| 120 | public function gethostgroupservicesAction() |
||
| 121 | { |
||
| 122 | $postData=$this->getRequest()->getPost(); |
||
| 123 | if (isset($postData['host'])) |
||
| 124 | { |
||
| 125 | $host=$postData['host']; |
||
| 126 | } |
||
| 127 | else |
||
| 128 | { |
||
| 129 | $this->_helper->json(array('status'=>'No Hosts','hostid' => -1)); |
||
| 130 | return; |
||
| 131 | } |
||
| 132 | |||
| 133 | $hostArray=$this->getHostGroupByName($host); |
||
| 134 | if (count($hostArray) > 1) |
||
| 135 | { |
||
| 136 | $this->_helper->json(array('status'=>'More than one hostgroup matches','hostid' => -1)); |
||
| 137 | return; |
||
| 138 | } |
||
| 139 | else if (count($hostArray) == 0) |
||
| 140 | { |
||
| 141 | $this->_helper->json(array('status'=>'No hostgroup matches','hostid' => -1)); |
||
| 142 | return; |
||
| 143 | } |
||
| 144 | $services=$this->getServicesByHostGroupid($hostArray[0]->id); |
||
| 145 | if (count($services) < 1) |
||
| 146 | { |
||
| 147 | $this->_helper->json(array('status'=>'No services found for hostgroup','hostid' => $hostArray[0]->id)); |
||
| 148 | return; |
||
| 149 | } |
||
| 150 | $retServices=array('status'=>'OK','services' => $services,'hostid' => $hostArray[0]->id); |
||
| 151 | |||
| 152 | $this->_helper->json($retServices); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** Get traps from mib : entry : mib=<mib> |
||
| 156 | * returns in JSON : |
||
| 157 | * status=>OK/No mib/Error getting mibs |
||
| 158 | * traps=>array of array( oid -> name) |
||
| 159 | */ |
||
| 160 | public function gettrapsAction() |
||
| 161 | { |
||
| 162 | $postData=$this->getRequest()->getPost(); |
||
| 163 | if (isset($postData['mib'])) |
||
| 164 | { |
||
| 165 | $mib=$postData['mib']; |
||
| 166 | } |
||
| 167 | else |
||
| 168 | { |
||
| 169 | $this->_helper->json(array('status'=>'No mib')); |
||
| 170 | return; |
||
| 171 | } |
||
| 172 | try |
||
| 173 | { |
||
| 174 | $traplist=$this->getMIB()->getTrapList($mib); |
||
| 175 | $retTraps=array('status'=>'OK','traps' => $traplist); |
||
| 176 | } |
||
| 177 | catch (Exception $e) |
||
| 178 | { |
||
| 179 | $retTraps=array('status' => 'Error getting mibs'); |
||
| 180 | } |
||
| 181 | $this->_helper->json($retTraps); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** Get trap objects from mib : entry : trap=<oid> |
||
| 185 | * returns in JSON : |
||
| 186 | * status=>OK/no trap/not found |
||
| 187 | * objects=>array of array( oid -> name, oid->mib) |
||
| 188 | */ |
||
| 189 | public function gettrapobjectsAction() |
||
| 190 | { |
||
| 191 | $postData=$this->getRequest()->getPost(); |
||
| 192 | if (isset($postData['trap'])) |
||
| 193 | { |
||
| 194 | $trap=$postData['trap']; |
||
| 195 | } |
||
| 196 | else |
||
| 197 | { |
||
| 198 | $this->_helper->json(array('status'=>'No trap')); |
||
| 199 | return; |
||
| 200 | } |
||
| 201 | try |
||
| 202 | { |
||
| 203 | $objectlist=$this->getMIB()->getObjectList($trap); |
||
| 204 | $retObjects=array('status'=>'OK','objects' => $objectlist); |
||
| 205 | } |
||
| 206 | catch (Exception $e) |
||
| 207 | { |
||
| 208 | $retObjects=array('status' => 'not found'); |
||
| 209 | } |
||
| 210 | $this->_helper->json($retObjects); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** Get list of all loaded mibs : entry : none |
||
| 214 | * return : array of strings. |
||
| 215 | */ |
||
| 216 | public function getmiblistAction() |
||
| 227 | } |
||
| 228 | |||
| 229 | /** Get MIB::Name from OID : entry : oid |
||
| 230 | * status=>OK/No oid/not found |
||
| 231 | * mib=>string |
||
| 232 | * name=>string |
||
| 233 | */ |
||
| 234 | public function translateoidAction() |
||
| 262 | ) |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | |||
| 266 | } |
||
| 267 | |||
| 268 | |||
| 269 | /** Save or execute database purge of <n> days |
||
| 270 | * days=>int |
||
| 271 | * action=>save/execute |
||
| 272 | * return : status=>OK/Message error |
||
| 273 | */ |
||
| 274 | public function dbmaintenanceAction() |
||
| 337 | } |
||
| 338 | |||
| 339 | } |
||
| 340 | |||
| 341 | /** Save log output to db |
||
| 342 | * destination=>log destination |
||
| 343 | * file=>file name |
||
| 344 | * level => int |
||
| 345 | * return : status=>OK/Message error |
||
| 346 | */ |
||
| 347 | public function logdestinationAction() |
||
| 348 | { |
||
| 349 | $postData=$this->getRequest()->getPost(); |
||
| 350 | if (isset($postData['destination'])) |
||
| 351 | { |
||
| 352 | $destination=$postData['destination']; |
||
| 353 | $logDest=$this->getModuleConfig()->getLogDestinations(); |
||
| 354 | if (!isset($logDest[$destination])) |
||
| 355 | { |
||
| 356 | $this->_helper->json(array('status'=>'invalid destination : '.$destination)); |
||
| 357 | return; |
||
| 358 | } |
||
| 359 | } |
||
| 360 | else |
||
| 361 | { |
||
| 362 | $this->_helper->json(array('status'=>'No destination')); |
||
| 363 | } |
||
| 364 | if (isset($postData['file'])) |
||
| 365 | { |
||
| 366 | $file=$postData['file']; |
||
| 367 | $fileHandler=@fopen($file,'w'); |
||
| 368 | if ($fileHandler == false) |
||
| 369 | { // File os note writabe / cannot create |
||
| 370 | $this->_helper->json(array('status'=>'File not writable : '.$file)); |
||
| 371 | return; |
||
| 372 | } |
||
| 373 | } |
||
| 374 | else |
||
| 375 | { |
||
| 376 | if ($destination != 'file') |
||
|
|
|||
| 377 | { |
||
| 378 | $file=null; |
||
| 379 | } |
||
| 380 | else |
||
| 381 | { |
||
| 382 | $this->_helper->json(array('status'=>'No file')); |
||
| 383 | return; |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | if (isset($postData['level'])) |
||
| 388 | { |
||
| 389 | $level=$postData['level']; |
||
| 390 | } |
||
| 391 | else |
||
| 392 | { |
||
| 393 | $this->_helper->json(array('status'=>'No level')); |
||
| 394 | return; |
||
| 395 | } |
||
| 396 | |||
| 397 | try |
||
| 398 | { |
||
| 399 | $this->setDBConfigValue('log_destination',$destination); |
||
| 400 | $this->setDBConfigValue('log_file',$file); |
||
| 401 | $this->setDBConfigValue('log_level',$level); |
||
| 402 | } |
||
| 403 | catch (Exception $e) |
||
| 404 | { |
||
| 405 | $this->_helper->json(array('status'=>'Save error : '.$e->getMessage() )); |
||
| 406 | return; |
||
| 407 | } |
||
| 408 | $this->_helper->json(array('status'=>'OK')); |
||
| 409 | return; |
||
| 410 | |||
| 411 | } |
||
| 412 | |||
| 413 | /** Test a rule evaluation |
||
| 414 | * rule=>rule to evaluate |
||
| 415 | * action=>'evaluate' |
||
| 416 | * return : status=>OK/Message error & message : return of evaluation |
||
| 417 | */ |
||
| 418 | public function testruleAction() |
||
| 464 | } |
||
| 465 | |||
| 466 | } |
||
| 467 | |||
| 469 |