Passed
Branch master (47c941)
by Roberto
05:04
created

ReCaptchaBuilderInvisible   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
c 2
b 0
f 0
dl 0
loc 107
rs 10
ccs 30
cts 33
cp 0.9091
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormId() 0 13 3
A htmlFormButton() 0 30 5
A __construct() 0 4 1
A htmlScriptTagJsApi() 0 18 2
1
<?php
2
/**
3
 * Copyright (c) 2017 - present
4
 * LaravelGoogleRecaptcha - ReCaptchaBuilderInvisible.php
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 12/9/2018
8
 * MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\ReCaptcha;
12
13
use Biscolab\ReCaptcha\Exceptions\InvalidConfigurationException;
14
use Illuminate\Support\Arr;
15
16
/**
17
 * Class ReCaptchaBuilderInvisible
18
 * @package Biscolab\ReCaptcha
19
 */
20
class ReCaptchaBuilderInvisible extends ReCaptchaBuilder
21
{
22
23
    /**
24
     * @var null|string
25
     */
26
    protected $form_id = null;
27
28
    /**
29
     * ReCaptchaBuilderInvisible constructor.
30
     *
31
     * @param string $api_site_key
32
     * @param string $api_secret_key
33
     */
34 22
    public function __construct(string $api_site_key, string $api_secret_key)
35
    {
36
37 22
        parent::__construct($api_site_key, $api_secret_key, 'invisible');
38 22
    }
39
40
    /**
41
     * Write HTML <button> tag in your HTML code
42
     * Insert before </form> tag
43
     *
44
     * @param string     $button_label
45
     * @param array|null $properties
46
     *
47
     * @return string
48
     */
49 3
    public function htmlFormButton($button_label = 'Submit', ?array $properties = []): string
50
    {
51
52 3
        $tag_properties = '';
53
54 3
        $properties = array_merge($properties, [
55 3
            'data-sitekey'  => $this->api_site_key,
56 3
            'data-callback' => 'biscolabLaravelReCaptcha',
57
        ]);
58
59 3
        if (empty($properties['class'])) {
60 2
            $properties['class'] = 'g-recaptcha';
61
        } else {
62 1
            $properties['class'] .= ' g-recaptcha';
63
        }
64
65 3
        ksort($properties);
66
67 3
        if ($properties) {
68
//            $tag_properties = str_replace("=", '="',
69
//                    http_build_query($properties, null, '" ', PHP_QUERY_RFC3986)) . '"';
70 3
            $temp_properties = [];
71 3
            foreach ($properties as $k => $v) {
72 3
                $temp_properties[] = $k . '="' . $v . '"';
73
            }
74
75 3
            $tag_properties = implode(" ", $temp_properties);
76
        }
77
78 3
        return ($this->version == 'invisible') ? '<button ' . $tag_properties . '>' . $button_label . '</button>' : '';
79
    }
80
81
    /**
82
     * Write script HTML tag in you HTML code
83
     * Insert before </head> tag
84
     *
85
     * @param array|null $configuration
86
     *
87
     * @return string
88
     * @throws \Exception
89
     */
90 1
    public function htmlScriptTagJsApi(?array $configuration = []): string
91
    {
92
93 1
        $html = parent::htmlScriptTagJsApi();
94
95 1
        $form_id = Arr::get($configuration, 'form_id');
96 1
        if (!$form_id) {
97 1
            $form_id = $this->getFormId();
98
        } else {
99
            $this->form_id = $form_id;
100
        }
101
        $html .= '<script>
102
		       function biscolabLaravelReCaptcha(token) {
103 1
		         document.getElementById("' . $form_id . '").submit();
104
		       }
105
		     </script>';
106
107 1
        return $html;
108
    }
109
110
    /**
111
     * @return string
112
     * @throws \Exception
113
     */
114 2
    public function getFormId(): string
115
    {
116
117 2
        if (!$this->form_id) {
118 2
            $form_id = config('recaptcha.default_form_id');
119
        } else {
120
            $form_id = $this->form_id;
121
        }
122 2
        if (!$form_id) {
123
            throw new InvalidConfigurationException("formId required");
124
        }
125
126 2
        return $form_id;
127
    }
128
}
129
130