Total Complexity | 49 |
Total Lines | 346 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like DeviceLinux 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 DeviceLinux, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class DeviceLinux extends \core\DeviceConfig { |
||
41 | |||
42 | /** |
||
43 | * constructor. Sets supported EAP methods. |
||
44 | */ |
||
45 | final public function __construct() { |
||
46 | parent::__construct(); |
||
47 | $this->setSupportedEapMethods([\core\common\EAP::EAPTYPE_PEAP_MSCHAP2, \core\common\EAP::EAPTYPE_TTLS_PAP, \core\common\EAP::EAPTYPE_TTLS_MSCHAP2, \core\common\EAP::EAPTYPE_TLS, \core\common\EAP::EAPTYPE_SILVERBULLET]); |
||
48 | $this->specialities['media:openroaming'] = _("This device does not support provisioning of OpenRoaming."); |
||
49 | $this->specialities['media:consortium_OI'] = _("This device does not support provisioning of Passpoint networks."); |
||
50 | |||
51 | } |
||
52 | |||
53 | /** |
||
54 | * create the actual installer script |
||
55 | * |
||
56 | * @return string filename of the generated installer |
||
57 | * @throws Exception |
||
58 | * |
||
59 | */ |
||
60 | public function writeInstaller() { |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * produces the HTML text to be displayed when clicking on the "help" button |
||
79 | * besides the download button. |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public function writeDeviceInfo() { |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * writes a line of Python code into the installer script |
||
106 | * |
||
107 | * @param resource $file the file handle |
||
108 | * @param string $prefix prefix to write |
||
109 | * @param string $name config item to write |
||
110 | * @param string $text text to write |
||
111 | * @return void |
||
112 | */ |
||
113 | private function writeConfigLine($file, $prefix, $name, $text) { |
||
114 | $out = $prefix . $name . ' = "' . $text; |
||
115 | fwrite($file, wordwrap($out, 70, " \" \\\n \"") . "\n"); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * localises the user messages and writes them into the file |
||
120 | * |
||
121 | * @param resource $file the file resource of the installer script |
||
122 | * @return void |
||
123 | */ |
||
124 | private function writeMessages($file) { |
||
125 | \core\common\Entity::intoThePotatoes(); |
||
126 | $messages = [ |
||
127 | 'quit'=> _("Really quit?"), |
||
128 | 'username_prompt'=> _("enter your userid"), |
||
129 | 'enter_password' => _("enter password"), |
||
130 | 'enter_import_password' => _("enter your import password"), |
||
131 | 'incorrect_password' => _("incorrect password"), |
||
132 | 'repeat_password' => _("repeat your password"), |
||
133 | 'passwords_differ'=> _("passwords do not match"), |
||
134 | 'installation_finished' => _("Installation successful"), |
||
135 | 'cat_dir_exisits' => _("Directory {} exists; some of its files may be overwritten."), |
||
136 | 'cont' => _("Continue?"), |
||
137 | 'nm_not_supported' => _("This NetworkManager version is not supported"), |
||
138 | 'cert_error' => _("Certificate file not found, looks like a CAT error"), |
||
139 | 'unknown_version' => _("Unknown version"), |
||
140 | 'dbus_error' => _("DBus connection problem, a sudo might help"), |
||
141 | 'yes' => _("Y"), |
||
142 | 'no' => _("N"), |
||
143 | 'p12_filter' => _("personal certificate file (p12 or pfx)"), |
||
144 | 'all_filter' => _("All files"), |
||
145 | 'p12_title' => _("personal certificate file (p12 or pfx)"), |
||
146 | 'save_wpa_conf' => _("NetworkManager configuration failed, but we may generate a wpa_supplicant configuration file if you wish. Be warned that your connection password will be saved in this file as clear text."), |
||
147 | 'save_wpa_confirm' => _("Write the file"), |
||
148 | 'wrongUsernameFormat' =>_("Error: Your username must be of the form 'xxx@institutionID' e.g. '[email protected]'!"), |
||
149 | 'wrong_realm' => _("Error: your username must be in the form of 'xxx@{}'. Please enter the username in the correct format."), |
||
150 | 'wrong_realm_suffix' => _("Error: your username must be in the form of 'xxx@institutionID' and end with '{}'. Please enter the username in the correct format."), |
||
151 | 'user_cert_missing' => _("personal certificate file not found"), |
||
152 | ]; |
||
153 | foreach ($messages as $name => $value) { |
||
154 | $this->writeConfigLine($file, 'Messages.', $name, $value . '"'); |
||
155 | } |
||
156 | \core\common\Entity::outOfThePotatoes(); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * writes configuration variables into the installer script |
||
161 | * |
||
162 | * @param resource $file the file handle |
||
163 | * @return void |
||
164 | */ |
||
165 | private function writeConfigVars($file) { |
||
166 | $eapMethod = \core\common\EAP::eapDisplayName($this->selectedEap); |
||
167 | $contacts = $this->mkSupportContacts(); |
||
168 | $tou = $this->mkUserConsent(); |
||
169 | $outerId = $this->determineOuterIdString(); |
||
170 | $config = [ |
||
171 | 'instname' => $this->attributes['general:instname'][0], |
||
172 | 'profilename' => $this->attributes['profile:name'][0], |
||
173 | 'url' => $contacts['url'], |
||
174 | 'email' => $contacts['email'], |
||
175 | 'title' => "eduroam CAT", |
||
176 | 'server_match' => $this->glueServerNames(), |
||
177 | 'eap_outer' => $eapMethod['OUTER'], |
||
178 | 'eap_inner' => $eapMethod['INNER'], |
||
179 | 'init_info' => $this->mkIntro(), |
||
180 | 'init_confirmation' => $this->mkProfileConfirmation(), |
||
181 | // 'sb_user_file' => $this->mkSbUserFile(), |
||
182 | ]; |
||
183 | |||
184 | $configRaw = [ |
||
185 | 'ssids' => $this->mkSsidList(), |
||
186 | 'del_ssids' => $this->mkDelSsidList(), |
||
187 | 'servers' => $this->mkSubjectAltNameList(), |
||
188 | ]; |
||
189 | |||
190 | if ($this->selectedEap == \core\common\EAP::EAPTYPE_TLS && isset($this->attributes['eap-specific:tls_use_other_id']) && $this->attributes['eap-specific:tls_use_other_id'][0] == 'on') { |
||
191 | $configRaw['use_other_tls_id'] = "True"; |
||
192 | } |
||
193 | else { |
||
194 | $configRaw['use_other_tls_id'] = "False"; |
||
195 | } |
||
196 | |||
197 | if ($outerId !== NULL) { |
||
198 | $configRaw['anonymous_identity'] = '"' . $outerId . '"'; |
||
199 | } |
||
200 | |||
201 | if (!empty($this->attributes['internal:realm'][0])) { |
||
202 | $config['user_realm'] = $this->attributes['internal:realm'][0]; |
||
203 | } |
||
204 | |||
205 | if(!empty($this->attributes['internal:hint_userinput_suffix'][0]) && $this->attributes['internal:hint_userinput_suffix'][0] == 1) { |
||
206 | $configRaw['hint_user_input'] = "True"; |
||
207 | } |
||
208 | |||
209 | if(!empty($this->attributes['internal:verify_userinput_suffix'][0]) && $this->attributes['internal:verify_userinput_suffix'][0] == 1) { |
||
210 | $configRaw['verify_user_realm_input'] = "True"; |
||
211 | } |
||
212 | |||
213 | foreach ($config as $name => $value) { |
||
214 | $this->writeConfigLine($file, 'Config.', $name, $value . '"'); |
||
215 | } |
||
216 | |||
217 | foreach ($configRaw as $name => $value) { |
||
218 | fwrite($file, 'Config.' . $name . ' = ' . $value . "\n"); |
||
219 | } |
||
220 | |||
221 | if ($tou === '') { |
||
222 | fwrite($file, 'Config.tou = ""' . "\n"); |
||
223 | } else { |
||
224 | fwrite($file, 'Config.tou = """' . $tou . '"""' . "\n"); |
||
225 | } |
||
226 | |||
227 | fwrite($file, 'Config.CA = """' . $this->mkCAfile() . '"""' . "\n"); |
||
228 | $sbUserFile = $this->mkSbUserFile(); |
||
229 | if ($sbUserFile !== '') { |
||
230 | fwrite($file, 'Config.sb_user_file = """' . $sbUserFile . '"""' . "\n"); |
||
231 | } |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * coerces the list of EAP server names into a single string |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | private function glueServerNames() { |
||
240 | $serverList = $this->attributes['eap:server_name']; |
||
241 | if (!$serverList) { |
||
242 | return ''; |
||
243 | } |
||
244 | $A0 = array_reverse(explode('.', array_shift($serverList))); |
||
245 | $B = $A0; |
||
246 | foreach ($serverList as $oneServer) { |
||
247 | $A = array_reverse(explode('.', $oneServer)); |
||
248 | $B = array_intersect_assoc($A0, $A); |
||
249 | $A0 = $B; |
||
250 | } |
||
251 | return implode('.', array_reverse($B)); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * generates the list of support contacts |
||
256 | * |
||
257 | * @return array |
||
258 | */ |
||
259 | private function mkSupportContacts() { |
||
260 | $url = (!empty($this->attributes['support:url'][0])) ? $this->attributes['support:url'][0] : $this->support_url_substitute; |
||
261 | $email = (!empty($this->attributes['support:email'][0])) ? $this->attributes['support:email'][0] : $this->support_email_substitute; |
||
262 | return ['url'=>$url, 'email'=>$email]; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * generates the list of subjectAltNames to configure |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | private function mkSubjectAltNameList() { |
||
271 | $serverList = $this->attributes['eap:server_name']; |
||
272 | if (!$serverList) { |
||
273 | return ''; |
||
274 | } |
||
275 | $out = ''; |
||
276 | foreach ($serverList as $oneServer) { |
||
277 | if ($out) { |
||
278 | $out .= ', '; |
||
279 | } |
||
280 | $out .= "'DNS:$oneServer'"; |
||
281 | } |
||
282 | return "[" . $out. "]"; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * generates the list of SSIDs to configure |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | private function mkSsidList() { |
||
291 | $networks = $this->attributes['internal:networks']; |
||
292 | $outArray = []; |
||
293 | foreach ($networks as $network => $networkDetails) { |
||
294 | if (!empty($networkDetails['ssid'])) { |
||
295 | $outArray = array_merge($outArray, $networkDetails['ssid']); |
||
296 | } |
||
297 | } |
||
298 | return "['" . implode("', '", $outArray) . "']"; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * generates the list of SSIDs to delete from the system |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | private function mkDelSsidList() { |
||
307 | $outArray = []; |
||
308 | $delSSIDs = $this->attributes['internal:remove_SSID']; |
||
309 | foreach ($delSSIDs as $ssid => $cipher) { |
||
310 | if ($cipher == 'DEL') { |
||
311 | $outArray[] = "'$ssid'"; |
||
312 | } |
||
313 | } |
||
314 | return '[' . implode(', ', $outArray) . ']'; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * creates a blob containing all CA certificates |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | private function mkCAfile(){ |
||
323 | $out = ''; |
||
324 | $cAlist = $this->attributes['internal:CAs'][0]; |
||
325 | foreach ($cAlist as $oneCa) { |
||
326 | $out .= $oneCa['pem']; |
||
327 | } |
||
328 | return $out; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * generates the welcome text |
||
333 | * |
||
334 | * @return string |
||
335 | */ |
||
336 | private function mkIntro() { |
||
337 | \core\common\Entity::intoThePotatoes(); |
||
338 | $out = _("This installer has been prepared for {0}") . '\n\n' . _("More information and comments:") . '\n\nEMAIL: {1}\nWWW: {2}\n\n' . |
||
339 | _("Installer created with software from the GEANT project."); |
||
340 | \core\common\Entity::outOfThePotatoes(); |
||
341 | return $out; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * generates text for the user consent dialog box, if any |
||
346 | * |
||
347 | * @return string |
||
348 | */ |
||
349 | private function mkUserConsent() { |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * generates the warning that the account will only work for inst members |
||
361 | * |
||
362 | * @return string |
||
363 | */ |
||
364 | private function mkProfileConfirmation() { |
||
365 | \core\common\Entity::intoThePotatoes(); |
||
366 | if ($this->attributes['internal:profile_count'][0] > 1) { |
||
367 | $out = _("This installer will only work properly if you are a member of {0} and the user group: {1}."); |
||
368 | } else { |
||
369 | $out = _("This installer will only work properly if you are a member of {0}."); |
||
370 | } |
||
371 | \core\common\Entity::outOfThePotatoes(); |
||
372 | return $out; |
||
373 | } |
||
374 | |||
375 | |||
376 | /** |
||
377 | * generates the client certificate data for Silberbullet installers |
||
378 | * |
||
379 | * @return string |
||
380 | */ |
||
381 | private function mkSbUserFile() { |
||
386 | } |
||
387 | |||
388 | } |
||
389 |