RecaptchaV3   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A sendVerifyRequest() 0 7 1
A __construct() 0 9 1
A verifyResponse() 0 21 6
A render() 0 22 2
1
<?php
2
3
namespace rias\contactformextensions\models;
4
5
use GuzzleHttp\Client;
6
7
class RecaptchaV3
8
{
9
    const API_URI = 'https://www.google.com/recaptcha/api.js';
10
    const VERIFY_URI = 'https://www.google.com/recaptcha/api/siteverify';
11
12
    /**
13
     * @var \GuzzleHttp\Client
14
     */
15
    protected $client;
16
17
    private $siteKey;
18
    private $secretKey;
19
    private $threshold;
20
    private $hideBadge;
21
22
    public function __construct(string $siteKey, string $secretKey, float $threshold, int $timeout = 5, bool $hideBadge = false)
23
    {
24
        $this->siteKey = $siteKey;
25
        $this->secretKey = $secretKey;
26
        $this->client = new Client([
27
            'timeout' => $timeout,
28
        ]);
29
        $this->threshold = $threshold;
30
        $this->hideBadge = $hideBadge;
31
    }
32
33
    public function render($action = 'homepage')
34
    {
35
        $siteKey = $this->siteKey;
36
        $api_uri = static::API_URI;
37
38
        $html = <<<HTML
39
                <script src="${api_uri}?render=${siteKey}"></script>
40
                <script>
41
                  grecaptcha.ready(function() {
42
                      grecaptcha.execute('${siteKey}', {action: '${action}'}).then(function(token) {
43
                         document.getElementById('g-recaptcha-response').value = token;
44
                      });
45
                  });
46
                </script>
47
                <input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" value="">
48
HTML;
49
50
        if ($this->hideBadge) {
51
            $html .= '<style>.grecaptcha-badge{display:none;!important}</style>'.PHP_EOL;
52
        }
53
54
        return $html;
55
    }
56
57
    public function verifyResponse($response, $clientIp)
58
    {
59
        if (empty($response)) {
60
            return false;
61
        }
62
63
        $response = $this->sendVerifyRequest([
64
            'secret'   => $this->secretKey,
65
            'remoteip' => $clientIp,
66
            'response' => $response,
67
        ]);
68
69
        if (!isset($response['success']) || $response['success'] !== true) {
70
            return false;
71
        }
72
73
        if (isset($response['score']) && $response['score'] >= $this->threshold) {
74
            return true;
75
        }
76
77
        return false;
78
    }
79
80
    protected function sendVerifyRequest(array $query = [])
81
    {
82
        $response = $this->client->post(static::VERIFY_URI, [
83
            'form_params' => $query,
84
        ]);
85
86
        return json_decode($response->getBody(), true);
87
    }
88
}
89