| Total Complexity | 58 |
| Total Lines | 427 |
| Duplicated Lines | 0 % |
| Changes | 17 | ||
| Bugs | 1 | Features | 0 |
Complex classes like TrapsController 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 TrapsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class TrapsController extends Controller |
||
| 23 | { |
||
| 24 | /** @var TrapModuleConfig $moduleConfig TrapModuleConfig instance */ |
||
| 25 | protected $moduleConfig; |
||
| 26 | /** @var TrapTableList $trapTableList (by date)*/ |
||
| 27 | protected $trapTableList; |
||
| 28 | /** @var TrapTableHostList $trapTableHostList TrapTableList (by hosts)*/ |
||
| 29 | protected $trapTableHostList; |
||
| 30 | /** @var HandlerTableList $handlerTableList HandlerTableList instance*/ |
||
| 31 | protected $handlerTableList; |
||
| 32 | /** @var ConfigObject $trapDB Trap database */ |
||
| 33 | protected $trapDB; |
||
| 34 | /** @var ConfigObject $icingaDB Icinga IDO database */ |
||
| 35 | protected $icingaDB; |
||
| 36 | /** @var MIBLoader $MIBData MIBLoader class */ |
||
| 37 | protected $MIBData; |
||
| 38 | /** @var Trap $trapClass Trap class for bin/trap_class.php */ |
||
| 39 | protected $trapClass; |
||
| 40 | /** @var UIDatabase $UIDatabase */ |
||
| 41 | protected $UIDatabase; |
||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | /** Get instance of TrapModuleConfig class |
||
| 46 | * @return TrapModuleConfig |
||
| 47 | */ |
||
| 48 | public function getModuleConfig() |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get instance of TrapTableList |
||
| 64 | * @return \Icinga\Module\Trapdirector\Tables\TrapTableList |
||
| 65 | */ |
||
| 66 | public function getTrapListTable() { |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return \Icinga\Module\Trapdirector\Tables\TrapTableHostList |
||
| 76 | */ |
||
| 77 | public function getTrapHostListTable() |
||
| 78 | { |
||
| 79 | if ($this->trapTableHostList == Null) |
||
| 80 | { |
||
| 81 | $this->trapTableHostList = new TrapTableHostList(); |
||
| 82 | $this->trapTableHostList->setConfig($this->getModuleConfig()); |
||
| 83 | } |
||
| 84 | return $this->trapTableHostList; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @return \Icinga\Module\Trapdirector\Tables\HandlerTableList |
||
| 89 | */ |
||
| 90 | public function getHandlerListTable() |
||
| 91 | { |
||
| 92 | if ($this->handlerTableList == Null) |
||
| 93 | { |
||
| 94 | $this->handlerTableList = new HandlerTableList(); |
||
| 95 | $this->handlerTableList->setConfig($this->getModuleConfig()); |
||
| 96 | } |
||
| 97 | return $this->handlerTableList; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return UIDatabase |
||
| 102 | */ |
||
| 103 | public function getUIDatabase() |
||
| 104 | { |
||
| 105 | if ($this->UIDatabase == Null) |
||
| 106 | { |
||
| 107 | $this->UIDatabase = new UIDatabase($this); |
||
| 108 | |||
| 109 | } |
||
| 110 | return $this->UIDatabase; |
||
| 111 | } |
||
| 112 | |||
| 113 | protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
||
| 114 | { |
||
| 115 | $limit = $this->params->get('limit', $limit); |
||
| 116 | $page = $this->params->get('page', $offset); |
||
| 117 | |||
| 118 | $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
||
| 119 | |||
| 120 | return $paginatable; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function displayExitError($source,$message) |
||
| 124 | { // TODO : check better ways to transmit data (with POST ?) |
||
| 125 | $this->redirectNow('trapdirector/error?source='.$source.'&message='.$message); |
||
| 126 | } |
||
| 127 | |||
| 128 | protected function checkReadPermission() |
||
| 129 | { |
||
| 130 | if (! $this->Auth()->hasPermission('trapdirector/view')) { |
||
| 131 | $this->displayExitError('Permissions','No permission fo view content'); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | protected function checkConfigPermission() |
||
| 136 | { |
||
| 137 | if (! $this->Auth()->hasPermission('trapdirector/config')) { |
||
| 138 | $this->displayExitError('Permissions','No permission fo configure'); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Check if user has write permission |
||
| 144 | * @param number $check optional : if set to 1, return true (user has permission) or false instead of displaying error page |
||
| 145 | * @return boolean : user has permission |
||
| 146 | */ |
||
| 147 | protected function checkModuleConfigPermission($check=0) |
||
| 148 | { |
||
| 149 | if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
||
| 150 | if ($check == 0) |
||
| 151 | { |
||
| 152 | $this->displayExitError('Permissions','No permission fo configure module'); |
||
| 153 | } |
||
| 154 | return false; |
||
| 155 | } |
||
| 156 | return true; |
||
| 157 | } |
||
| 158 | |||
| 159 | /************************* Trap class get **********************/ |
||
| 160 | public function getTrapClass() |
||
| 161 | { // TODO : try/catch here ? or within caller |
||
| 162 | if ($this->trapClass == null) |
||
| 163 | { |
||
| 164 | require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
||
| 165 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
||
| 166 | //$debug_level=4; |
||
| 167 | $this->trapClass = new Trap($icingaweb2_etc); |
||
| 168 | //$Trap->setLogging($debug_level,'syslog'); |
||
| 169 | } |
||
| 170 | return $this->trapClass; |
||
| 171 | } |
||
| 172 | |||
| 173 | /************************** MIB related **************************/ |
||
| 174 | |||
| 175 | /** Get MIBLoader class |
||
| 176 | * @return MIBLoader class |
||
| 177 | */ |
||
| 178 | protected function getMIB() |
||
| 179 | { |
||
| 180 | if ($this->MIBData == null) |
||
| 181 | { |
||
| 182 | $dbConn = $this->getUIDatabase()->getDbConn(); |
||
| 183 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
| 184 | $this->MIBData=new MIBLoader( |
||
| 185 | $this->Config()->get('config', 'snmptranslate'), |
||
| 186 | $this->Config()->get('config', 'snmptranslate_dirs'), |
||
| 187 | $dbConn, |
||
| 188 | $this->getModuleConfig() |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | return $this->MIBData; |
||
| 192 | } |
||
| 193 | |||
| 194 | /************************** Database queries *******************/ |
||
| 195 | |||
| 196 | /** Get host(s) by IP (v4 or v6) or by name in IDO database |
||
| 197 | * does not catch exceptions |
||
| 198 | * @return array of objects ( name, id (object_id), display_name) |
||
| 199 | */ |
||
| 200 | protected function getHostByIP($ip) |
||
| 201 | { |
||
| 202 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
||
| 203 | $dbConn = $this->getUIDatabase()->getIdoDbConn(); |
||
| 204 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
| 205 | |||
| 206 | // TODO : check for SQL injections |
||
| 207 | $query=$dbConn->select() |
||
| 208 | ->from( |
||
| 209 | array('a' => 'icinga_objects'), |
||
| 210 | array('name' => 'a.name1','id' => 'object_id')) |
||
| 211 | ->join( |
||
| 212 | array('b' => 'icinga_hosts'), |
||
| 213 | 'b.host_object_id=a.object_id', |
||
| 214 | array('display_name' => 'b.display_name')) |
||
| 215 | ->where("(b.address LIKE '%".$ip."%' OR b.address6 LIKE '%".$ip."%' OR a.name1 LIKE '%".$ip."%' OR b.display_name LIKE '%".$ip."%') and a.is_active = 1"); |
||
| 216 | return $dbConn->fetchAll($query); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** Get host(s) by name in IDO database |
||
| 220 | * does not catch exceptions |
||
| 221 | * @return array of objects ( name, id (object_id), display_name) |
||
| 222 | */ |
||
| 223 | protected function getHostByName($name) |
||
| 224 | { |
||
| 225 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
||
| 226 | $dbConn = $this->getUIDatabase()->getIdoDbConn(); |
||
| 227 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
| 228 | |||
| 229 | // TODO : check for SQL injections |
||
| 230 | $query=$dbConn->select() |
||
| 231 | ->from( |
||
| 232 | array('a' => 'icinga_objects'), |
||
| 233 | array('name' => 'a.name1','id' => 'object_id')) |
||
| 234 | ->join( |
||
| 235 | array('b' => 'icinga_hosts'), |
||
| 236 | 'b.host_object_id=a.object_id', |
||
| 237 | array('display_name' => 'b.display_name')) |
||
| 238 | ->where("a.name1 = '$name'"); |
||
| 239 | return $dbConn->fetchAll($query); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** Get host groups by name in IDO database |
||
| 243 | * does not catch exceptions |
||
| 244 | * @return array of objects ( name, id (object_id), display_name) |
||
| 245 | */ |
||
| 246 | protected function getHostGroupByName($ip) |
||
| 247 | { |
||
| 248 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
||
| 249 | $dbConn = $this->getUIDatabase()->getIdoDbConn(); |
||
| 250 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
| 251 | // TODO : check for SQL injections |
||
| 252 | $query=$dbConn->select() |
||
| 253 | ->from( |
||
| 254 | array('a' => 'icinga_objects'), |
||
| 255 | array('name' => 'a.name1','id' => 'object_id')) |
||
| 256 | ->join( |
||
| 257 | array('b' => 'icinga_hostgroups'), |
||
| 258 | 'b.hostgroup_object_id=a.object_id', |
||
| 259 | array('display_name' => 'b.alias')) |
||
| 260 | ->where("(a.name1 LIKE '%".$ip."%' OR b.alias LIKE '%".$ip."%') and a.is_active = 1"); |
||
| 261 | return $dbConn->fetchAll($query); |
||
| 262 | } |
||
| 263 | |||
| 264 | |||
| 265 | /** Get host IP (v4 and v6) by name in IDO database |
||
| 266 | * does not catch exceptions |
||
| 267 | * @return array ( name, display_name, ip4, ip6) |
||
| 268 | */ |
||
| 269 | protected function getHostInfoByID($id) |
||
| 270 | { |
||
| 271 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
||
| 272 | $dbConn = $this->getUIDatabase()->getIdoDbConn(); |
||
| 273 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
| 274 | $query=$dbConn->select() |
||
| 275 | ->from( |
||
| 276 | array('a' => 'icinga_objects'), |
||
| 277 | array('name' => 'a.name1')) |
||
| 278 | ->join( |
||
| 279 | array('b' => 'icinga_hosts'), |
||
| 280 | 'b.host_object_id=a.object_id', |
||
| 281 | array('ip4' => 'b.address', 'ip6' => 'b.address6', 'display_name' => 'b.display_name')) |
||
| 282 | ->where("a.object_id = '".$id."'"); |
||
| 283 | return $dbConn->fetchRow($query); |
||
| 284 | } |
||
| 285 | |||
| 286 | |||
| 287 | /** Get host by objectid in IDO database |
||
| 288 | * does not catch exceptions |
||
| 289 | * @return array of objects ( id, name, display_name, ip, ip6, ) |
||
| 290 | */ |
||
| 291 | protected function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above |
||
| 292 | { |
||
| 293 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
||
| 294 | $dbConn = $this->getUIDatabase()->getIdoDbConn(); |
||
| 295 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
| 296 | $query=$dbConn->select() |
||
| 297 | ->from( |
||
| 298 | array('a' => 'icinga_objects'), |
||
| 299 | array('name' => 'a.name1','id' => 'a.object_id')) |
||
| 300 | ->join( |
||
| 301 | array('b' => 'icinga_hosts'), |
||
| 302 | 'b.host_object_id=a.object_id', |
||
| 303 | array('display_name' => 'b.display_name' , 'ip' => 'b.address', 'ip6' => 'b.address6')) |
||
| 304 | ->where('a.object_id = ?',$id); |
||
| 305 | return $dbConn->fetchRow($query); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** Get services from object ( host_object_id) in IDO database |
||
| 309 | * does not catch exceptions |
||
| 310 | * @param $id int object_id |
||
| 311 | * @return array display_name (of service), service_object_id |
||
| 312 | */ |
||
| 313 | protected function getServicesByHostid($id) |
||
| 329 | } |
||
| 330 | |||
| 331 | /** Get services from hostgroup object id ( hostgroup_object_id) in IDO database |
||
| 332 | * gets all hosts in hostgroup and return common services |
||
| 333 | * does not catch exceptions |
||
| 334 | * @param $id int object_id |
||
| 335 | * @return array display_name (of service), service_object_id |
||
| 336 | */ |
||
| 337 | protected function getServicesByHostGroupid($id) |
||
| 338 | { |
||
| 339 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
||
| 340 | $dbConn = $this->getUIDatabase()->getIdoDbConn(); |
||
| 341 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
| 342 | $query=$dbConn->select() |
||
| 343 | ->from( |
||
| 344 | array('s' => 'icinga_hostgroup_members'), |
||
| 345 | array('host_object_id' => 's.host_object_id')) |
||
| 346 | ->join( |
||
| 347 | array('a' => 'icinga_hostgroups'), |
||
| 348 | 's.hostgroup_id=a.hostgroup_id', |
||
| 349 | 'hostgroup_object_id') |
||
| 350 | ->where('a.hostgroup_object_id='.$id); |
||
| 351 | $hosts=$dbConn->fetchAll($query); |
||
| 352 | $common_services=array(); |
||
| 353 | $num_hosts=count($hosts); |
||
| 354 | foreach ($hosts as $key => $host) |
||
| 355 | { // For each host, get all services and add in common_services if not found or add counter |
||
| 356 | $host_services=$this->getServicesByHostid($host->host_object_id); |
||
| 357 | foreach($host_services as $service) |
||
| 358 | { |
||
| 359 | if (isset($common_services[$service->name2]['num'])) |
||
| 360 | { |
||
| 361 | $common_services[$service->name2]['num'] +=1; |
||
| 362 | } |
||
| 363 | else |
||
| 364 | { |
||
| 365 | $common_services[$service->name2]['num']=1; |
||
| 366 | $common_services[$service->name2]['name']=$service->name; |
||
| 367 | } |
||
| 368 | } |
||
| 369 | } |
||
| 370 | $result=array(); |
||
| 371 | |||
| 372 | //print_r($common_services); |
||
| 373 | foreach (array_keys($common_services) as $key) |
||
| 374 | { |
||
| 375 | if ($common_services[$key]['num'] == $num_hosts) |
||
| 376 | { |
||
| 377 | array_push($result,array($key,$common_services[$key]['name'])); |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | return $result; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** Get services object id by host name / service name in IDO database |
||
| 385 | * does not catch exceptions |
||
| 386 | * @param $hostname string host name |
||
| 387 | * @param $name string service name |
||
| 388 | * @return array service id |
||
| 389 | */ |
||
| 390 | protected function getServiceIDByName($hostname,$name) |
||
| 391 | { |
||
| 392 | $dbConn = $this->getUIDatabase()->getIdoDbConn(); |
||
| 393 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
| 394 | |||
| 395 | if ($name == null) |
||
| 396 | { |
||
| 397 | return array(); |
||
| 398 | } |
||
| 399 | |||
| 400 | $query=$dbConn->select() |
||
| 401 | ->from( |
||
| 402 | array('s' => 'icinga_services'), |
||
| 403 | array('name' => 's.display_name','id' => 's.service_object_id')) |
||
| 404 | ->join( |
||
| 405 | array('a' => 'icinga_objects'), |
||
| 406 | 's.service_object_id=a.object_id', |
||
| 407 | 'is_active') |
||
| 408 | ->where('a.name2=\''.$name.'\' AND a.name1=\''.$hostname.'\' AND a.is_active = 1'); |
||
| 409 | |||
| 410 | return $dbConn->fetchAll($query); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** Get object name from object_id in IDO database |
||
| 414 | * does not catch exceptions |
||
| 415 | * @param int $id object_id (default to null, used first if not null) |
||
| 416 | * @return array name1 (host) name2 (service) |
||
| 417 | */ |
||
| 418 | protected function getObjectNameByid($id) |
||
| 419 | { |
||
| 420 | // select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id |
||
| 421 | if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id'); } |
||
| 422 | $dbConn = $this->getUIDatabase()->getIdoDbConn(); |
||
| 423 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
| 424 | |||
| 425 | $query=$dbConn->select() |
||
| 426 | ->from( |
||
| 427 | array('a' => 'icinga_objects'), |
||
| 428 | array('name1' => 'a.name1','name2' => 'a.name2')) |
||
| 429 | ->where('a.object_id='.$id.' AND a.is_active = 1'); |
||
| 430 | |||
| 431 | return $dbConn->fetchRow($query); |
||
| 432 | } |
||
| 433 | |||
| 434 | /** Check if director is installed |
||
| 435 | * @return bool true/false |
||
| 436 | */ |
||
| 437 | protected function isDirectorInstalled() |
||
| 449 | } |
||
| 450 | |||
| 451 | } |
||
| 452 | |||
| 453 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths