Passed
Push — master ( b01395...c75806 )
by Roberto
05:23 queued 13s
created

ReCaptchaBuilderV3   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 75
rs 10
c 3
b 0
f 0
ccs 22
cts 23
cp 0.9565
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B htmlScriptTagJsApi() 0 52 6
1
<?php
2
/**
3
 * Copyright (c) 2017 - present
4
 * LaravelGoogleRecaptcha - ReCaptchaBuilderV3.php
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 22/1/2019
8
 * MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\ReCaptcha;
12
13
use Illuminate\Support\Arr;
14
15
/**
16
 * Class ReCaptchaBuilderV3
17
 * @package Biscolab\ReCaptcha
18
 */
19
class ReCaptchaBuilderV3 extends ReCaptchaBuilder
20
{
21
22
    /**
23
     * ReCaptchaBuilderV3 constructor.
24
     *
25
     * @param string $api_site_key
26
     * @param string $api_secret_key
27
     */
28 19
    public function __construct(string $api_site_key, string $api_secret_key)
29
    {
30
31 19
        parent::__construct($api_site_key, $api_secret_key, 'v3');
32 19
    }
33
34
    /**
35
     * Write script HTML tag in you HTML code
36
     * Insert before </head> tag
37
     *
38
     * @param array|null $configuration
39
     *
40
     * @return string
41
     */
42 4
    public function htmlScriptTagJsApi(?array $configuration = []): string
43
    {
44
45 4
        if ($this->skip_by_ip) {
46
            return '';
47
        }
48
49 4
        $html = "<script src=\"https://www.google.com/recaptcha/api.js?render={$this->api_site_key}\"></script>";
50
51 4
        $action = Arr::get($configuration, 'action', 'homepage');
52
53 4
        $js_custom_validation = Arr::get($configuration, 'custom_validation', '');
54
55
        // Check if set custom_validation. That function will override default fetch validation function
56 4
        if ($js_custom_validation) {
57
58 1
            $validate_function = ($js_custom_validation) ? "{$js_custom_validation}(token);" : '';
59
        } else {
60
61 3
            $js_then_callback = Arr::get($configuration, 'callback_then', '');
62 3
            $js_callback_catch = Arr::get($configuration, 'callback_catch', '');
63
64 3
            $js_then_callback = ($js_then_callback) ? "{$js_then_callback}(response)" : '';
65 3
            $js_callback_catch = ($js_callback_catch) ? "{$js_callback_catch}(err)" : '';
66
67
            $validate_function = "
68 3
                fetch('/" . config('recaptcha.default_validation_route',
69 3
                    'biscolab-recaptcha/validate') . "?" . config('recaptcha.default_token_parameter_name',
70 3
                    'token') . "=' + token, {
71
                    headers: {
72
                        \"X-Requested-With\": \"XMLHttpRequest\",
73
                        \"X-CSRF-TOKEN\": csrfToken.content
74
                    }
75
                })
76
                .then(function(response) {
77 3
                   	{$js_then_callback}
78
                })
79
                .catch(function(err) {
80 3
                    {$js_callback_catch}
81
                });";
82
        }
83
84
        $html .= "<script>
85
                    var csrfToken = document.head.querySelector('meta[name=\"csrf-token\"]');
86
                  grecaptcha.ready(function() {
87 4
                      grecaptcha.execute('{$this->api_site_key}', {action: '{$action}'}).then(function(token) {
88 4
                        {$validate_function}
89
                      });
90
                  });
91
		     </script>";
92
93 4
        return $html;
94
    }
95
96
}