Total Complexity | 46 |
Total Lines | 297 |
Duplicated Lines | 0 % |
Changes | 24 | ||
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 |
||
23 | class TrapsController extends Controller |
||
24 | { |
||
25 | /** @var TrapModuleConfig $moduleConfig TrapModuleConfig instance */ |
||
26 | protected $moduleConfig; |
||
27 | /** @var TrapTableList $trapTableList (by date)*/ |
||
28 | protected $trapTableList; |
||
29 | /** @var TrapTableHostList $trapTableHostList TrapTableList (by hosts)*/ |
||
30 | protected $trapTableHostList; |
||
31 | /** @var HandlerTableList $handlerTableList HandlerTableList instance*/ |
||
32 | protected $handlerTableList; |
||
33 | /** @var ConfigObject $trapDB Trap database */ |
||
34 | protected $trapDB; |
||
35 | /** @var ConfigObject $icingaDB Icinga IDO database */ |
||
36 | protected $icingaDB; |
||
37 | /** @var MIBLoader $MIBData MIBLoader class */ |
||
38 | protected $MIBData; |
||
39 | /** @var Trap $trapClass Trap class for bin/trap_class.php */ |
||
40 | protected $trapClass; |
||
41 | /** @var UIDatabase $UIDatabase */ |
||
42 | protected $UIDatabase = NULL; |
||
43 | /** @var Icinga2API $IcingaAPI */ |
||
44 | protected $icingaAPI = NULL; |
||
45 | /** @var bool $apiMode connection to icinngaDB is by api (true) od ido DB (false) */ |
||
46 | protected $apiMode = FALSE; |
||
47 | |||
48 | |||
49 | |||
50 | |||
51 | /** Get instance of TrapModuleConfig class |
||
52 | * @return TrapModuleConfig |
||
53 | */ |
||
54 | public function getModuleConfig() |
||
55 | { |
||
56 | if ($this->moduleConfig == Null) |
||
57 | { |
||
58 | $db_prefix=$this->Config()->get('config', 'database_prefix'); |
||
59 | if ($db_prefix === null) |
||
60 | { |
||
61 | $this->redirectNow('trapdirector/settings?message=No database prefix'); |
||
62 | } |
||
63 | $this->moduleConfig = new TrapModuleConfig($db_prefix); |
||
64 | } |
||
65 | return $this->moduleConfig; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Get instance of TrapTableList |
||
70 | * @return \Icinga\Module\Trapdirector\Tables\TrapTableList |
||
71 | */ |
||
72 | public function getTrapListTable() { |
||
73 | if ($this->trapTableList == Null) { |
||
74 | $this->trapTableList = new TrapTableList(); |
||
75 | $this->trapTableList->setConfig($this->getModuleConfig()); |
||
76 | } |
||
77 | return $this->trapTableList; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return \Icinga\Module\Trapdirector\Tables\TrapTableHostList |
||
82 | */ |
||
83 | public function getTrapHostListTable() |
||
84 | { |
||
85 | if ($this->trapTableHostList == Null) |
||
86 | { |
||
87 | $this->trapTableHostList = new TrapTableHostList(); |
||
88 | $this->trapTableHostList->setConfig($this->getModuleConfig()); |
||
89 | } |
||
90 | return $this->trapTableHostList; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return \Icinga\Module\Trapdirector\Tables\HandlerTableList |
||
95 | */ |
||
96 | public function getHandlerListTable() |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return UIDatabase |
||
108 | */ |
||
109 | public function getUIDatabase() |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get Ido connection on API (first) or Database as fallback |
||
121 | * @return \Icinga\Module\Trapdirector\TrapsActions\UIDatabase|\Icinga\Module\Trapdirector\Icinga2API |
||
122 | */ |
||
123 | public function getIdoConn() |
||
124 | { |
||
125 | if ($this->icingaAPI === NULL) |
||
126 | { |
||
127 | $host = $this->Config()->get('config', 'icingaAPI_host'); |
||
128 | if ($host == '') |
||
129 | { |
||
130 | $this->apiMode = FALSE; |
||
131 | return $this->getUIDatabase(); |
||
132 | } |
||
133 | $port = $this->Config()->get('config', 'icingaAPI_port'); |
||
134 | $user = $this->Config()->get('config', 'icingaAPI_user'); |
||
135 | $pass = $this->Config()->get('config', 'icingaAPI_password'); |
||
136 | $this->icingaAPI = new Icinga2API($host,$port); |
||
137 | $this->icingaAPI->setCredentials($user, $pass); |
||
138 | list($ret,$message) = $this->icingaAPI->test($this->getModuleConfig()->getapiUserPermissions()); |
||
139 | if ($ret === TRUE) // On error, switch to ido DB |
||
140 | { |
||
141 | $this->apiMode = FALSE; |
||
142 | return $this->getUIDatabase(); |
||
143 | } |
||
144 | $this->apiMode = TRUE; |
||
145 | |||
146 | } |
||
147 | return $this->icingaAPI; |
||
148 | |||
149 | } |
||
150 | |||
151 | protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null) |
||
152 | { |
||
153 | $limit = $this->params->get('limit', $limit); |
||
154 | $page = $this->params->get('page', $offset); |
||
155 | |||
156 | $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0); |
||
157 | |||
158 | return $paginatable; |
||
159 | } |
||
160 | |||
161 | public function displayExitError($source,$message) |
||
162 | { // TODO : check better ways to transmit data (with POST ?) |
||
163 | $this->redirectNow('trapdirector/error?source='.$source.'&message='.$message); |
||
164 | } |
||
165 | |||
166 | protected function checkReadPermission() |
||
167 | { |
||
168 | if (! $this->Auth()->hasPermission('trapdirector/view')) { |
||
169 | $this->displayExitError('Permissions','No permission fo view content'); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | protected function checkConfigPermission() |
||
174 | { |
||
175 | if (! $this->Auth()->hasPermission('trapdirector/config')) { |
||
176 | $this->displayExitError('Permissions','No permission fo configure'); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Check if user has write permission |
||
182 | * @param number $check optional : if set to 1, return true (user has permission) or false instead of displaying error page |
||
183 | * @return boolean : user has permission |
||
184 | */ |
||
185 | protected function checkModuleConfigPermission($check=0) |
||
186 | { |
||
187 | if (! $this->Auth()->hasPermission('trapdirector/module_config')) { |
||
188 | if ($check == 0) |
||
189 | { |
||
190 | $this->displayExitError('Permissions','No permission fo configure module'); |
||
191 | } |
||
192 | return false; |
||
193 | } |
||
194 | return true; |
||
195 | } |
||
196 | |||
197 | /************************* Trap class get **********************/ |
||
198 | public function getTrapClass() |
||
199 | { // TODO : try/catch here ? or within caller |
||
200 | if ($this->trapClass == null) |
||
201 | { |
||
202 | require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
||
203 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
||
204 | //$debug_level=4; |
||
205 | $this->trapClass = new Trap($icingaweb2_etc); |
||
206 | //$Trap->setLogging($debug_level,'syslog'); |
||
207 | } |
||
208 | return $this->trapClass; |
||
209 | } |
||
210 | |||
211 | /************************** MIB related **************************/ |
||
212 | |||
213 | /** Get MIBLoader class |
||
214 | * @return MIBLoader class |
||
215 | */ |
||
216 | protected function getMIB() |
||
217 | { |
||
218 | if ($this->MIBData == null) |
||
219 | { |
||
220 | $dbConn = $this->getUIDatabase()->getDbConn(); |
||
221 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
222 | $this->MIBData=new MIBLoader( |
||
223 | $this->Config()->get('config', 'snmptranslate'), |
||
224 | $this->Config()->get('config', 'snmptranslate_dirs'), |
||
225 | $dbConn, |
||
226 | $this->getModuleConfig() |
||
227 | ); |
||
228 | } |
||
229 | return $this->MIBData; |
||
230 | } |
||
231 | |||
232 | /************************** Database queries *******************/ |
||
233 | |||
234 | /** Check if director is installed |
||
235 | * @return bool true/false |
||
236 | */ |
||
237 | protected function isDirectorInstalled() |
||
238 | { |
||
239 | $output=array(); |
||
240 | exec('icingacli module list',$output); |
||
241 | foreach ($output as $line) |
||
242 | { |
||
243 | if (preg_match('/^director .*enabled/',$line)) |
||
244 | { |
||
245 | return true; |
||
246 | } |
||
247 | } |
||
248 | return false; |
||
249 | } |
||
250 | |||
251 | |||
252 | /************************ UI elements **************************/ |
||
253 | |||
254 | /** |
||
255 | * get max rows to display before paging. |
||
256 | * @return number |
||
257 | */ |
||
258 | public function itemListDisplay() |
||
259 | { |
||
260 | return $this->getUIDatabase()->getDBConfigValue('max_rows_in_list'); |
||
261 | } |
||
262 | |||
263 | public function setitemListDisplay(int $maxRows) |
||
264 | { |
||
265 | return $this->getUIDatabase()->setDBConfigValue('max_rows_in_list',$maxRows); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * get Handlers categories list (index => textvalue). |
||
270 | * @return array |
||
271 | */ |
||
272 | public function getHandlersCategory() |
||
284 | } |
||
285 | |||
286 | public function setHandlerCategory(array $catArray) |
||
287 | { |
||
288 | $catString=''; |
||
289 | foreach ($catArray as $index => $value) |
||
290 | { |
||
291 | if ($catString != '' ) $catString .= '!'; |
||
292 | $catString .= $index . ':' . $value; |
||
293 | } |
||
294 | $this->getUIDatabase()->setDBConfigValue('handler_categories', $catString); |
||
295 | } |
||
296 | |||
297 | public function addHandlersCategory(string $catName) |
||
298 | { |
||
299 | $catArray = $this->getHandlersCategory(); |
||
300 | $i=1; |
||
301 | while (isset($catArray[$i]) && $i < 100) $i++; |
||
302 | if ($i == 100) throw new ProgrammingError('Category array error'); |
||
303 | $catArray[$i] = $catName; |
||
304 | $this->setHandlerCategory($catArray); |
||
305 | } |
||
306 | |||
307 | public function delHandlersCategory(int $catIndex) |
||
313 | } |
||
314 | |||
315 | public function renameHandlersCategory(int $catIndex, string $catName) |
||
316 | { |
||
317 | $catArray = $this->getHandlersCategory(); |
||
318 | $catArray[$catIndex] = $catName; |
||
319 | $this->setHandlerCategory($catArray); |
||
320 | } |
||
321 | |||
324 |