RecaptchaValidator::describeObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace fieldwork\validators;
4
5
use fieldwork\components\Recaptcha;
6
7
class RecaptchaValidator extends FieldValidator
8
{
9
10
    private $recaptchaField;
11
12
    const ENDPOINT = 'https://www.google.com/recaptcha/api/siteverify';
13
14
    public function __construct ($errorMsg, Recaptcha $recaptchaField)
15
    {
16
        parent::__construct($errorMsg);
17
        $this->recaptchaField = $recaptchaField;
18
    }
19
20
    public function isValid ($value)
21
    {
22
        $response = \Requests::post(self::ENDPOINT, array(), array(
23
            'secret'   => $this->recaptchaField->getSecretKey(),
24
            'response' => $value,
25
            'remoteip' => $_SERVER['REMOTE_ADDR']
26
        ));
27
        $data     = json_decode($response->body, true);
28
        return $data !== null ? $data['success'] : false;
29
    }
30
31
    /**
32
     * Returns a short human-readable slug/string describing the object
33
     * @return string
34
     */
35
    function describeObject ()
36
    {
37
        return 'recaptcha';
38
    }
39
}