| Total Complexity | 43 |
| Total Lines | 414 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like RoleDescriptor 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 RoleDescriptor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class RoleDescriptor extends SignedElementHelper |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * The name of this descriptor element. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private $elementName; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The ID of this element. |
||
| 28 | * |
||
| 29 | * @var string|null |
||
| 30 | */ |
||
| 31 | public $ID = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * How long this element is valid, as a unix timestamp. |
||
| 35 | * |
||
| 36 | * @var int|null |
||
| 37 | */ |
||
| 38 | public $validUntil = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The length of time this element can be cached, as string. |
||
| 42 | * |
||
| 43 | * @var string|null |
||
| 44 | */ |
||
| 45 | public $cacheDuration = null; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * List of supported protocols. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | public $protocolSupportEnumeration = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Error URL for this role. |
||
| 56 | * |
||
| 57 | * @var string|null |
||
| 58 | */ |
||
| 59 | public $errorURL = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Extensions on this element. |
||
| 63 | * |
||
| 64 | * Array of extension elements. |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | public $Extensions = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * KeyDescriptor elements. |
||
| 72 | * |
||
| 73 | * Array of \SAML2\XML\md\KeyDescriptor elements. |
||
| 74 | * |
||
| 75 | * @var \SAML2\XML\md\KeyDescriptor[] |
||
| 76 | */ |
||
| 77 | public $KeyDescriptor = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Organization of this role. |
||
| 81 | * |
||
| 82 | * @var \SAML2\XML\md\Organization|null |
||
| 83 | */ |
||
| 84 | public $Organization = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * ContactPerson elements for this role. |
||
| 88 | * |
||
| 89 | * Array of \SAML2\XML\md\ContactPerson objects. |
||
| 90 | * |
||
| 91 | * @var \SAML2\XML\md\ContactPerson[] |
||
| 92 | */ |
||
| 93 | public $ContactPerson = []; |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * Initialize a RoleDescriptor. |
||
| 98 | * |
||
| 99 | * @param string $elementName The name of this element. |
||
| 100 | * @param \DOMElement|null $xml The XML element we should load. |
||
| 101 | * @throws \Exception |
||
| 102 | */ |
||
| 103 | protected function __construct(string $elementName, \DOMElement $xml = null) |
||
| 104 | { |
||
| 105 | parent::__construct($xml); |
||
| 106 | $this->elementName = $elementName; |
||
| 107 | |||
| 108 | if ($xml === null) { |
||
| 109 | return; |
||
| 110 | } |
||
| 111 | |||
| 112 | if ($xml->hasAttribute('ID')) { |
||
| 113 | $this->setID($xml->getAttribute('ID')); |
||
| 114 | } |
||
| 115 | if ($xml->hasAttribute('validUntil')) { |
||
| 116 | $this->setValidUntil(Utils::xsDateTimeToTimestamp($xml->getAttribute('validUntil'))); |
||
| 117 | } |
||
| 118 | if ($xml->hasAttribute('cacheDuration')) { |
||
| 119 | $this->setCacheDuration($xml->getAttribute('cacheDuration')); |
||
| 120 | } |
||
| 121 | |||
| 122 | if (!$xml->hasAttribute('protocolSupportEnumeration')) { |
||
| 123 | throw new \Exception('Missing protocolSupportEnumeration attribute on '.$xml->localName); |
||
| 124 | } |
||
| 125 | $this->setProtocolSupportEnumeration(preg_split('/[\s]+/', $xml->getAttribute('protocolSupportEnumeration'))); |
||
|
|
|||
| 126 | |||
| 127 | if ($xml->hasAttribute('errorURL')) { |
||
| 128 | $this->setErrorURL($xml->getAttribute('errorURL')); |
||
| 129 | } |
||
| 130 | |||
| 131 | $this->setExtensions(Extensions::getList($xml)); |
||
| 132 | |||
| 133 | foreach (Utils::xpQuery($xml, './saml_metadata:KeyDescriptor') as $kd) { |
||
| 134 | $this->addKeyDescriptor(new KeyDescriptor($kd)); |
||
| 135 | } |
||
| 136 | |||
| 137 | $organization = Utils::xpQuery($xml, './saml_metadata:Organization'); |
||
| 138 | if (count($organization) > 1) { |
||
| 139 | throw new \Exception('More than one Organization in the entity.'); |
||
| 140 | } elseif (!empty($organization)) { |
||
| 141 | $this->setOrganization(new Organization($organization[0])); |
||
| 142 | } |
||
| 143 | |||
| 144 | foreach (Utils::xpQuery($xml, './saml_metadata:ContactPerson') as $cp) { |
||
| 145 | $this->addContactPerson(new ContactPerson($cp)); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * Collect the value of the ID-property |
||
| 152 | * @return string|null |
||
| 153 | */ |
||
| 154 | public function getID() |
||
| 155 | { |
||
| 156 | return $this->ID; |
||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * Set the value of the ID-property |
||
| 162 | * @param string|null $Id |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | public function setID(string $Id = null) |
||
| 166 | { |
||
| 167 | $this->ID = $Id; |
||
| 168 | } |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * Collect the value of the validUntil-property |
||
| 173 | * @return int|null |
||
| 174 | */ |
||
| 175 | public function getValidUntil() |
||
| 176 | { |
||
| 177 | return $this->validUntil; |
||
| 178 | } |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Set the value of the validUntil-property |
||
| 183 | * @param int|null $validUntil |
||
| 184 | * @return void |
||
| 185 | */ |
||
| 186 | public function setValidUntil(int $validUntil = null) |
||
| 187 | { |
||
| 188 | $this->validUntil = $validUntil; |
||
| 189 | } |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * Collect the value of the cacheDuration-property |
||
| 194 | * @return string|null |
||
| 195 | */ |
||
| 196 | public function getCacheDuration() |
||
| 199 | } |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * Set the value of the cacheDuration-property |
||
| 204 | * @param string|null $cacheDuration |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | public function setCacheDuration(string $cacheDuration = null) |
||
| 208 | { |
||
| 209 | $this->cacheDuration = $cacheDuration; |
||
| 210 | } |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * Collect the value of the Extensions-property |
||
| 215 | * @return \SAML2\XML\Chunk[] |
||
| 216 | */ |
||
| 217 | public function getExtensions() : array |
||
| 220 | } |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Set the value of the Extensions-property |
||
| 225 | * @param array $extensions |
||
| 226 | * @return void |
||
| 227 | */ |
||
| 228 | public function setExtensions(array $extensions) |
||
| 231 | } |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * Add an Extension. |
||
| 236 | * |
||
| 237 | * @param \SAML2\XML\Chunk $extensions The Extensions |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | public function addExtension(Extensions $extension) |
||
| 241 | { |
||
| 242 | $this->Extensions[] = $extension; |
||
| 243 | } |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * Set the value of the errorURL-property |
||
| 248 | * @param string|null $errorURL |
||
| 249 | * @return void |
||
| 250 | */ |
||
| 251 | public function setErrorURL(string $errorURL = null) |
||
| 252 | { |
||
| 253 | if (!is_null($errorURL) && !filter_var($errorURL, FILTER_VALIDATE_URL)) { |
||
| 254 | throw new \InvalidArgumentException('RoleDescriptor errorURL is not a valid URL.'); |
||
| 255 | } |
||
| 256 | $this->errorURL = $errorURL; |
||
| 257 | } |
||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * Collect the value of the errorURL-property |
||
| 262 | * @return string|null |
||
| 263 | */ |
||
| 264 | public function getErrorURL() |
||
| 265 | { |
||
| 266 | return $this->errorURL; |
||
| 267 | } |
||
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * Collect the value of the ProtocolSupportEnumeration-property |
||
| 272 | * @return string[] |
||
| 273 | */ |
||
| 274 | public function getProtocolSupportEnumeration() : array |
||
| 275 | { |
||
| 276 | return $this->protocolSupportEnumeration; |
||
| 277 | } |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * Set the value of the ProtocolSupportEnumeration-property |
||
| 282 | * @param array $protocols |
||
| 283 | * @return void |
||
| 284 | */ |
||
| 285 | public function setProtocolSupportEnumeration(array $protocols) |
||
| 286 | { |
||
| 287 | $this->protocolSupportEnumeration = $protocols; |
||
| 288 | } |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * Add the value to the ProtocolSupportEnumeration-property |
||
| 293 | * @param string $protocol |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | public function addProtocolSupportEnumeration(string $protocol) |
||
| 297 | { |
||
| 298 | $this->protocolSupportEnumeration[] = $protocol; |
||
| 299 | } |
||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * Collect the value of the Organization-property |
||
| 304 | * @return \SAML2\XML\md\Organization|null |
||
| 305 | */ |
||
| 306 | public function getOrganization() |
||
| 307 | { |
||
| 308 | return $this->Organization; |
||
| 309 | } |
||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Set the value of the Organization-property |
||
| 314 | * @param \SAML2\XML\md\Organization|null $organization |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | public function setOrganization(Organization $organization = null) |
||
| 318 | { |
||
| 319 | $this->Organization = $organization; |
||
| 320 | } |
||
| 321 | |||
| 322 | |||
| 323 | /** |
||
| 324 | * Collect the value of the ContactPerson-property |
||
| 325 | * @return \SAML2\XML\md\ContactPerson[] |
||
| 326 | */ |
||
| 327 | public function getContactPerson() : array |
||
| 330 | } |
||
| 331 | |||
| 332 | |||
| 333 | /** |
||
| 334 | * Set the value of the ContactPerson-property |
||
| 335 | * @param array $contactPerson |
||
| 336 | * @return void |
||
| 337 | */ |
||
| 338 | public function setContactPerson(array $contactPerson) |
||
| 339 | { |
||
| 340 | $this->ContactPerson = $contactPerson; |
||
| 341 | } |
||
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * Add the value to the ContactPerson-property |
||
| 346 | * @param \SAML2\XML\md\ContactPerson $contactPerson |
||
| 347 | * @return void |
||
| 348 | */ |
||
| 349 | public function addContactPerson(ContactPerson $contactPerson) |
||
| 352 | } |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * Collect the value of the KeyDescriptor-property |
||
| 357 | * @return \SAML2\XML\md\KeyDescriptor[] |
||
| 358 | */ |
||
| 359 | public function getKeyDescriptor() : array |
||
| 362 | } |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * Set the value of the KeyDescriptor-property |
||
| 367 | * @param array $keyDescriptor |
||
| 368 | * @return void |
||
| 369 | */ |
||
| 370 | public function setKeyDescriptor(array $keyDescriptor) |
||
| 371 | { |
||
| 372 | $this->KeyDescriptor = $keyDescriptor; |
||
| 373 | } |
||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * Add the value to the KeyDescriptor-property |
||
| 378 | * @param \SAML2\XML\md\KeyDescriptor $keyDescriptor |
||
| 379 | * @return void |
||
| 380 | */ |
||
| 381 | public function addKeyDescriptor(KeyDescriptor $keyDescriptor) |
||
| 384 | } |
||
| 385 | |||
| 386 | |||
| 387 | /** |
||
| 388 | * Add this RoleDescriptor to an EntityDescriptor. |
||
| 389 | * |
||
| 390 | * @param \DOMElement $parent The EntityDescriptor we should append this endpoint to. |
||
| 391 | * @return \DOMElement |
||
| 392 | */ |
||
| 393 | protected function toXML(\DOMElement $parent) : \DOMElement |
||
| 431 | } |
||
| 432 | } |
||
| 433 |