HoneypotField::validate()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 9.0534
cc 4
eloc 12
nc 2
nop 1
crap 4
1
<?php namespace StudioBonito\SilverStripe\SpamProtection\Honeypot\FormField;
2
3
use FormField;
4
5
class HoneypotField extends \HiddenField
6
{
7
    /**
8
     * The number of seconds before you can submit a valid request.
9
     *
10
     * @var int
11
     * @config
12
     */
13
    private static $time_limit = 5;
0 ignored issues
show
Unused Code introduced by
The property $time_limit is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14
15
    /**
16
     * Reject the field if the honeypot has been filled or if the form has been submitted to quickly.
17
     *
18
     * @param $validator
19
     *
20
     * @return bool
21
     */
22 6
    public function validate($validator)
23
    {
24 6
        $timeLimit = $this->config()->time_limit;
25
26 6
        $timestamp = $this->getForm()->getController()->getRequest()->postVar($this->getName() . '_Timestamp');
27
28 6
        if (!empty($this->value) || ($timeLimit > 0 && ($timestamp + $timeLimit) > time())) {
29 3
            $validator->validationError(
30 3
                $this->name,
31 3
                _t(
32 3
                    'HoneypotField.SPAM',
33
                    'Your submission has been rejected because it was treated as spam.'
34 3
                ),
35
                'error'
36 3
            );
37
38 3
            return false;
39
        }
40
41 3
        return true;
42
    }
43
44
    /**
45
     * Override the Type to remove the class namespace.
46
     *
47
     * @codeCoverageIgnore
48
     *
49
     * @return string
50
     */
51
    public function Type()
52
    {
53
        return 'honeypotspamprotector';
54
    }
55
56
    /**
57
     * Override the Field to add the Captcha and Timestamp fields.
58
     *
59
     * @codeCoverageIgnore
60
     *
61
     * @param array $properties
62
     *
63
     * @return string
64
     */
65
    public function Field($properties = array())
66
    {
67
        return $this->createHoneypotField() . $this->createTimestampField();
68
    }
69
70
    /**
71
     * Create the Captcha Field.
72
     *
73
     * @codeCoverageIgnore
74
     *
75
     * @return string
76
     */
77
    protected function createHoneypotField()
78
    {
79
        return FormField::create_tag(
80
            'input',
81
            array(
82
                'type'  => 'text',
83
                'id'    => $this->ID(),
84
                'name'  => $this->getName(),
85
                'value' => $this->Value(),
86
                'style' => 'display:none!important',
87
            )
88
        );
89
    }
90
91
    /**
92
     * Create the Timestamp Field.
93
     *
94
     * @codeCoverageIgnore
95
     *
96
     * @return string
97
     */
98
    protected function createTimestampField()
99
    {
100
        return FormField::create_tag(
101
            'input',
102
            array(
103
                'type'  => 'text',
104
                'id'    => $this->ID() . '_Timestamp',
105
                'name'  => $this->getName() . '_Timestamp',
106
                'value' => time(),
107
                'style' => 'display:none!important',
108
            )
109
        );
110
    }
111
}
112