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