| Total Complexity | 57 |
| Total Lines | 449 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DeviceXML 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 DeviceXML, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | abstract class DeviceXML extends \core\DeviceConfig { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * construct the device |
||
| 48 | */ |
||
| 49 | public function __construct() { |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * $langScope can be 'global' when all lang and all lang-specific information |
||
| 55 | * is dumped or 'single' when only the selected lang (and defaults) are passed |
||
| 56 | * NOTICE: 'global' is not yet supported |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | public $langScope; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * whether all EAP types should be included in the file or only the |
||
| 64 | * preferred one |
||
| 65 | * |
||
| 66 | * @var boolean |
||
| 67 | */ |
||
| 68 | public $allEaps = FALSE; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * vendor-specific additional information |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | public $VendorSpecific; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * create HTML code explaining the installer |
||
| 79 | * |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | public function writeDeviceInfo() { |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * create the actual XML file |
||
| 92 | * |
||
| 93 | * @return string filename of the generated installer |
||
| 94 | * @throws Exception |
||
| 95 | * |
||
| 96 | */ |
||
| 97 | public function writeInstaller() { |
||
| 98 | $attr = $this->attributes; |
||
| 99 | $NAMESPACE = 'urn:RFC4282:realm'; |
||
| 100 | //EAPIdentityProvider begin |
||
| 101 | $eapIdp = new EAPIdentityProvider(); |
||
| 102 | $eapIdp->setProperty('CredentialApplicability', $this->getCredentialApplicability()); |
||
| 103 | // $eap_idp->setProperty('ValidUntil',$this->getValidUntil()); |
||
| 104 | // ProviderInfo-> |
||
| 105 | $eapIdp->setProperty('ProviderInfo', $this->getProviderInfo()); |
||
| 106 | // TODO $eap_idp->setProperty('VendorSpecific',$this->getVendorSpecific()); |
||
| 107 | $methodList = []; |
||
| 108 | if ($this->allEaps) { |
||
| 109 | $eapmethods = []; |
||
| 110 | foreach ($attr['all_eaps'] as $eap) { |
||
| 111 | $eapRep = $eap->getArrayRep(); |
||
| 112 | if (in_array($eapRep, $this->supportedEapMethods)) { |
||
| 113 | $eapmethods[] = $eapRep; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } else { |
||
| 117 | $eapmethods = [$this->selectedEap]; |
||
| 118 | } |
||
| 119 | foreach ($eapmethods as $eap) { |
||
| 120 | $methodList[] = $this->getAuthMethod($eap); |
||
| 121 | } |
||
| 122 | $authMethods = new AuthenticationMethods(); |
||
| 123 | $authMethods->setProperty('AuthenticationMethods', $methodList); |
||
| 124 | $eapIdp->setProperty('AuthenticationMethods', $authMethods); |
||
| 125 | if (empty($attr['internal:realm'][0])) { |
||
| 126 | $eapIdp->setAttribute('ID', 'undefined'); |
||
| 127 | $eapIdp->setAttribute('namespace', 'urn:undefined'); |
||
| 128 | } else { |
||
| 129 | $eapIdp->setAttribute('ID', $attr['internal:realm'][0]); |
||
| 130 | $eapIdp->setAttribute('namespace', $NAMESPACE); |
||
| 131 | } |
||
| 132 | if ($this->langScope === 'single') { |
||
| 133 | $eapIdp->setAttribute('lang', $this->languageInstance->getLang()); |
||
| 134 | } |
||
| 135 | $eapIdp->setAttribute('version', '1'); |
||
| 136 | |||
| 137 | |||
| 138 | // EAPIdentityProvider end |
||
| 139 | // Generate XML |
||
| 140 | |||
| 141 | $rootname = 'EAPIdentityProviderList'; |
||
| 142 | $root = new \SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><{$rootname} xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"eap-metadata.xsd\"></{$rootname}>"); |
||
| 143 | |||
| 144 | marshalObject($root, $eapIdp); |
||
| 145 | $dom = dom_import_simplexml($root)->ownerDocument; |
||
| 146 | //TODO schema validation makes sense so probably should be used |
||
| 147 | if ($dom->schemaValidate(ROOT . '/devices/xml/eap-metadata.xsd') === FALSE) { |
||
| 148 | throw new Exception("Schema validation failed for eap-metadata"); |
||
| 149 | } |
||
| 150 | file_put_contents($this->installerBasename . '.eap-config', $dom->saveXML()); |
||
| 151 | return($this->installerBasename . '.eap-config'); |
||
| 152 | } |
||
| 153 | |||
| 154 | private const ATTRIBUTENAMES = [ |
||
| 155 | 'support:email' => 'EmailAddress', |
||
| 156 | 'support:url' => 'WebAddress', |
||
| 157 | 'support:phone' => 'Phone', |
||
| 158 | 'profile:description' => 'Description', |
||
| 159 | 'support:info_file' => 'TermsOfUse', |
||
| 160 | 'general:logo_file' => 'ProviderLogo', |
||
| 161 | ]; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * |
||
| 165 | * @param string $attrName the attribute name |
||
| 166 | * @return array of values for this attribute |
||
| 167 | */ |
||
| 168 | private function getSimpleMLAttribute($attrName) { |
||
| 169 | if (empty($this->attributes[$attrName][0])) { |
||
| 170 | return([]); |
||
| 171 | } |
||
| 172 | $attributeList = $this->attributes[$attrName]; |
||
| 173 | if (!isset(self::ATTRIBUTENAMES[$attrName])) { |
||
| 174 | $this->loggerInstance->debug(4, "Missing class definition for $attrName\n"); |
||
| 175 | return([]); |
||
| 176 | } |
||
| 177 | $className = "\devices\xml\\" . self::ATTRIBUTENAMES[$attrName]; |
||
| 178 | $objs = []; |
||
| 179 | if ($this->langScope === 'global') { |
||
| 180 | foreach ($attributeList['langs'] as $language => $value) { |
||
| 181 | $language = ($language === 'C' ? 'any' : $language); |
||
| 182 | $obj = new $className(); |
||
| 183 | $obj->setValue($value); |
||
| 184 | $obj->setAttributes(['lang' => $language]); |
||
| 185 | $objs[] = $obj; |
||
| 186 | } |
||
| 187 | } else { |
||
| 188 | $obj = new $className(); |
||
| 189 | $obj->setValue($attributeList[0]); |
||
| 190 | $objs[] = $obj; |
||
| 191 | } |
||
| 192 | return($objs); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * constructs the name of the institution and puts it into the XML. |
||
| 197 | * consists of the best-language-match inst name, and if the inst has more |
||
| 198 | * than one profile also the best-language-match profile name |
||
| 199 | * |
||
| 200 | * @return \devices\xml\DisplayName[] |
||
| 201 | */ |
||
| 202 | private function getDisplayName() { |
||
| 203 | $attr = $this->attributes; |
||
| 204 | $objs = []; |
||
| 205 | if ($this->langScope === 'global') { |
||
| 206 | $instNameLangs = $attr['general:instname']['langs']; |
||
| 207 | if ($attr['internal:profile_count'][0] > 1) { |
||
| 208 | $profileNameLangs = $attr['profile:name']['langs']; |
||
| 209 | } |
||
| 210 | foreach ($instNameLangs as $language => $value) { |
||
| 211 | $language = ($language === 'C' ? 'any' : $language); |
||
| 212 | $displayname = new DisplayName(); |
||
| 213 | if (isset($profileNameLangs)) { |
||
| 214 | $langOrC = isset($profileNameLangs[$language]) ? $profileNameLangs[$language] : $profileNameLangs['C']; |
||
| 215 | $value .= ' - ' . $langOrC; |
||
| 216 | } |
||
| 217 | $displayname->setValue($value); |
||
| 218 | $displayname->setAttributes(['lang' => $language]); |
||
| 219 | $objs[] = $displayname; |
||
| 220 | } |
||
| 221 | } else { |
||
| 222 | $displayname = new DisplayName(); |
||
| 223 | $value = $attr['general:instname'][0]; |
||
| 224 | if ($attr['internal:profile_count'][0] > 1) { |
||
| 225 | $value .= ' - ' . $attr['profile:name'][0]; |
||
| 226 | } |
||
| 227 | $displayname->setValue($value); |
||
| 228 | $objs[] = $displayname; |
||
| 229 | } |
||
| 230 | return $objs; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * retrieves the provider logo and puts it into the XML structure |
||
| 235 | * |
||
| 236 | * @return \devices\xml\ProviderLogo |
||
| 237 | */ |
||
| 238 | private function getProviderLogo() { |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * retrieves provider information and puts it into the XML structure. |
||
| 252 | * contains the profile description and the ToU file, if any |
||
| 253 | * |
||
| 254 | * @return \devices\xml\ProviderInfo |
||
| 255 | */ |
||
| 256 | private function getProviderInfo() { |
||
| 257 | $providerinfo = new ProviderInfo(); |
||
| 258 | $providerinfo->setProperty('DisplayName', $this->getDisplayName()); |
||
| 259 | $providerinfo->setProperty('Description', $this->getSimpleMLAttribute('profile:description')); |
||
| 260 | $providerinfo->setProperty('ProviderLocation', $this->getProviderLocation()); |
||
| 261 | $providerinfo->setProperty('ProviderLogo', $this->getProviderLogo()); |
||
| 262 | $providerinfo->setProperty('TermsOfUse', $this->getSimpleMLAttribute('support:info_file')); |
||
| 263 | $providerinfo->setProperty('Helpdesk', $this->getHelpdesk()); |
||
| 264 | return $providerinfo; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * retrieves the location information and puts it into the XML structure |
||
| 269 | * |
||
| 270 | * @return \devices\xml\ProviderLocation|\devices\xml\ProviderLocation[] |
||
| 271 | */ |
||
| 272 | private function getProviderLocation() { |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * retrieves helpdesk contact information and puts it into the XML structure |
||
| 298 | * |
||
| 299 | * @return \devices\xml\Helpdesk |
||
| 300 | */ |
||
| 301 | private function getHelpdesk() { |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * determine where this credential should be applicable |
||
| 311 | * |
||
| 312 | * @return \devices\xml\CredentialApplicability |
||
| 313 | */ |
||
| 314 | private function getCredentialApplicability() { |
||
| 315 | $ssids = $this->attributes['internal:SSID']; |
||
| 316 | $oids = $this->attributes['internal:consortia']; |
||
| 317 | $credentialapplicability = new CredentialApplicability(); |
||
| 318 | $ieee80211s = []; |
||
| 319 | foreach ($ssids as $ssid => $ciph) { |
||
| 320 | $ieee80211 = new IEEE80211(); |
||
| 321 | $ieee80211->setProperty('SSID', $ssid); |
||
| 322 | $ieee80211->setProperty('MinRSNProto', $ciph == 'AES' ? 'CCMP' : 'TKIP'); |
||
| 323 | $ieee80211s[] = $ieee80211; |
||
| 324 | } |
||
| 325 | foreach ($oids as $oid) { |
||
| 326 | $ieee80211 = new IEEE80211(); |
||
| 327 | $ieee80211->setProperty('ConsortiumOID', $oid); |
||
| 328 | $ieee80211s[] = $ieee80211; |
||
| 329 | } |
||
| 330 | $credentialapplicability->setProperty('IEEE80211', $ieee80211s); |
||
| 331 | return $credentialapplicability; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * retrieves the parameters needed for the given EAP method and creates |
||
| 336 | * appropriate nodes in the XML structure for them |
||
| 337 | * |
||
| 338 | * @param array $eap the EAP type in question |
||
| 339 | * @return array a recap of the findings |
||
| 340 | */ |
||
| 341 | private function getAuthenticationMethodParams($eap) { |
||
| 342 | $inner = \core\common\EAP::innerAuth($eap); |
||
| 343 | $outerMethod = $eap["OUTER"]; |
||
| 344 | |||
| 345 | if (isset($inner["METHOD"]) && $inner["METHOD"]) { |
||
| 346 | $innerauthmethod = new InnerAuthenticationMethod(); |
||
| 347 | $typeOfInner = "\devices\xml\\" . ($inner["EAP"] ? 'EAPMethod' : 'NonEAPAuthMethod'); |
||
| 348 | $eapmethod = new $typeOfInner(); |
||
| 349 | $eaptype = new Type(); |
||
| 350 | $eaptype->setValue($inner['METHOD']); |
||
| 351 | $eapmethod->setProperty('Type', $eaptype); |
||
| 352 | $innerauthmethod->setProperty($typeOfInner, $eapmethod); |
||
| 353 | return ['inner_method' => $innerauthmethod, 'methodID' => $outerMethod, 'inner_methodID' => $inner['METHOD']]; |
||
| 354 | } else { |
||
| 355 | return ['inner_method' => 0, 'methodID' => $outerMethod, 'inner_methodID' => 0]; |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * sets the server-side credentials for a given EAP type |
||
| 361 | * |
||
| 362 | * @param \core\common\EAP $eaptype the EAP type |
||
| 363 | * @return \devices\XML\ServerSideCredential |
||
| 364 | */ |
||
| 365 | private function setServerSideCredentials($eaptype) { |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * sets the realm information for the client-side credential |
||
| 392 | * |
||
| 393 | * @param \devices\XML\ClientSideCredential $clientsidecredential |
||
| 394 | * @return void |
||
| 395 | */ |
||
| 396 | private function setClientSideRealm ($clientsidecredential) { |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * sets the client certificate |
||
| 413 | * |
||
| 414 | * @return \devices\XML\ClientCertificate |
||
| 415 | */ |
||
| 416 | private function setClientCertificate() { |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * sets the client-side credentials for the given EAP type |
||
| 425 | * |
||
| 426 | * @param array $eapParams the EAP parameters |
||
| 427 | * @return \devices\XML\ClientSideCredential |
||
| 428 | */ |
||
| 429 | private function setClientSideCredentials($eapParams) { |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * sets the EAP method |
||
| 447 | * |
||
| 448 | * @param \core\common\EAP $eaptype |
||
| 449 | * @return \devices\XML\EAPMethod |
||
| 450 | */ |
||
| 451 | private function setEapMethod($eaptype) { |
||
| 452 | $eapmethod = new EAPMethod(); |
||
| 453 | $eapmethod->setProperty('Type', $eaptype); |
||
| 454 | if (isset($this->VendorSpecific)) { |
||
| 455 | $vendorspecifics = []; |
||
| 456 | foreach ($this->VendorSpecific as $vs) { |
||
| 457 | $vendorspecific = new VendorSpecific(); |
||
| 458 | $vs['value']->addAttribute('xsi:noNamespaceSchemaLocation', "xxx.xsd"); |
||
| 459 | $vendorspecific->setValue($vs['value']); |
||
| 460 | $vendorspecific->setAttributes(['vendor' => $vs['vendor']]); |
||
| 461 | $vendorspecifics[] = $vendorspecific; |
||
| 462 | } |
||
| 463 | $eapmethod->setProperty('VendorSpecific', $vendorspecifics); |
||
| 464 | } |
||
| 465 | return($eapmethod); |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * determines the authentication method to use |
||
| 470 | * |
||
| 471 | * @param \core\common\EAP $eap the EAP method |
||
| 472 | * @return \devices\xml\AuthenticationMethod |
||
| 473 | */ |
||
| 474 | private function getAuthMethod($eap) { |
||
| 493 | |||
| 494 | |||
| 495 | } |
||
| 496 | |||
| 497 | |||
| 498 | |||
| 500 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.