RecaptchaValidator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 4
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 33
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isValid() 0 10 2
A describeObject() 0 4 1
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
}