Completed
Pull Request — master (#79)
by ARCANEDEV
07:25
created

NoCaptchaV3::getApiScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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