| Total Complexity | 55 |
| Total Lines | 437 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Assertion 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 Assertion, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Assertion extends AbstractSamlModel |
||
| 22 | { |
||
| 23 | //region Attributes |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $id; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $version = SamlConstants::VERSION_20; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | protected $issueInstant; |
||
| 39 | |||
| 40 | //endregion |
||
| 41 | |||
| 42 | //region Elements |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Issuer |
||
| 46 | */ |
||
| 47 | protected $issuer; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Signature|null |
||
| 51 | */ |
||
| 52 | protected $signature; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Subject|null |
||
| 56 | */ |
||
| 57 | protected $subject; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Conditions|null |
||
| 61 | */ |
||
| 62 | protected $conditions; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array|AbstractStatement[]|AuthnStatement[]|AttributeStatement[] |
||
| 66 | */ |
||
| 67 | protected $items = []; |
||
| 68 | |||
| 69 | //endregion |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Core 3.3.4 Processing rules. |
||
| 73 | * |
||
| 74 | * @param string $nameId |
||
| 75 | * @param string|null $format |
||
| 76 | * |
||
| 77 | * @return bool |
||
| 78 | */ |
||
| 79 | public function equals($nameId, $format) |
||
| 80 | { |
||
| 81 | if (false == $this->getSubject()) { |
||
|
|
|||
| 82 | return false; |
||
| 83 | } |
||
| 84 | |||
| 85 | if (false == $this->getSubject()->getNameID()) { |
||
| 86 | return false; |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($this->getSubject()->getNameID()->getValue() != $nameId) { |
||
| 90 | return false; |
||
| 91 | } |
||
| 92 | |||
| 93 | if ($this->getSubject()->getNameID()->getFormat() != $format) { |
||
| 94 | return false; |
||
| 95 | } |
||
| 96 | |||
| 97 | return true; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $sessionIndex |
||
| 102 | * |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | public function hasSessionIndex($sessionIndex) |
||
| 106 | { |
||
| 107 | if (null == $this->getAllAuthnStatements()) { |
||
| 108 | return false; |
||
| 109 | } |
||
| 110 | |||
| 111 | foreach ($this->getAllAuthnStatements() as $authnStatement) { |
||
| 112 | if ($authnStatement->getSessionIndex() == $sessionIndex) { |
||
| 113 | return true; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | return false; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function hasAnySessionIndex() |
||
| 121 | { |
||
| 122 | if (false == $this->getAllAuthnStatements()) { |
||
| 123 | return false; |
||
| 124 | } |
||
| 125 | |||
| 126 | foreach ($this->getAllAuthnStatements() as $authnStatement) { |
||
| 127 | if ($authnStatement->getSessionIndex()) { |
||
| 128 | return true; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | return false; |
||
| 133 | } |
||
| 134 | |||
| 135 | //region Getters & Setters |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return Assertion |
||
| 139 | */ |
||
| 140 | public function setConditions(Conditions $conditions = null) |
||
| 141 | { |
||
| 142 | $this->conditions = $conditions; |
||
| 143 | |||
| 144 | return $this; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return \LightSaml\Model\Assertion\Conditions|null |
||
| 149 | */ |
||
| 150 | public function getConditions() |
||
| 151 | { |
||
| 152 | return $this->conditions; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $id |
||
| 157 | * |
||
| 158 | * @return Assertion |
||
| 159 | */ |
||
| 160 | public function setId($id) |
||
| 161 | { |
||
| 162 | $this->id = (string) $id; |
||
| 163 | |||
| 164 | return $this; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function getId() |
||
| 171 | { |
||
| 172 | return $this->id; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string|int|\DateTime $issueInstant |
||
| 177 | * |
||
| 178 | * @throws \InvalidArgumentException |
||
| 179 | * |
||
| 180 | * @return Assertion |
||
| 181 | */ |
||
| 182 | public function setIssueInstant($issueInstant) |
||
| 183 | { |
||
| 184 | $this->issueInstant = Helper::getTimestampFromValue($issueInstant); |
||
| 185 | |||
| 186 | return $this; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return int |
||
| 191 | */ |
||
| 192 | public function getIssueInstantTimestamp() |
||
| 193 | { |
||
| 194 | return $this->issueInstant; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function getIssueInstantString() |
||
| 201 | { |
||
| 202 | if ($this->issueInstant) { |
||
| 203 | return Helper::time2string($this->issueInstant); |
||
| 204 | } |
||
| 205 | |||
| 206 | return null; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function getIssueInstantDateTime() |
||
| 213 | { |
||
| 214 | if ($this->issueInstant) { |
||
| 215 | return new \DateTime('@'.$this->issueInstant); |
||
| 216 | } |
||
| 217 | |||
| 218 | return null; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param Issuer $issuer |
||
| 223 | * |
||
| 224 | * @return Assertion |
||
| 225 | */ |
||
| 226 | public function setIssuer(Issuer $issuer = null) |
||
| 227 | { |
||
| 228 | $this->issuer = $issuer; |
||
| 229 | |||
| 230 | return $this; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return \LightSaml\Model\Assertion\Issuer |
||
| 235 | */ |
||
| 236 | public function getIssuer() |
||
| 237 | { |
||
| 238 | return $this->issuer; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param Signature $signature |
||
| 243 | * |
||
| 244 | * @return Assertion |
||
| 245 | */ |
||
| 246 | public function setSignature(Signature $signature = null) |
||
| 247 | { |
||
| 248 | $this->signature = $signature; |
||
| 249 | |||
| 250 | return $this; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return \LightSaml\Model\XmlDSig\Signature|null |
||
| 255 | */ |
||
| 256 | public function getSignature() |
||
| 257 | { |
||
| 258 | return $this->signature; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return Assertion |
||
| 263 | */ |
||
| 264 | public function setSubject(Subject $subject) |
||
| 265 | { |
||
| 266 | $this->subject = $subject; |
||
| 267 | |||
| 268 | return $this; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @return \LightSaml\Model\Assertion\Subject |
||
| 273 | */ |
||
| 274 | public function getSubject() |
||
| 275 | { |
||
| 276 | return $this->subject; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $version |
||
| 281 | * |
||
| 282 | * @return Assertion |
||
| 283 | */ |
||
| 284 | public function setVersion($version) |
||
| 285 | { |
||
| 286 | $this->version = (string) $version; |
||
| 287 | |||
| 288 | return $this; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function getVersion() |
||
| 295 | { |
||
| 296 | return $this->version; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return Assertion |
||
| 301 | */ |
||
| 302 | public function addItem(AbstractStatement $statement) |
||
| 303 | { |
||
| 304 | $this->items[] = $statement; |
||
| 305 | |||
| 306 | return $this; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return AbstractStatement[]|AttributeStatement[]|AuthnStatement[]|array |
||
| 311 | */ |
||
| 312 | public function getAllItems() |
||
| 313 | { |
||
| 314 | return $this->items; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return \LightSaml\Model\Assertion\AuthnStatement[] |
||
| 319 | */ |
||
| 320 | public function getAllAuthnStatements() |
||
| 321 | { |
||
| 322 | $result = []; |
||
| 323 | foreach ($this->items as $item) { |
||
| 324 | if ($item instanceof AuthnStatement) { |
||
| 325 | $result[] = $item; |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | return $result; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return \LightSaml\Model\Assertion\AttributeStatement[] |
||
| 334 | */ |
||
| 335 | public function getAllAttributeStatements() |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return \LightSaml\Model\Assertion\AttributeStatement|null |
||
| 349 | */ |
||
| 350 | public function getFirstAttributeStatement() |
||
| 351 | { |
||
| 352 | foreach ($this->items as $item) { |
||
| 353 | if ($item instanceof AttributeStatement) { |
||
| 354 | return $item; |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | return null; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @return \LightSaml\Model\Assertion\AuthnStatement|null |
||
| 363 | */ |
||
| 364 | public function getFirstAuthnStatement() |
||
| 365 | { |
||
| 366 | foreach ($this->items as $item) { |
||
| 367 | if ($item instanceof AuthnStatement) { |
||
| 368 | return $item; |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | return null; |
||
| 373 | } |
||
| 374 | |||
| 375 | //endregion |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | public function hasBearerSubject() |
||
| 381 | { |
||
| 382 | if ($this->getAllAuthnStatements() && $this->getSubject()) { |
||
| 383 | if ($this->getSubject()->getBearerConfirmations()) { |
||
| 384 | return true; |
||
| 385 | } |
||
| 386 | } |
||
| 387 | |||
| 388 | return false; |
||
| 389 | } |
||
| 390 | |||
| 391 | protected function prepareForXml() |
||
| 392 | { |
||
| 393 | if (false == $this->getId()) { |
||
| 394 | $this->setId(Helper::generateID()); |
||
| 395 | } |
||
| 396 | if (false == $this->getIssueInstantTimestamp()) { |
||
| 397 | $this->setIssueInstant(time()); |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @return void |
||
| 403 | */ |
||
| 404 | public function serialize(\DOMNode $parent, SerializationContext $context) |
||
| 405 | { |
||
| 406 | $this->prepareForXml(); |
||
| 407 | |||
| 408 | $result = $this->createElement('Assertion', SamlConstants::NS_ASSERTION, $parent, $context); |
||
| 409 | |||
| 410 | $this->attributesToXml(['ID', 'Version', 'IssueInstant'], $result); |
||
| 411 | |||
| 412 | $this->singleElementsToXml( |
||
| 413 | ['Issuer', 'Subject', 'Conditions'], |
||
| 414 | $result, |
||
| 415 | $context |
||
| 416 | ); |
||
| 417 | |||
| 418 | foreach ($this->items as $item) { |
||
| 419 | $item->serialize($result, $context); |
||
| 420 | } |
||
| 421 | |||
| 422 | // must be added at the end |
||
| 423 | $this->singleElementsToXml(['Signature'], $result, $context); |
||
| 424 | } |
||
| 425 | |||
| 426 | public function deserialize(\DOMNode $node, DeserializationContext $context) |
||
| 458 | ]); |
||
| 459 | } |
||
| 460 | } |
||
| 461 |