Total Complexity | 53 |
Total Lines | 448 |
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() |
||
235 | { |
||
236 | |||
237 | // CHeck permissions : display tests in any case, but no configuration. |
||
238 | $this->view->configPermission=$this->checkModuleConfigPermission(1); |
||
239 | // But check read permission |
||
240 | $this->checkReadPermission(); |
||
241 | |||
242 | $this->view->tabs = $this->Module()->getConfigTabs()->activate('config'); |
||
243 | |||
244 | // Get message : sent on configuration problems detected by controllers |
||
245 | $this->get_param(); |
||
246 | |||
247 | // Test if configuration exists, if not create for installer script |
||
248 | $this->check_empty_config(); |
||
249 | |||
250 | // Test Database |
||
251 | $this->check_db(); |
||
252 | |||
253 | //********* Test API |
||
254 | $this->check_api(); |
||
255 | |||
256 | //*********** Test snmptrapd alive and options |
||
257 | list ($this->view->snmptrapdError, $this->view->snmptrapdMessage) = $this->checkSnmpTrapd(); |
||
258 | |||
259 | // List DB in $ressources |
||
260 | $resources = $this->get_db_list(array('mysql', 'pgsql')); |
||
261 | |||
262 | // Check standard Icingaweb2 path |
||
263 | $this->check_icingaweb_path(); |
||
264 | |||
265 | $phpBinary = $this->get_php_binary(); |
||
266 | if ($phpBinary == null) |
||
|
|||
267 | { |
||
268 | $phpBinary = ' PHP BINARY NOT FOUND '; |
||
269 | |||
270 | } |
||
271 | |||
272 | // Setup path for mini documentation |
||
273 | $this->view->traps_in_config= $phpBinary . ' ' . $this->Module()->getBaseDir() . '/bin/trap_in.php'; |
||
274 | |||
275 | $this->view->installer= $this->Module()->getBaseDir() . '/bin/installer.sh ' |
||
276 | . ' -c all ' |
||
277 | . ' -d ' . $this->Module()->getBaseDir() |
||
278 | . ' -p ' . $phpBinary |
||
279 | . ' -a ' . exec('whoami') |
||
280 | . ' -w ' . Icinga::app()->getConfigDir(); |
||
281 | |||
282 | // ******************* configuration form setup******************* |
||
283 | $this->view->form = $form = new TrapsConfigForm(); |
||
284 | |||
285 | // set default paths; |
||
286 | $this->view->form->setPaths($this->Module()->getBaseDir(),Icinga::app()->getConfigDir()); |
||
287 | |||
288 | // set default ido database |
||
289 | $this->view->form->setDefaultIDODB($this->Config()->module('monitoring','backends')->get('icinga','resource')); |
||
290 | |||
291 | // Make form handle request. |
||
292 | $form->setIniConfig($this->Config()) |
||
293 | ->setDBList($resources) |
||
294 | ->handleRequest(); |
||
295 | |||
296 | } |
||
297 | |||
298 | public function createschemaAction() |
||
353 | } |
||
354 | |||
355 | public function updateschemaAction() |
||
419 | } |
||
420 | |||
421 | private function checkSnmpTrapd() |
||
470 |