| Total Complexity | 64 |
| Total Lines | 468 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 20 | class EntityDescriptor extends Metadata |
||
| 21 | { |
||
| 22 | /** @var string */ |
||
| 23 | protected $entityID; |
||
| 24 | |||
| 25 | /** @var int|null */ |
||
| 26 | protected $validUntil; |
||
| 27 | |||
| 28 | /** @var string|null */ |
||
| 29 | protected $cacheDuration; |
||
| 30 | |||
| 31 | /** @var string|null */ |
||
| 32 | protected $id; |
||
| 33 | |||
| 34 | /** @var Signature|null */ |
||
| 35 | protected $signature; |
||
| 36 | |||
| 37 | /** @var IdpSsoDescriptor[]|SpSsoDescriptor[] */ |
||
| 38 | protected $items; |
||
| 39 | |||
| 40 | /** @var Organization[]|null */ |
||
| 41 | protected $organizations; |
||
| 42 | |||
| 43 | /** @var ContactPerson[]|null */ |
||
| 44 | protected $contactPersons; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param string $filename |
||
| 48 | * |
||
| 49 | * @return EntityDescriptor |
||
| 50 | */ |
||
| 51 | public static function load($filename) |
||
| 52 | { |
||
| 53 | return self::loadXml(file_get_contents($filename)); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $xml |
||
| 58 | * |
||
| 59 | * @return EntityDescriptor |
||
| 60 | */ |
||
| 61 | public static function loadXml($xml) |
||
| 62 | { |
||
| 63 | $context = new DeserializationContext(); |
||
| 64 | $context->getDocument()->loadXML($xml); |
||
| 65 | $ed = new self(); |
||
| 66 | $ed->deserialize($context->getDocument(), $context); |
||
| 67 | |||
| 68 | return $ed; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string|null $entityId |
||
| 73 | */ |
||
| 74 | public function __construct($entityId = null, array $items = []) |
||
| 75 | { |
||
| 76 | $this->entityID = $entityId; |
||
| 77 | $this->items = $items; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return EntityDescriptor |
||
| 82 | */ |
||
| 83 | public function addContactPerson(ContactPerson $contactPerson) |
||
| 84 | { |
||
| 85 | if (false == is_array($this->contactPersons)) { |
||
|
|
|||
| 86 | $this->contactPersons = []; |
||
| 87 | } |
||
| 88 | $this->contactPersons[] = $contactPerson; |
||
| 89 | |||
| 90 | return $this; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return ContactPerson[]|null |
||
| 95 | */ |
||
| 96 | public function getAllContactPersons() |
||
| 97 | { |
||
| 98 | return $this->contactPersons; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return ContactPerson|null |
||
| 103 | */ |
||
| 104 | public function getFirstContactPerson() |
||
| 105 | { |
||
| 106 | if (is_array($this->contactPersons) && isset($this->contactPersons[0])) { |
||
| 107 | return $this->contactPersons[0]; |
||
| 108 | } |
||
| 109 | |||
| 110 | return null; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param \LightSaml\Model\Metadata\Organization $organization |
||
| 115 | * |
||
| 116 | * @return EntityDescriptor |
||
| 117 | */ |
||
| 118 | public function addOrganization(Organization $organization) |
||
| 119 | { |
||
| 120 | if (false == is_array($this->organizations)) { |
||
| 121 | $this->organizations = []; |
||
| 122 | } |
||
| 123 | $this->organizations[] = $organization; |
||
| 124 | |||
| 125 | return $this; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return Organization[]|null |
||
| 130 | */ |
||
| 131 | public function getAllOrganizations() |
||
| 132 | { |
||
| 133 | return $this->organizations; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return \LightSaml\Model\Metadata\Organization|null |
||
| 138 | */ |
||
| 139 | public function getFirstOrganization() |
||
| 140 | { |
||
| 141 | if (is_array($this->organizations) && isset($this->organizations[0])) { |
||
| 142 | return $this->organizations[0]; |
||
| 143 | } |
||
| 144 | |||
| 145 | return null; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string|null $cacheDuration |
||
| 150 | * |
||
| 151 | * @throws \InvalidArgumentException |
||
| 152 | * |
||
| 153 | * @return EntityDescriptor |
||
| 154 | */ |
||
| 155 | public function setCacheDuration($cacheDuration) |
||
| 156 | { |
||
| 157 | Helper::validateDurationString($cacheDuration); |
||
| 158 | |||
| 159 | $this->cacheDuration = $cacheDuration; |
||
| 160 | |||
| 161 | return $this; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return string|null |
||
| 166 | */ |
||
| 167 | public function getCacheDuration() |
||
| 168 | { |
||
| 169 | return $this->cacheDuration; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $entityID |
||
| 174 | * |
||
| 175 | * @return EntityDescriptor |
||
| 176 | */ |
||
| 177 | public function setEntityID($entityID) |
||
| 178 | { |
||
| 179 | $this->entityID = (string) $entityID; |
||
| 180 | |||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getEntityID() |
||
| 188 | { |
||
| 189 | return $this->entityID; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string|null $id |
||
| 194 | * |
||
| 195 | * @return EntityDescriptor |
||
| 196 | */ |
||
| 197 | public function setID($id) |
||
| 198 | { |
||
| 199 | $this->id = null !== $id ? (string) $id : null; |
||
| 200 | |||
| 201 | return $this; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return string|null |
||
| 206 | */ |
||
| 207 | public function getID() |
||
| 208 | { |
||
| 209 | return $this->id; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param \LightSaml\Model\Metadata\IdpSsoDescriptor|\LightSaml\Model\Metadata\SpSsoDescriptor $item |
||
| 214 | * |
||
| 215 | * @throws \InvalidArgumentException |
||
| 216 | * |
||
| 217 | * @return EntityDescriptor |
||
| 218 | */ |
||
| 219 | public function addItem($item) |
||
| 220 | { |
||
| 221 | if (false == $item instanceof IdpSsoDescriptor && |
||
| 222 | false == $item instanceof SpSsoDescriptor |
||
| 223 | ) { |
||
| 224 | throw new \InvalidArgumentException('EntityDescriptor item must be IdpSsoDescriptor or SpSsoDescriptor'); |
||
| 225 | } |
||
| 226 | |||
| 227 | if (false == is_array($this->items)) { |
||
| 228 | $this->items = []; |
||
| 229 | } |
||
| 230 | |||
| 231 | $this->items[] = $item; |
||
| 232 | |||
| 233 | return $this; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return IdpSsoDescriptor[]|SpSsoDescriptor[]|SSODescriptor[] |
||
| 238 | */ |
||
| 239 | public function getAllItems() |
||
| 240 | { |
||
| 241 | return $this->items; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return IdpSsoDescriptor[] |
||
| 246 | */ |
||
| 247 | public function getAllIdpSsoDescriptors() |
||
| 248 | { |
||
| 249 | $result = []; |
||
| 250 | foreach ($this->getAllItems() as $item) { |
||
| 251 | if ($item instanceof IdpSsoDescriptor) { |
||
| 252 | $result[] = $item; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | return $result; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return SpSsoDescriptor[] |
||
| 261 | */ |
||
| 262 | public function getAllSpSsoDescriptors() |
||
| 263 | { |
||
| 264 | $result = []; |
||
| 265 | foreach ($this->getAllItems() as $item) { |
||
| 266 | if ($item instanceof SpSsoDescriptor) { |
||
| 267 | $result[] = $item; |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | return $result; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return IdpSsoDescriptor|null |
||
| 276 | */ |
||
| 277 | public function getFirstIdpSsoDescriptor() |
||
| 278 | { |
||
| 279 | foreach ($this->getAllItems() as $item) { |
||
| 280 | if ($item instanceof IdpSsoDescriptor) { |
||
| 281 | return $item; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | return null; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @return SpSsoDescriptor|null |
||
| 290 | */ |
||
| 291 | public function getFirstSpSsoDescriptor() |
||
| 292 | { |
||
| 293 | foreach ($this->getAllItems() as $item) { |
||
| 294 | if ($item instanceof SpSsoDescriptor) { |
||
| 295 | return $item; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | return null; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param Signature|null $signature |
||
| 304 | * |
||
| 305 | * @return EntityDescriptor |
||
| 306 | */ |
||
| 307 | public function setSignature(Signature $signature) |
||
| 308 | { |
||
| 309 | $this->signature = $signature; |
||
| 310 | |||
| 311 | return $this; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return Signature|null |
||
| 316 | */ |
||
| 317 | public function getSignature() |
||
| 318 | { |
||
| 319 | return $this->signature; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param int $validUntil |
||
| 324 | * |
||
| 325 | * @return EntityDescriptor |
||
| 326 | */ |
||
| 327 | public function setValidUntil($validUntil) |
||
| 328 | { |
||
| 329 | $this->validUntil = Helper::getTimestampFromValue($validUntil); |
||
| 330 | |||
| 331 | return $this; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return int|null |
||
| 336 | */ |
||
| 337 | public function getValidUntilTimestamp() |
||
| 338 | { |
||
| 339 | return $this->validUntil; |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return string|null |
||
| 344 | */ |
||
| 345 | public function getValidUntilString() |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return \DateTime|null |
||
| 356 | */ |
||
| 357 | public function getValidUntilDateTime() |
||
| 358 | { |
||
| 359 | if ($this->validUntil) { |
||
| 360 | return new \DateTime('@'.$this->validUntil); |
||
| 361 | } |
||
| 362 | |||
| 363 | return null; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @return array|KeyDescriptor[] |
||
| 368 | */ |
||
| 369 | public function getAllIdpKeyDescriptors() |
||
| 370 | { |
||
| 371 | $result = []; |
||
| 372 | foreach ($this->getAllIdpSsoDescriptors() as $idp) { |
||
| 373 | foreach ($idp->getAllKeyDescriptors() as $key) { |
||
| 374 | $result[] = $key; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | return $result; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @return array|KeyDescriptor[] |
||
| 383 | */ |
||
| 384 | public function getAllSpKeyDescriptors() |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @return EndpointReference[] |
||
| 398 | */ |
||
| 399 | public function getAllEndpoints() |
||
| 400 | { |
||
| 401 | $result = []; |
||
| 402 | foreach ($this->getAllIdpSsoDescriptors() as $idpSsoDescriptor) { |
||
| 403 | foreach ($idpSsoDescriptor->getAllSingleSignOnServices() as $sso) { |
||
| 404 | $result[] = new EndpointReference($this, $idpSsoDescriptor, $sso); |
||
| 405 | } |
||
| 406 | foreach ($idpSsoDescriptor->getAllSingleLogoutServices() as $slo) { |
||
| 407 | $result[] = new EndpointReference($this, $idpSsoDescriptor, $slo); |
||
| 408 | } |
||
| 409 | } |
||
| 410 | foreach ($this->getAllSpSsoDescriptors() as $spSsoDescriptor) { |
||
| 411 | foreach ($spSsoDescriptor->getAllAssertionConsumerServices() as $acs) { |
||
| 412 | $result[] = new EndpointReference($this, $spSsoDescriptor, $acs); |
||
| 413 | } |
||
| 414 | foreach ($spSsoDescriptor->getAllSingleLogoutServices() as $slo) { |
||
| 415 | $result[] = new EndpointReference($this, $spSsoDescriptor, $slo); |
||
| 416 | } |
||
| 417 | } |
||
| 418 | |||
| 419 | return $result; |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @return void |
||
| 424 | */ |
||
| 425 | public function serialize(\DOMNode $parent, SerializationContext $context) |
||
| 440 | } |
||
| 441 | |||
| 442 | public function deserialize(\DOMNode $node, DeserializationContext $context) |
||
| 443 | { |
||
| 444 | $this->checkXmlNodeName($node, 'EntityDescriptor', SamlConstants::NS_METADATA); |
||
| 445 | |||
| 446 | $this->attributesFromXml($node, ['entityID', 'validUntil', 'cacheDuration', 'ID']); |
||
| 488 | ]); |
||
| 489 | } |
||
| 490 | } |
||
| 491 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.