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 | $mdh = new Metadata\MetaDataStorageHandlerSerialize($moduleConfig->getValue('metahandlerConfig', null)); |
||
124 | |||
125 | $authsource = $moduleConfig->getValue('auth', 'login-admin'); |
||
126 | $useridattr = $moduleConfig->getValue('useridattr', 'eduPersonPrincipalName'); |
||
127 | |||
128 | $as = new Auth\Simple($authsource); |
||
129 | $as->requireAuth(); |
||
130 | |||
131 | $attributes = $as->getAttributes(); |
||
132 | // Check if userid exists |
||
133 | if (!isset($attributes[$useridattr])) { |
||
134 | throw new Error\Exception('User ID is missing'); |
||
135 | } |
||
136 | $userid = $attributes[$useridattr][0]; |
||
137 | |||
138 | $entityId = $request->get('entityid'); |
||
139 | $xmlMetadata = $request->get('xmlmetadata'); |
||
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($_REQUEST['was-entityid'], 'saml20-sp-remote'); |
||
178 | $this->requireOwnership($premetadata, $userid); |
||
179 | $mdh->deleteMetadata($_REQUEST['was-entityid'], '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 |