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