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 |
||
128 | public function htmlScriptTagJsApi(?string $formId = '', ?array $configuration = []): string { |
||
129 | |||
130 | if ($this->skip_by_ip) { |
||
131 | return ''; |
||
132 | } |
||
133 | |||
134 | switch ($this->version) { |
||
135 | case 'v3': |
||
136 | $html = "<script src=\"https://www.google.com/recaptcha/api.js?render={$this->api_site_key}\"></script>"; |
||
137 | break; |
||
138 | default: |
||
139 | $html = "<script src=\"https://www.google.com/recaptcha/api.js\" async defer></script>"; |
||
140 | } |
||
141 | |||
142 | if ($this->version == 'invisible') { |
||
143 | if (!$formId) { |
||
144 | throw new Exception("formId required", 1); |
||
145 | } |
||
146 | $html .= '<script> |
||
147 | function biscolabLaravelReCaptcha(token) { |
||
148 | document.getElementById("' . $formId . '").submit(); |
||
149 | } |
||
150 | </script>'; |
||
151 | } |
||
152 | elseif ($this->version == 'v3') { |
||
153 | |||
154 | $action = array_get($configuration, 'action', 'homepage'); |
||
155 | |||
156 | $js_custom_validation = array_get($configuration, 'custom_validation', ''); |
||
157 | |||
158 | // Check if set custom_validation. That function will override default fetch validation function |
||
159 | if($js_custom_validation) { |
||
160 | |||
161 | $validate_function = ($js_custom_validation) ? "{$js_custom_validation}(token);" : ''; |
||
162 | } |
||
163 | else { |
||
164 | |||
165 | $js_then_callback = array_get($configuration, 'callback_then', ''); |
||
166 | $js_callback_catch = array_get($configuration, 'callback_catch', ''); |
||
167 | |||
168 | $js_then_callback = ($js_then_callback) ? "{$js_then_callback}(response)" : ''; |
||
169 | $js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : ''; |
||
170 | |||
171 | $validate_function = " |
||
172 | fetch('/" . config('recaptcha.default_validation_route', 'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name', 'token') . "=' + token, { |
||
173 | headers: { |
||
174 | \"X-Requested-With\": \"XMLHttpRequest\", |
||
175 | \"X-CSRF-TOKEN\": csrfToken.content |
||
176 | } |
||
177 | }) |
||
178 | .then(function(response) { |
||
179 | {$js_then_callback} |
||
180 | }) |
||
181 | .catch(function(err) { |
||
182 | {$js_callback_catch} |
||
183 | });"; |
||
184 | } |
||
185 | |||
186 | $html .= "<script> |
||
187 | var csrfToken = document.head.querySelector('meta[name=\"csrf-token\"]'); |
||
188 | grecaptcha.ready(function() { |
||
189 | grecaptcha.execute('{$this->api_site_key}', {action: '{$action}'}).then(function(token) { |
||
190 | {$validate_function} |
||
191 | }); |
||
192 | }); |
||
193 | </script>"; |
||
194 | } |
||
195 | |||
196 | return $html; |
||
197 | } |
||
251 | } |