| Conditions | 10 |
| Paths | 52 |
| Total Lines | 88 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 139 | public function edit(Request $request): Template |
||
| 140 | { |
||
| 141 | $authsource = $this->moduleConfig->getOptionalValue('auth', 'login-admin'); |
||
| 142 | $useridattr = $this->moduleConfig->getOptionalValue('useridattr', 'eduPersonPrincipalName'); |
||
| 143 | |||
| 144 | $as = new $this->authSimple($authsource); |
||
| 145 | $as->requireAuth(); |
||
| 146 | |||
| 147 | $attributes = $as->getAttributes(); |
||
| 148 | // Check if userid exists |
||
| 149 | if (!isset($attributes[$useridattr])) { |
||
| 150 | throw new Error\Exception('User ID is missing'); |
||
| 151 | } |
||
| 152 | $userid = $attributes[$useridattr][0]; |
||
| 153 | |||
| 154 | $entityId = $request->get('entityid'); |
||
| 155 | $xmlMetadata = $request->get('xmlmetadata'); |
||
| 156 | |||
| 157 | $mdh = new Metadata\MetaDataStorageHandlerSerialize( |
||
| 158 | $this->moduleConfig->getOptionalArray('metahandlerConfig', []), |
||
| 159 | ); |
||
| 160 | |||
| 161 | if ($entityId !== null) { |
||
| 162 | $metadata = $mdh->getMetadata($entityId, 'saml20-sp-remote'); |
||
| 163 | $this->requireOwnership($metadata, $userid); |
||
| 164 | } elseif ($xmlMetadata !== null) { |
||
| 165 | $xmlUtils = new Utils\XML(); |
||
| 166 | $xmlUtils->checkSAMLMessage($xmlMetadata, 'saml-meta'); |
||
| 167 | $entities = Metadata\SAMLParser::parseDescriptorsString($xmlMetadata); |
||
| 168 | $entity = array_pop($entities); |
||
| 169 | $metadata = $entity->getMetadata20SP(); |
||
| 170 | |||
| 171 | /* Trim metadata endpoint arrays. */ |
||
| 172 | $metadata['AssertionConsumerService'] = [ |
||
| 173 | Utils\Config\Metadata::getDefaultEndpoint( |
||
| 174 | $metadata['AssertionConsumerService'], |
||
| 175 | [C::BINDING_HTTP_POST], |
||
| 176 | ), |
||
| 177 | ]; |
||
| 178 | $metadata['SingleLogoutService'] = [ |
||
| 179 | Utils\Config\Metadata::getDefaultEndpoint( |
||
| 180 | $metadata['SingleLogoutService'], |
||
| 181 | [C::BINDING_HTTP_REDIRECT], |
||
| 182 | ), |
||
| 183 | ]; |
||
| 184 | } else { |
||
| 185 | $metadata = [ |
||
| 186 | 'owner' => $userid, |
||
| 187 | ]; |
||
| 188 | } |
||
| 189 | |||
| 190 | $editor = new Editor(); |
||
| 191 | |||
| 192 | if ($request->get('submit')) { |
||
| 193 | $editor->checkForm($request->request->all()); |
||
| 194 | $metadata = $editor->formToMeta($request->request->all(), [], ['owner' => $userid]); |
||
| 195 | $wasEntityId = $request->get('was-entityid'); |
||
| 196 | if (($wasEntityId !== null) && ($wasEntityId !== $metadata['entityid'])) { |
||
| 197 | $premetadata = $mdh->getMetadata($wasEntityId, 'saml20-sp-remote'); |
||
| 198 | $this->requireOwnership($premetadata, $userid); |
||
| 199 | $mdh->deleteMetadata($wasEntityId, 'saml20-sp-remote'); |
||
| 200 | } |
||
| 201 | |||
| 202 | try { |
||
| 203 | $testmetadata = $mdh->getMetadata($metadata['entityid'], 'saml20-sp-remote'); |
||
| 204 | } catch (Exception $e) { |
||
| 205 | // catch |
||
| 206 | $testmetadata = null; |
||
| 207 | } |
||
| 208 | |||
| 209 | if ($testmetadata) { |
||
| 210 | $this->requireOwnership($testmetadata, $userid); |
||
| 211 | } |
||
| 212 | |||
| 213 | $result = $mdh->saveMetadata($metadata['entityid'], 'saml20-sp-remote', $metadata); |
||
| 214 | if ($result === false) { |
||
| 215 | throw new Error\Exception("Could not save metadata. See log for details"); |
||
| 216 | } |
||
| 217 | |||
| 218 | return new Template($this->config, 'metaedit:saved.twig'); |
||
| 219 | } |
||
| 220 | |||
| 221 | $form = $editor->metaToForm($metadata); |
||
| 222 | |||
| 223 | $t = new Template($this->config, 'metaedit:formedit.twig'); |
||
| 224 | $t->data['form'] = $form; |
||
| 225 | |||
| 226 | return $t; |
||
| 227 | } |
||
| 260 |