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