| Conditions | 10 |
| Paths | 52 |
| Total Lines | 89 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 119 | public function edit(Request $request): Template |
||
| 120 | { |
||
| 121 | /* Load configuration and metadata */ |
||
| 122 | $moduleConfig = Configuration::getConfig('module_metaedit.php'); |
||
| 123 | |||
| 124 | $authsource = $moduleConfig->getValue('auth', 'login-admin'); |
||
| 125 | $useridattr = $moduleConfig->getValue('useridattr', 'eduPersonPrincipalName'); |
||
| 126 | |||
| 127 | $as = new Auth\Simple($authsource); |
||
| 128 | $as->requireAuth(); |
||
| 129 | |||
| 130 | $attributes = $as->getAttributes(); |
||
| 131 | // Check if userid exists |
||
| 132 | if (!isset($attributes[$useridattr])) { |
||
| 133 | throw new Error\Exception('User ID is missing'); |
||
| 134 | } |
||
| 135 | $userid = $attributes[$useridattr][0]; |
||
| 136 | |||
| 137 | $entityId = $request->get('entityid'); |
||
| 138 | $xmlMetadata = $request->get('xmlmetadata'); |
||
| 139 | |||
| 140 | $mdh = new Metadata\MetaDataStorageHandlerSerialize($moduleConfig->getArray('metahandlerConfig', [])); |
||
| 141 | |||
| 142 | if ($entityId !== null) { |
||
| 143 | $metadata = $mdh->getMetadata($entityId, 'saml20-sp-remote'); |
||
| 144 | $this->requireOwnership($metadata, $userid); |
||
| 145 | } elseif ($xmlMetadata !== null) { |
||
| 146 | $xmlUtils = new Utils\XML(); |
||
| 147 | $xmlUtils->checkSAMLMessage($xmlMetadata, 'saml-meta'); |
||
| 148 | $entities = Metadata\SAMLParser::parseDescriptorsString($xmlMetadata); |
||
| 149 | $entity = array_pop($entities); |
||
| 150 | $metadata = $entity->getMetadata20SP(); |
||
| 151 | |||
| 152 | /* Trim metadata endpoint arrays. */ |
||
| 153 | $metadata['AssertionConsumerService'] = [ |
||
| 154 | Utils\Config\Metadata::getDefaultEndpoint( |
||
| 155 | $metadata['AssertionConsumerService'], |
||
| 156 | [Constants::BINDING_HTTP_POST] |
||
| 157 | ) |
||
| 158 | ]; |
||
| 159 | $metadata['SingleLogoutService'] = [ |
||
| 160 | Utils\Config\Metadata::getDefaultEndpoint( |
||
| 161 | $metadata['SingleLogoutService'], |
||
| 162 | [Constants::BINDING_HTTP_REDIRECT] |
||
| 163 | ) |
||
| 164 | ]; |
||
| 165 | } else { |
||
| 166 | $metadata = [ |
||
| 167 | 'owner' => $userid, |
||
| 168 | ]; |
||
| 169 | } |
||
| 170 | |||
| 171 | $editor = new Editor(); |
||
| 172 | |||
| 173 | if ($request->get('submit')) { |
||
| 174 | $editor->checkForm($request->request->all()); |
||
| 175 | $metadata = $editor->formToMeta($request->request->all(), [], ['owner' => $userid]); |
||
| 176 | $wasEntityId = $request->get('was-entityid'); |
||
| 177 | if (($wasEntityId !== null) && ($wasEntityId !== $metadata['entityid'])) { |
||
| 178 | $premetadata = $mdh->getMetadata($wasEntityId, 'saml20-sp-remote'); |
||
| 179 | $this->requireOwnership($premetadata, $userid); |
||
| 180 | $mdh->deleteMetadata($wasEntityId, 'saml20-sp-remote'); |
||
| 181 | } |
||
| 182 | |||
| 183 | try { |
||
| 184 | $testmetadata = $mdh->getMetadata($metadata['entityid'], 'saml20-sp-remote'); |
||
| 185 | } catch (Exception $e) { |
||
| 186 | // catch |
||
| 187 | $testmetadata = null; |
||
| 188 | } |
||
| 189 | |||
| 190 | if ($testmetadata) { |
||
| 191 | $this->requireOwnership($testmetadata, $userid); |
||
| 192 | } |
||
| 193 | |||
| 194 | $result = $mdh->saveMetadata($metadata['entityid'], 'saml20-sp-remote', $metadata); |
||
| 195 | if ($result === false) { |
||
| 196 | throw new Error\Exception("Could not save metadata. See log for details"); |
||
| 197 | } |
||
| 198 | |||
| 199 | return new Template($this->config, 'metaedit:saved.twig'); |
||
| 200 | } |
||
| 201 | |||
| 202 | $form = $editor->metaToForm($metadata); |
||
| 203 | |||
| 204 | $t = new Template($this->config, 'metaedit:formedit.twig'); |
||
| 205 | $t->data['form'] = $form; |
||
| 206 | |||
| 207 | return $t; |
||
| 208 | } |
||
| 243 |