| Total Complexity | 59 |
| Total Lines | 482 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like EntityDescriptor 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 EntityDescriptor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class EntityDescriptor extends SignedElementHelper |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * The entityID this EntityDescriptor represents. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | public $entityID; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The ID of this element. |
||
| 29 | * |
||
| 30 | * @var string|null |
||
| 31 | */ |
||
| 32 | public $ID = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * How long this element is valid, as a unix timestamp. |
||
| 36 | * |
||
| 37 | * @var int|null |
||
| 38 | */ |
||
| 39 | public $validUntil = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The length of time this element can be cached, as string. |
||
| 43 | * |
||
| 44 | * @var string|null |
||
| 45 | */ |
||
| 46 | public $cacheDuration = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Extensions on this element. |
||
| 50 | * |
||
| 51 | * Array of extension elements. |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | public $Extensions = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Array with all roles for this entity. |
||
| 59 | * |
||
| 60 | * Array of \SAML2\XML\md\RoleDescriptor objects (and subclasses of RoleDescriptor). |
||
| 61 | * |
||
| 62 | * @var (\SAML2\XML\md\UnknownRoleDescriptor|\SAML2\XML\md\IDPSSODescriptor|\SAML2\XML\md\SPSSODescriptor|\SAML2\XML\md\AuthnAuthorityDescriptor|\SAML2\XML\md\AttributeAuthorityDescriptor|\SAML2\XML\md\PDPDescriptor)[] |
||
| 63 | */ |
||
| 64 | public $RoleDescriptor = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * AffiliationDescriptor of this entity. |
||
| 68 | * |
||
| 69 | * @var \SAML2\XML\md\AffiliationDescriptor|null |
||
| 70 | */ |
||
| 71 | public $AffiliationDescriptor = null; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Organization of this entity. |
||
| 75 | * |
||
| 76 | * @var \SAML2\XML\md\Organization|null |
||
| 77 | */ |
||
| 78 | public $Organization = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * ContactPerson elements for this entity. |
||
| 82 | * |
||
| 83 | * @var \SAML2\XML\md\ContactPerson[] |
||
| 84 | */ |
||
| 85 | public $ContactPerson = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * AdditionalMetadataLocation elements for this entity. |
||
| 89 | * |
||
| 90 | * @var \SAML2\XML\md\AdditionalMetadataLocation[] |
||
| 91 | */ |
||
| 92 | public $AdditionalMetadataLocation = []; |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Initialize an EntitiyDescriptor. |
||
| 97 | * |
||
| 98 | * @param \DOMElement|null $xml The XML element we should load. |
||
| 99 | * @throws \Exception |
||
| 100 | */ |
||
| 101 | public function __construct(\DOMElement $xml = null) |
||
| 102 | { |
||
| 103 | parent::__construct($xml); |
||
| 104 | |||
| 105 | if ($xml === null) { |
||
| 106 | return; |
||
| 107 | } |
||
| 108 | |||
| 109 | if (!$xml->hasAttribute('entityID')) { |
||
| 110 | throw new \Exception('Missing required attribute entityID on EntityDescriptor.'); |
||
| 111 | } |
||
| 112 | $this->setEntityID($xml->getAttribute('entityID')); |
||
| 113 | |||
| 114 | if ($xml->hasAttribute('ID')) { |
||
| 115 | $this->setID($xml->getAttribute('ID')); |
||
| 116 | } |
||
| 117 | if ($xml->hasAttribute('validUntil')) { |
||
| 118 | $this->setValidUntil(Utils::xsDateTimeToTimestamp($xml->getAttribute('validUntil'))); |
||
| 119 | } |
||
| 120 | if ($xml->hasAttribute('cacheDuration')) { |
||
| 121 | $this->setCacheDuration($xml->getAttribute('cacheDuration')); |
||
| 122 | } |
||
| 123 | |||
| 124 | $this->setExtensions(Extensions::getList($xml)); |
||
| 125 | |||
| 126 | for ($node = $xml->firstChild; $node !== null; $node = $node->nextSibling) { |
||
| 127 | if (!($node instanceof \DOMElement)) { |
||
| 128 | continue; |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($node->namespaceURI !== Constants::NS_MD) { |
||
| 132 | continue; |
||
| 133 | } |
||
| 134 | |||
| 135 | switch ($node->localName) { |
||
| 136 | case 'RoleDescriptor': |
||
| 137 | $this->addRoleDescriptor(new UnknownRoleDescriptor($node)); |
||
| 138 | break; |
||
| 139 | case 'IDPSSODescriptor': |
||
| 140 | $this->addRoleDescriptor(new IDPSSODescriptor($node)); |
||
| 141 | break; |
||
| 142 | case 'SPSSODescriptor': |
||
| 143 | $this->addRoleDescriptor(new SPSSODescriptor($node)); |
||
| 144 | break; |
||
| 145 | case 'AuthnAuthorityDescriptor': |
||
| 146 | $this->addRoleDescriptor(new AuthnAuthorityDescriptor($node)); |
||
| 147 | break; |
||
| 148 | case 'AttributeAuthorityDescriptor': |
||
| 149 | $this->addRoleDescriptor(new AttributeAuthorityDescriptor($node)); |
||
| 150 | break; |
||
| 151 | case 'PDPDescriptor': |
||
| 152 | $this->addRoleDescriptor(new PDPDescriptor($node)); |
||
| 153 | break; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | $affiliationDescriptor = Utils::xpQuery($xml, './saml_metadata:AffiliationDescriptor'); |
||
| 158 | if (count($affiliationDescriptor) > 1) { |
||
| 159 | throw new \Exception('More than one AffiliationDescriptor in the entity.'); |
||
| 160 | } elseif (!empty($affiliationDescriptor)) { |
||
| 161 | $this->setAffiliationDescriptor(new AffiliationDescriptor($affiliationDescriptor[0])); |
||
| 162 | } |
||
| 163 | |||
| 164 | $roleDescriptor = $this->getRoleDescriptor(); |
||
| 165 | if (empty($roleDescriptor) && is_null($this->getAffiliationDescriptor())) { |
||
| 166 | throw new \Exception('Must have either one of the RoleDescriptors or an AffiliationDescriptor in EntityDescriptor.'); |
||
| 167 | } elseif (!empty($roleDescriptor) && !is_null($this->getAffiliationDescriptor())) { |
||
| 168 | throw new \Exception('AffiliationDescriptor cannot be combined with other RoleDescriptor elements in EntityDescriptor.'); |
||
| 169 | } |
||
| 170 | |||
| 171 | $organization = Utils::xpQuery($xml, './saml_metadata:Organization'); |
||
| 172 | if (count($organization) > 1) { |
||
| 173 | throw new \Exception('More than one Organization in the entity.'); |
||
| 174 | } elseif (!empty($organization)) { |
||
| 175 | $this->setOrganization(new Organization($organization[0])); |
||
| 176 | } |
||
| 177 | |||
| 178 | foreach (Utils::xpQuery($xml, './saml_metadata:ContactPerson') as $cp) { |
||
| 179 | $this->addContactPerson(new ContactPerson($cp)); |
||
| 180 | } |
||
| 181 | |||
| 182 | foreach (Utils::xpQuery($xml, './saml_metadata:AdditionalMetadataLocation') as $aml) { |
||
| 183 | $this->addAdditionalMetadataLocation(new AdditionalMetadataLocation($aml)); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * Collect the value of the entityID-property |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function getEntityID() : string |
||
| 193 | { |
||
| 194 | return $this->entityID; |
||
| 195 | } |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Set the value of the entityID-property |
||
| 200 | * @param string|null $entityId |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function setEntityID(string $entityId = null) |
||
| 204 | { |
||
| 205 | $this->entityID = $entityId; |
||
| 206 | } |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Collect the value of the ID-property |
||
| 211 | * @return string|null |
||
| 212 | */ |
||
| 213 | public function getID() |
||
| 214 | { |
||
| 215 | return $this->ID; |
||
| 216 | } |
||
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * Set the value of the ID-property |
||
| 221 | * @param string|null $Id |
||
| 222 | * @return void |
||
| 223 | */ |
||
| 224 | public function setID(string $Id = null) |
||
| 225 | { |
||
| 226 | $this->ID = $Id; |
||
| 227 | } |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * Collect the value of the validUntil-property |
||
| 232 | * @return int|null |
||
| 233 | */ |
||
| 234 | public function getValidUntil() |
||
| 237 | } |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * Set the value of the validUntil-property |
||
| 242 | * @param int|null $validUntil |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | public function setValidUntil(int $validUntil = null) |
||
| 246 | { |
||
| 247 | $this->validUntil = $validUntil; |
||
| 248 | } |
||
| 249 | |||
| 250 | |||
| 251 | /** |
||
| 252 | * Collect the value of the cacheDuration-property |
||
| 253 | * @return string|null |
||
| 254 | */ |
||
| 255 | public function getCacheDuration() |
||
| 256 | { |
||
| 257 | return $this->cacheDuration; |
||
| 258 | } |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Set the value of the cacheDuration-property |
||
| 263 | * @param string|null $cacheDuration |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | public function setCacheDuration(string $cacheDuration = null) |
||
| 267 | { |
||
| 268 | $this->cacheDuration = $cacheDuration; |
||
| 269 | } |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * Collect the value of the Extensions-property |
||
| 274 | * @return \SAML2\XML\Chunk[] |
||
| 275 | */ |
||
| 276 | public function getExtensions() : array |
||
| 277 | { |
||
| 278 | return $this->Extensions; |
||
| 279 | } |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Set the value of the Extensions-property |
||
| 284 | * @param array $extensions |
||
| 285 | * @return void |
||
| 286 | */ |
||
| 287 | public function setExtensions(array $extensions) |
||
| 288 | { |
||
| 289 | $this->Extensions = $extensions; |
||
| 290 | } |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * Add an Extension. |
||
| 295 | * |
||
| 296 | * @param \SAML2\XML\Chunk $extensions The Extensions |
||
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | public function addExtension(Extensions $extension) |
||
| 300 | { |
||
| 301 | $this->Extensions[] = $extension; |
||
| 302 | } |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * Collect the value of the RoleDescriptor-property |
||
| 307 | * @return \SAML2\XML\md\RoleDescriptor[] |
||
| 308 | */ |
||
| 309 | public function getRoleDescriptor() : array |
||
| 310 | { |
||
| 311 | return $this->RoleDescriptor; |
||
| 312 | } |
||
| 313 | |||
| 314 | |||
| 315 | /** |
||
| 316 | * Set the value of the RoleDescriptor-property |
||
| 317 | * @param array $roleDescriptor |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | public function setRoleDescriptor(array $roleDescriptor) |
||
| 321 | { |
||
| 322 | $this->RoleDescriptor = $roleDescriptor; |
||
| 323 | } |
||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * Add the value to the RoleDescriptor-property |
||
| 328 | * @param \SAML2\XML\md\RoleDescriptor $roleDescriptor |
||
| 329 | * @return void |
||
| 330 | */ |
||
| 331 | public function addRoleDescriptor(RoleDescriptor $roleDescriptor) |
||
| 332 | { |
||
| 333 | $this->RoleDescriptor[] = $roleDescriptor; |
||
| 334 | } |
||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * Collect the value of the AffiliationDescriptor-property |
||
| 339 | * @return \SAML2\XML\md\AffiliationDescriptor|null |
||
| 340 | */ |
||
| 341 | public function getAffiliationDescriptor() |
||
| 342 | { |
||
| 343 | return $this->AffiliationDescriptor; |
||
| 344 | } |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * Set the value of the AffliationDescriptor-property |
||
| 349 | * @param \SAML2\XML\md\AffiliationDescriptor|null $affiliationDescriptor |
||
| 350 | * @return void |
||
| 351 | */ |
||
| 352 | public function setAffiliationDescriptor(AffiliationDescriptor $affiliationDescriptor = null) |
||
| 355 | } |
||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * Collect the value of the Organization-property |
||
| 360 | * @return \SAML2\XML\md\Organization|null |
||
| 361 | */ |
||
| 362 | public function getOrganization() |
||
| 363 | { |
||
| 364 | return $this->Organization; |
||
| 365 | } |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * Set the value of the Organization-property |
||
| 370 | * @param \SAML2\XML\md\Organization|null $organization |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | public function setOrganization(Organization $organization = null) |
||
| 374 | { |
||
| 375 | $this->Organization = $organization; |
||
| 376 | } |
||
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * Collect the value of the ContactPerson-property |
||
| 381 | * @return \SAML2\XML\md\ContactPerson[] |
||
| 382 | */ |
||
| 383 | public function getContactPerson() : array |
||
| 384 | { |
||
| 385 | return $this->ContactPerson; |
||
| 386 | } |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * Set the value of the ContactPerson-property |
||
| 391 | * @param array $contactPerson |
||
| 392 | * @return void |
||
| 393 | */ |
||
| 394 | public function setContactPerson(array $contactPerson) |
||
| 395 | { |
||
| 396 | $this->ContactPerson = $contactPerson; |
||
| 397 | } |
||
| 398 | |||
| 399 | |||
| 400 | /** |
||
| 401 | * Add the value to the ContactPerson-property |
||
| 402 | * @param \SAML2\XML\md\ContactPerson $contactPerson |
||
| 403 | * @return void |
||
| 404 | */ |
||
| 405 | public function addContactPerson(ContactPerson $contactPerson) |
||
| 408 | } |
||
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * Collect the value of the AdditionalMetadataLocation-property |
||
| 413 | * @return \SAML2\XML\md\AdditionalMetadataLocation[] |
||
| 414 | */ |
||
| 415 | public function getAdditionalMetadataLocation() : array |
||
| 416 | { |
||
| 417 | return $this->AdditionalMetadataLocation; |
||
| 418 | } |
||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * Set the value of the AdditionalMetadataLocation-property |
||
| 423 | * @param array $additionalMetadataLocation |
||
| 424 | * @return void |
||
| 425 | */ |
||
| 426 | public function setAdditionalMetadataLocation(array $additionalMetadataLocation) |
||
| 427 | { |
||
| 428 | $this->AdditionalMetadataLocation = $additionalMetadataLocation; |
||
| 429 | } |
||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * Add the value to the AdditionalMetadataLocation-property |
||
| 434 | * @param AdditionalMetadataLocation $additionalMetadataLocation |
||
| 435 | * @return void |
||
| 436 | */ |
||
| 437 | public function addAdditionalMetadataLocation(AdditionalMetadataLocation $additionalMetadataLocation) |
||
| 440 | } |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * Create this EntityDescriptor. |
||
| 445 | * |
||
| 446 | * @param \DOMElement|null $parent The EntitiesDescriptor we should append this EntityDescriptor to. |
||
| 447 | * @return \DOMElement |
||
| 448 | */ |
||
| 449 | public function toXML(\DOMElement $parent = null) : \DOMElement |
||
| 450 | { |
||
| 451 | if ($parent === null) { |
||
| 452 | $doc = DOMDocumentFactory::create(); |
||
| 453 | $e = $doc->createElementNS(Constants::NS_MD, 'md:EntityDescriptor'); |
||
| 454 | $doc->appendChild($e); |
||
| 500 | } |
||
| 501 | } |
||
| 502 |