Completed
Push — master ( daab91...b549ed )
by Derek Stephen
02:54 queued 11s
created

ReCaptchaAdapter::getMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace Del\Form\Field\Captcha;
4
5
use Del\Form\Validator\ValidatorInterface;
6
use Exception;
7
8
class ReCaptchaAdapter implements CaptchaAdapterInterface, ValidatorInterface
9
{
10
    /** @var string $siteKey */
11
    private $siteKey;
12
13
    /** @var string $secretKey */
14
    private $secretKey;
15
16
    /**
17
     * ReCaptchaAdapter constructor.
18
     * @param string $siteKey
19
     * @param string $secretKey
20
     */
21
    public function __construct(string $siteKey, string $secretKey)
22
    {
23
        $this->siteKey = $siteKey;
24
        $this->secretKey = $secretKey;
25
    }
26
27
    public function generate(): string
28
    {
29
30
    }
31
32
    public function render(): string
33
    {
34
        return '<script src="https://www.google.com/recaptcha/api.js?render=' . $this->siteKey . '"></script>
35
        <script>
36
            $(document).ready(function(){
37
                $(\'#captcha\').parent().parent().parent().hide();
38
                grecaptcha.ready(function() {
39
                grecaptcha.execute(\'' . $this->siteKey . '\', {action: \'homepage\'}).then(function(token) {
40
                    $(\'#captcha\').val(token);
41
                });
42
                });
43
            });   
44
        </script>';
45
    }
46
47
    public function isValid($value)
48
    {
49
        $verify = curl_init();
50
        curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
51
        curl_setopt($verify, CURLOPT_POST, true);
52
        curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query([
53
            'secret' => $this->secretKey,
54
            'response' => $value,
55
        ]));
56
        curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
57
        curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
58
        $json = curl_exec($verify);
59
        $data = json_decode($json, true);
60
61
        return $data['success'];
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function getMessages()
68
    {
69
        return [];
70
    }
71
72
}