Total Complexity | 54 |
Total Lines | 477 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like SettingsController 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 SettingsController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class SettingsController extends TrapsController |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * get param dberror or idoerror |
||
24 | * set errorDetected |
||
25 | */ |
||
26 | private function get_param() |
||
27 | { |
||
28 | $dberrorMsg=$this->params->get('dberror'); |
||
29 | if ($dberrorMsg != '') |
||
30 | { |
||
31 | $this->view->errorDetected=$dberrorMsg; |
||
32 | } |
||
33 | $dberrorMsg=$this->params->get('idodberror'); |
||
34 | if ($dberrorMsg != '') |
||
35 | { |
||
36 | $this->view->errorDetected=$dberrorMsg; |
||
37 | } |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Check empty configuration (and create one if needed) |
||
42 | * Setup : configErrorDetected |
||
43 | */ |
||
44 | private function check_empty_config() |
||
59 | } |
||
60 | |||
61 | } |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Check database and IDO database |
||
66 | * Setup : |
||
67 | * db_error : numerical error (trap db) 0=OK |
||
68 | * message : message (trap db) |
||
69 | * ido_db_error : numerical error 0=OK |
||
70 | * ido_message : message |
||
71 | */ |
||
72 | private function check_db() |
||
73 | { |
||
74 | $db_message=array( // index => ( message OK, message NOK, optional link if NOK ) |
||
75 | 0 => array('Database configuration OK','',''), |
||
76 | 1 => array('Database set in config.ini','No database in config.ini',''), |
||
77 | 2 => array('Database exists in Icingaweb2 config','Database does not exist in Icingaweb2 : ', |
||
78 | Url::fromPath('config/resource')), |
||
79 | 3 => array('Database credentials OK','Database does not exist/invalid credentials/no schema : ', |
||
80 | Url::fromPath('trapdirector/settings/createschema')), |
||
81 | 4 => array('Schema is set','Schema is not set for ', |
||
82 | Url::fromPath('trapdirector/settings/createschema')), |
||
83 | 5 => array('Schema is up to date','Schema is outdated :', |
||
84 | Url::fromPath('trapdirector/settings/updateschema')), |
||
85 | ); |
||
86 | |||
87 | try { |
||
88 | $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
||
89 | $dberror=array(0,''); |
||
90 | } catch (DBException $e) { |
||
91 | $dberror = $e->getArray(); |
||
92 | } |
||
93 | |||
94 | $this->view->db_error=$dberror[0]; |
||
95 | switch ($dberror[0]) |
||
96 | { |
||
97 | case 2: |
||
98 | case 4: |
||
99 | $db_message[$dberror[0]][1] .= $dberror[1]; |
||
100 | break; |
||
101 | case 3: |
||
102 | $db_message[$dberror[0]][1] .= $dberror[1] . ', Message : ' . $dberror[2]; |
||
103 | break; |
||
104 | case 5: |
||
105 | $db_message[$dberror[0]][1] .= ' version '. $dberror[1] . ', version needed : ' .$dberror[2]; |
||
106 | break; |
||
107 | case 0: |
||
108 | case 1: |
||
109 | break; |
||
110 | default: |
||
111 | new ProgrammingError('Out of bond result from database test'); |
||
112 | } |
||
113 | $this->view->message=$db_message; |
||
114 | |||
115 | try { |
||
116 | $this->getUIDatabase()->testGetIdoDb(); // Get DB in test mode |
||
117 | $dberror=array(0,''); |
||
118 | } catch (DBException $e) { |
||
119 | $dberror = $e->getArray(); |
||
120 | } |
||
121 | |||
122 | $this->view->ido_db_error=$dberror[0]; |
||
123 | $this->view->ido_message='IDO Database : ' . $dberror[1]; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Check API parameters |
||
128 | * Setup : |
||
129 | * apimessage |
||
130 | */ |
||
131 | private function check_api() |
||
132 | { |
||
133 | if ($this->Config()->get('config', 'icingaAPI_host') != '') |
||
134 | { |
||
135 | $apitest=new Icinga2API($this->Config()->get('config', 'icingaAPI_host'),$this->Config()->get('config', 'icingaAPI_port')); |
||
136 | $apitest->setCredentials($this->Config()->get('config', 'icingaAPI_user'), $this->Config()->get('config', 'icingaAPI_password')); |
||
137 | try { |
||
138 | list($this->view->apimessageError,$this->view->apimessage)=$apitest->test($this->getModuleConfig()::getapiUserPermissions()); |
||
139 | //$this->view->apimessageError=false; |
||
140 | } catch (RuntimeException $e) { |
||
141 | $this->view->apimessage='API config : ' . $e->getMessage(); |
||
142 | $this->view->apimessageError=true; |
||
143 | } |
||
144 | } |
||
145 | else |
||
146 | { |
||
147 | $this->view->apimessage='API parameters not configured'; |
||
148 | $this->view->apimessageError=true; |
||
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Check icingaweb2 etc path |
||
154 | * Setup : |
||
155 | * icingaEtcWarn : 0 if same than in trap_in.php, 1 if not |
||
156 | * icingaweb2_etc : path |
||
157 | */ |
||
158 | private function check_icingaweb_path() |
||
173 | } |
||
174 | } |
||
175 | |||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Get db list filtered by $allowed |
||
180 | * @param array $allowed : array of allowed database |
||
181 | * @return array : resource list |
||
182 | */ |
||
183 | private function get_db_list($allowed) |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Get php binary with path or NULL if not found. |
||
198 | * @return string|NULL |
||
199 | */ |
||
200 | private function get_php_binary() |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Index of configuration |
||
223 | * Params setup in $this->view : |
||
224 | * errorDetected : if db or ido was detected by another page |
||
225 | * configErrorDetected : error if empty configuration (or error wrting a new one). |
||
226 | * db_error : numerical error (trap db) 0=OK |
||
227 | * message : message (trap db) |
||
228 | * ido_db_error : numerical error 0=OK |
||
229 | * ido_message : message |
||
230 | * apimessage |
||
231 | * icingaEtcWarn : 0 if same than in trap_in.php, 1 if not |
||
232 | * icingaweb2_etc : path |
||
233 | **/ |
||
234 | public function indexAction() |
||
295 | |||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Satellite configuration |
||
300 | * Params setup in $this->view : |
||
301 | * errorDetected : if db or ido was detected by another page |
||
302 | * configErrorDetected : error if empty configuration (or error wrting a new one). |
||
303 | * db_error : numerical error (trap db) 0=OK |
||
304 | * message : message (trap db) |
||
305 | * ido_db_error : numerical error 0=OK |
||
306 | * ido_message : message |
||
307 | * apimessage |
||
308 | * icingaEtcWarn : 0 if same than in trap_in.php, 1 if not |
||
309 | * icingaweb2_etc : path |
||
310 | **/ |
||
311 | public function satelliteAction() |
||
323 | } |
||
324 | |||
325 | |||
326 | public function createschemaAction() |
||
381 | } |
||
382 | |||
383 | public function updateschemaAction() |
||
384 | { |
||
385 | $this->checkModuleConfigPermission(); |
||
386 | $this->getTabs()->add('get',array( |
||
387 | 'active' => true, |
||
388 | 'label' => $this->translate('Update Schema'), |
||
389 | 'url' => Url::fromRequest() |
||
390 | )); |
||
391 | // check if needed |
||
392 | $dberror=array(); |
||
|
|||
393 | try |
||
394 | { |
||
395 | $this->getUIDatabase()->testGetDb(); // Get DB in test mode |
||
396 | echo 'Schema already exists and is up to date<br>'; |
||
397 | return; |
||
398 | } |
||
399 | catch (DBException $e) |
||
400 | { |
||
401 | $dberror=$e->getArray(); |
||
402 | } |
||
403 | |||
404 | echo 'Return to <a href="' . Url::fromPath('trapdirector/settings') .'" class="link-button icon-wrench"> settings page </a><br><br>'; |
||
405 | |||
406 | if ($dberror[0] != 5) |
||
407 | { |
||
408 | echo 'Database does not exists or is not setup correctly<br>'; |
||
409 | return; |
||
410 | } |
||
411 | // setup |
||
412 | require_once($this->Module()->getBaseDir() .'/bin/trap_class.php'); |
||
413 | $icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc'); |
||
414 | $debug_level=4; |
||
415 | $Trap = new Trap($icingaweb2_etc); |
||
416 | |||
417 | |||
418 | $prefix=$this->Config()->get('config', 'database_prefix'); |
||
419 | $updateSchema=$this->Module()->getBaseDir() . '/SQL/'; |
||
420 | |||
421 | $target_version=$dberror[2]; |
||
422 | |||
423 | if ($this->params->get('msgok') == null) { |
||
424 | // Check for messages and display if any |
||
425 | echo "Upgrade databse is going to start.<br>Don't forget to backup your database before update<br>"; |
||
426 | $Trap->setLogging(2,'syslog'); |
||
427 | $message = $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix,true); |
||
428 | if ($message != '') |
||
429 | { |
||
430 | echo 'Note :<br><pre>'; |
||
431 | echo $message; |
||
432 | echo '</pre>'; |
||
433 | echo '<br>'; |
||
434 | echo '<a class="link-button" style="font-size:large;font-weight:bold" href="' . Url::fromPath('trapdirector/settings/updateschema') .'?msgok=1">Click here to update</a>'; |
||
435 | echo '<br>'; |
||
436 | return; |
||
437 | } |
||
438 | } |
||
439 | |||
440 | $Trap->setLogging($debug_level,'display'); |
||
441 | |||
442 | echo 'Updating schema to '. $target_version . ': <br>'; |
||
443 | echo '<pre>'; |
||
444 | |||
445 | $Trap->trapsDB->update_schema($updateSchema,$target_version,$prefix); |
||
446 | echo '</pre>'; |
||
447 | } |
||
448 | |||
449 | |||
450 | private function checkSnmpTrapd() |
||
496 | } |
||
497 | } |
||
499 |