| Conditions | 31 |
| Paths | 7814 |
| Total Lines | 172 |
| Code Lines | 111 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| 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 |
||
| 92 | public function getconsent(Request $request) |
||
| 93 | { |
||
| 94 | // session_cache_limiter('nocache'); |
||
| 95 | |||
| 96 | $this->logger::info('Consent - getconsent: Accessing consent interface'); |
||
| 97 | |||
| 98 | $stateId = $request->query->get('StateId'); |
||
| 99 | if ($stateId === null) { |
||
| 100 | throw new Error\BadRequest('Missing required StateId query parameter.'); |
||
| 101 | } |
||
| 102 | |||
| 103 | $state = $this->authState::loadState($stateId, 'consent:request'); |
||
| 104 | |||
| 105 | if (is_null($state)) { |
||
| 106 | throw new Error\NoState(); |
||
| 107 | } elseif (array_key_exists('core:SP', $state)) { |
||
| 108 | $spentityid = $state['core:SP']; |
||
| 109 | } elseif (array_key_exists('saml:sp:State', $state)) { |
||
| 110 | $spentityid = $state['saml:sp:State']['core:SP']; |
||
| 111 | } else { |
||
| 112 | $spentityid = 'UNKNOWN'; |
||
| 113 | } |
||
| 114 | |||
| 115 | // The user has pressed the yes-button |
||
| 116 | if ($request->query->get('yes') !== null) { |
||
| 117 | if ($request->query->get('saveconsent') !== null) { |
||
| 118 | $this->logger::stats('consentResponse remember'); |
||
| 119 | } else { |
||
| 120 | $this->logger::stats('consentResponse rememberNot'); |
||
| 121 | } |
||
| 122 | |||
| 123 | $statsInfo = [ |
||
| 124 | 'remember' => $request->query->get('saveconsent'), |
||
| 125 | ]; |
||
| 126 | if (isset($state['Destination']['entityid'])) { |
||
| 127 | $statsInfo['spEntityID'] = $state['Destination']['entityid']; |
||
| 128 | } |
||
| 129 | Stats::log('consent:accept', $statsInfo); |
||
| 130 | |||
| 131 | if ( |
||
| 132 | array_key_exists('consent:store', $state) |
||
| 133 | && $request->query->get('saveconsent') === '1' |
||
| 134 | ) { |
||
| 135 | // Save consent |
||
| 136 | $store = $state['consent:store']; |
||
| 137 | $userId = $state['consent:store.userId']; |
||
| 138 | $targetedId = $state['consent:store.destination']; |
||
| 139 | $attributeSet = $state['consent:store.attributeSet']; |
||
| 140 | |||
| 141 | $this->logger::debug( |
||
| 142 | 'Consent - saveConsent() : [' . $userId . '|' . $targetedId . '|' . $attributeSet . ']' |
||
| 143 | ); |
||
| 144 | try { |
||
| 145 | $store->saveConsent($userId, $targetedId, $attributeSet); |
||
| 146 | } catch (Exception $e) { |
||
| 147 | $this->logger::error('Consent: Error writing to storage: ' . $e->getMessage()); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | return new RunnableResponse([Auth\ProcessingChain::class, 'resumeProcessing'], [$state]); |
||
| 152 | } |
||
| 153 | |||
| 154 | // Prepare attributes for presentation |
||
| 155 | $attributes = $state['Attributes']; |
||
| 156 | $noconsentattributes = $state['consent:noconsentattributes']; |
||
| 157 | |||
| 158 | // Remove attributes that do not require consent |
||
| 159 | foreach ($attributes as $attrkey => $attrval) { |
||
| 160 | if (in_array($attrkey, $noconsentattributes, true)) { |
||
| 161 | unset($attributes[$attrkey]); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | $para = [ |
||
| 165 | 'attributes' => &$attributes |
||
| 166 | ]; |
||
| 167 | |||
| 168 | // Reorder attributes according to attributepresentation hooks |
||
| 169 | Module::callHooks('attributepresentation', $para); |
||
| 170 | |||
| 171 | // Parse parameters |
||
| 172 | if (array_key_exists('name', $state['Source'])) { |
||
| 173 | $srcName = $state['Source']['name']; |
||
| 174 | } elseif (array_key_exists('OrganizationDisplayName', $state['Source'])) { |
||
| 175 | $srcName = $state['Source']['OrganizationDisplayName']; |
||
| 176 | } else { |
||
| 177 | $srcName = $state['Source']['entityid']; |
||
| 178 | } |
||
| 179 | |||
| 180 | if (array_key_exists('name', $state['Destination'])) { |
||
| 181 | $dstName = $state['Destination']['name']; |
||
| 182 | } elseif (array_key_exists('OrganizationDisplayName', $state['Destination'])) { |
||
| 183 | $dstName = $state['Destination']['OrganizationDisplayName']; |
||
| 184 | } else { |
||
| 185 | $dstName = $state['Destination']['entityid']; |
||
| 186 | } |
||
| 187 | |||
| 188 | // Make, populate and layout consent form |
||
| 189 | $t = new Template($this->config, 'consent:consentform.twig'); |
||
| 190 | $translator = $t->getTranslator(); |
||
| 191 | $t->data['srcMetadata'] = $state['Source']; |
||
| 192 | $t->data['dstMetadata'] = $state['Destination']; |
||
| 193 | $t->data['yesTarget'] = Module::getModuleURL('consent/getconsent'); |
||
| 194 | $t->data['yesData'] = ['StateId' => $stateId]; |
||
| 195 | $t->data['noTarget'] = Module::getModuleURL('consent/noconsent'); |
||
| 196 | $t->data['noData'] = ['StateId' => $stateId]; |
||
| 197 | $t->data['attributes'] = $attributes; |
||
| 198 | $t->data['checked'] = $state['consent:checked']; |
||
| 199 | $t->data['stateId'] = $stateId; |
||
| 200 | |||
| 201 | $t->data['srcName'] = htmlspecialchars( |
||
| 202 | is_array($srcName) ? $translator->getPreferredTranslation($srcName) : $srcName |
||
| 203 | ); |
||
| 204 | $t->data['dstName'] = htmlspecialchars( |
||
| 205 | is_array($dstName) ? $translator->getPreferredTranslation($dstName) : $dstName |
||
| 206 | ); |
||
| 207 | |||
| 208 | if (array_key_exists('descr_purpose', $state['Destination'])) { |
||
| 209 | $t->data['dstDesc'] = $translator->getPreferredTranslation( |
||
| 210 | Utils\Arrays::arrayize( |
||
| 211 | $state['Destination']['descr_purpose'], |
||
| 212 | 'en' |
||
| 213 | ) |
||
| 214 | ); |
||
| 215 | } |
||
| 216 | |||
| 217 | // Fetch privacypolicy |
||
| 218 | if ( |
||
| 219 | array_key_exists('UIInfo', $state['Destination']) && |
||
| 220 | array_key_exists('PrivacyStatementURL', $state['Destination']['UIInfo']) && |
||
| 221 | (!empty($state['Destination']['UIInfo']['PrivacyStatementURL'])) |
||
| 222 | ) { |
||
| 223 | $privacypolicy = reset($state['Destination']['UIInfo']['PrivacyStatementURL']); |
||
| 224 | } elseif ( |
||
| 225 | array_key_exists('UIInfo', $state['Source']) && |
||
| 226 | array_key_exists('PrivacyStatementURL', $state['Source']['UIInfo']) && |
||
| 227 | (!empty($state['Source']['UIInfo']['PrivacyStatementURL'])) |
||
| 228 | ) { |
||
| 229 | $privacypolicy = reset($state['Source']['UIInfo']['PrivacyStatementURL']); |
||
| 230 | } else { |
||
| 231 | $privacypolicy = false; |
||
| 232 | } |
||
| 233 | if ($privacypolicy !== false) { |
||
| 234 | $privacypolicy = str_replace( |
||
| 235 | '%SPENTITYID%', |
||
| 236 | urlencode($spentityid), |
||
| 237 | $privacypolicy |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | $t->data['sppp'] = $privacypolicy; |
||
| 241 | |||
| 242 | // Set focus element |
||
| 243 | switch ($state['consent:focus']) { |
||
| 244 | case 'yes': |
||
| 245 | $t->data['autofocus'] = 'yesbutton'; |
||
| 246 | break; |
||
| 247 | case 'no': |
||
| 248 | $t->data['autofocus'] = 'nobutton'; |
||
| 249 | break; |
||
| 250 | case null: |
||
| 251 | default: |
||
| 252 | break; |
||
| 253 | } |
||
| 254 | |||
| 255 | $t->data['usestorage'] = array_key_exists('consent:store', $state); |
||
| 256 | |||
| 257 | if (array_key_exists('consent:hiddenAttributes', $state)) { |
||
| 258 | $t->data['hiddenAttributes'] = $state['consent:hiddenAttributes']; |
||
| 259 | } else { |
||
| 260 | $t->data['hiddenAttributes'] = []; |
||
| 261 | } |
||
| 262 | |||
| 263 | return $t; |
||
| 264 | } |
||
| 359 |