| Total Complexity | 42 |
| Total Lines | 158 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AssertionValidator 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 AssertionValidator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class AssertionValidator implements AssertionValidatorInterface |
||
| 29 | { |
||
| 30 | /** @var NameIdValidatorInterface */ |
||
| 31 | protected $nameIdValidator; |
||
| 32 | |||
| 33 | /** @var SubjectValidatorInterface */ |
||
| 34 | protected $subjectValidator; |
||
| 35 | |||
| 36 | /** @var StatementValidatorInterface */ |
||
| 37 | protected $statementValidator; |
||
| 38 | |||
| 39 | public function __construct( |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @return void |
||
| 51 | */ |
||
| 52 | public function validateAssertion(Assertion $assertion) |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @throws LightSamlValidationException |
||
| 62 | */ |
||
| 63 | protected function validateAssertionAttributes(Assertion $assertion) |
||
| 64 | { |
||
| 65 | if (false == Helper::validateRequiredString($assertion->getVersion())) { |
||
|
|
|||
| 66 | throw new LightSamlValidationException('Assertion element must have the Version attribute set.'); |
||
| 67 | } |
||
| 68 | if (SamlConstants::VERSION_20 != $assertion->getVersion()) { |
||
| 69 | throw new LightSamlValidationException('Assertion element must have the Version attribute value equal to 2.0.'); |
||
| 70 | } |
||
| 71 | if (false == Helper::validateRequiredString($assertion->getId())) { |
||
| 72 | throw new LightSamlValidationException('Assertion element must have the ID attribute set.'); |
||
| 73 | } |
||
| 74 | if (false == Helper::validateIdString($assertion->getId())) { |
||
| 75 | throw new LightSamlValidationException('Assertion element must have an ID attribute with at least 16 characters (the equivalent of 128 bits).'); |
||
| 76 | } |
||
| 77 | if (false == $assertion->getIssueInstantTimestamp()) { |
||
| 78 | throw new LightSamlValidationException('Assertion element must have the IssueInstant attribute set.'); |
||
| 79 | } |
||
| 80 | if (false == $assertion->getIssuer()) { |
||
| 81 | throw new LightSamlValidationException('Assertion element must have an issuer element.'); |
||
| 82 | } |
||
| 83 | $this->nameIdValidator->validateNameId($assertion->getIssuer()); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @throws LightSamlValidationException |
||
| 88 | */ |
||
| 89 | protected function validateSubject(Assertion $assertion) |
||
| 90 | { |
||
| 91 | if (false == $assertion->getSubject()) { |
||
| 92 | if (false == $assertion->getAllItems()) { |
||
| 93 | throw new LightSamlValidationException('Assertion with no Statements must have a subject.'); |
||
| 94 | } |
||
| 95 | foreach ($assertion->getAllItems() as $item) { |
||
| 96 | if ($item instanceof AuthnStatement || $item instanceof AttributeStatement) { |
||
| 97 | throw new LightSamlValidationException('AuthnStatement, AuthzDecisionStatement and AttributeStatement require a subject.'); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } else { |
||
| 101 | $this->subjectValidator->validateSubject($assertion->getSubject()); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | protected function validateConditions(Assertion $assertion) |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | protected function validateConditionsInterval(Conditions $conditions) |
||
| 135 | { |
||
| 136 | if ($conditions->getNotBeforeTimestamp() && |
||
| 137 | $conditions->getNotOnOrAfterTimestamp() && |
||
| 138 | $conditions->getNotBeforeTimestamp() > $conditions->getNotOnOrAfterTimestamp() |
||
| 139 | ) { |
||
| 140 | throw new LightSamlValidationException('Conditions NotBefore MUST BE less than NotOnOrAfter'); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @throws LightSamlValidationException |
||
| 146 | */ |
||
| 147 | protected function validateProxyRestriction(ProxyRestriction $item) |
||
| 148 | { |
||
| 149 | if (null === $item->getCount() || '' === $item->getCount() || intval($item->getCount()) != $item->getCount() || $item->getCount() < 0) { |
||
| 150 | throw new LightSamlValidationException('Count attribute of ProxyRestriction MUST BE a non-negative integer'); |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($item->getAllAudience()) { |
||
| 154 | foreach ($item->getAllAudience() as $audience) { |
||
| 155 | if (false == Helper::validateWellFormedUriString($audience)) { |
||
| 156 | throw new LightSamlValidationException('ProxyRestriction Audience MUST BE a wellformed uri'); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @throws LightSamlValidationException |
||
| 164 | */ |
||
| 165 | protected function validateAudienceRestriction(AudienceRestriction $item) |
||
| 166 | { |
||
| 167 | if (false == $item->getAllAudience()) { |
||
| 168 | return; |
||
| 169 | } |
||
| 170 | |||
| 171 | foreach ($item->getAllAudience() as $audience) { |
||
| 172 | if (false == Helper::validateWellFormedUriString($audience)) { |
||
| 173 | throw new LightSamlValidationException('AudienceRestriction MUST BE a wellformed uri'); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | protected function validateStatements(Assertion $assertion) |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.