| Conditions | 10 |
| Paths | 19 |
| Total Lines | 69 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 146 | public function htmlScriptTagJsApi(?string $formId = '', ?array $configuration = []): string { |
||
| 147 | |||
| 148 | if ($this->skip_by_ip) { |
||
| 149 | return ''; |
||
| 150 | } |
||
| 151 | |||
| 152 | switch ($this->version) { |
||
| 153 | case 'v3': |
||
| 154 | $html = "<script src=\"https://www.google.com/recaptcha/api.js?render={$this->api_site_key}\"></script>"; |
||
| 155 | break; |
||
| 156 | default: |
||
| 157 | $html = "<script src=\"https://www.google.com/recaptcha/api.js\" async defer></script>"; |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($this->version == 'invisible') { |
||
| 161 | if (!$formId) { |
||
| 162 | throw new Exception("formId required", 1); |
||
| 163 | } |
||
| 164 | $html .= '<script> |
||
| 165 | function biscolabLaravelReCaptcha(token) { |
||
| 166 | document.getElementById("' . $formId . '").submit(); |
||
| 167 | } |
||
| 168 | </script>'; |
||
| 169 | } |
||
| 170 | elseif ($this->version == 'v3') { |
||
| 171 | |||
| 172 | $action = array_get($configuration, 'action', 'homepage'); |
||
| 173 | |||
| 174 | $js_custom_validation = array_get($configuration, 'custom_validation', ''); |
||
| 175 | |||
| 176 | // Check if set custom_validation. That function will override default fetch validation function |
||
| 177 | if ($js_custom_validation) { |
||
| 178 | |||
| 179 | $validate_function = ($js_custom_validation) ? "{$js_custom_validation}(token);" : ''; |
||
| 180 | } |
||
| 181 | else { |
||
| 182 | |||
| 183 | $js_then_callback = array_get($configuration, 'callback_then', ''); |
||
| 184 | $js_callback_catch = array_get($configuration, 'callback_catch', ''); |
||
| 185 | |||
| 186 | $js_then_callback = ($js_then_callback) ? "{$js_then_callback}(response)" : ''; |
||
| 187 | $js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : ''; |
||
| 188 | |||
| 189 | $validate_function = " |
||
| 190 | fetch('/" . config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name', 'token') . "=' + token, { |
||
| 191 | headers: { |
||
| 192 | \"X-Requested-With\": \"XMLHttpRequest\", |
||
| 193 | \"X-CSRF-TOKEN\": csrfToken.content |
||
| 194 | } |
||
| 195 | }) |
||
| 196 | .then(function(response) { |
||
| 197 | {$js_then_callback} |
||
| 198 | }) |
||
| 199 | .catch(function(err) { |
||
| 200 | {$js_callback_catch} |
||
| 201 | });"; |
||
| 202 | } |
||
| 203 | |||
| 204 | $html .= "<script> |
||
| 205 | var csrfToken = document.head.querySelector('meta[name=\"csrf-token\"]'); |
||
| 206 | grecaptcha.ready(function() { |
||
| 207 | grecaptcha.execute('{$this->api_site_key}', {action: '{$action}'}).then(function(token) { |
||
| 208 | {$validate_function} |
||
| 209 | }); |
||
| 210 | }); |
||
| 211 | </script>"; |
||
| 212 | } |
||
| 213 | |||
| 214 | return $html; |
||
| 215 | } |
||
| 298 | } |