Completed
Pull Request — master (#79)
by ARCANEDEV
04:20
created

NoCaptchaV3::script()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 2
1
<?php namespace Arcanedev\NoCaptcha;
2
3
use Arcanedev\NoCaptcha\Utilities\ResponseV3;
4
use Illuminate\Support\Arr;
5
6
/**
7
 * Class     NoCaptchaV3
8
 *
9
 * @package  Arcanedev\NoCaptcha
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class NoCaptchaV3 extends AbstractNoCaptcha
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * Decides if we've already loaded the script file or not.
21
     *
22
     * @param bool
23
     */
24
    protected $scriptLoaded = false;
25
26
    /* -----------------------------------------------------------------
27
     |  Main Methods
28
     | -----------------------------------------------------------------
29
     */
30
31
    /**
32
     * @param  string  $name
33
     *
34
     * @return \Illuminate\Support\HtmlString
35
     */
36 12
    public function input($name = 'g-recaptcha-response')
37
    {
38 12
        return $this->toHtmlString(
39 12
            '<input type="hidden" id="'.$name.'" name="'.$name.'">'
40
        );
41
    }
42
43
    /**
44
     * Get script tag.
45
     *
46
     * @param  string|null  $callbackName
47
     *
48
     * @return \Illuminate\Support\HtmlString
49
     */
50 16
    public function script($callbackName = null)
51
    {
52 16
        $script = '';
53
54 16
        if ( ! $this->scriptLoaded) {
55 16
            $script = '<script src="'.$this->getScriptSrc($callbackName).'"></script>';
56 16
            $this->scriptLoaded = true;
57
        }
58
59 16
        return $this->toHtmlString($script);
60
    }
61
62
    /**
63
     * Get the NoCaptcha API Script.
64
     *
65
     * @return \Illuminate\Support\HtmlString
66
     */
67
    public function getApiScript()
68
    {
69
        return $this->toHtmlString(
70
            "<script>
71
                window.noCaptcha = {
72
                    render: function(action, callback) {
73
                        grecaptcha.execute('".$this->getSiteKey()."', {action})
74
                              .then(callback);
75
                    }
76
                }
77
            </script>"
78
        );
79
    }
80
81
    /* -----------------------------------------------------------------
82
     |  Check Methods
83
     | -----------------------------------------------------------------
84
     */
85
86
    /**
87
     * Check if callback is not empty.
88
     *
89
     * @param  string|null  $callbackName
90
     *
91
     * @return bool
92
     */
93 16
    private function hasCallbackName($callbackName)
94
    {
95 16
        return ! (is_null($callbackName) || trim($callbackName) === '');
96
    }
97
98
99
100
    /* -----------------------------------------------------------------
101
     |  Other Methods
102
     | -----------------------------------------------------------------
103
     */
104
105
    /**
106
     * Parse the response.
107
     *
108
     * @param  string  $json
109
     *
110
     * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|mixed
111
     */
112 20
    protected function parseResponse($json)
113
    {
114 20
        return ResponseV3::fromJson($json);
115
    }
116
117
    /**
118
     * Get script source link.
119
     *
120
     * @param  string|null  $callbackName
121
     *
122
     * @return string
123
     */
124 16
    private function getScriptSrc($callbackName = null)
125
    {
126 16
        $queries = [];
127
128 16
        if ($this->hasLang())
129 8
            Arr::set($queries, 'hl', $this->lang);
130
131 16
        Arr::set($queries, 'render', $this->getSiteKey());
132
133 16
        if ($this->hasCallbackName($callbackName))
134
            Arr::set($queries, 'onload', $callbackName);
135
136 16
        return static::CLIENT_URL . (count($queries) ? '?' . http_build_query($queries) : '');
137
    }
138
}
139