Completed
Push — master ( 5f8d2a...42c733 )
by Franco
11s
created

enableSpamProtection()   C

Complexity

Conditions 8
Paths 48

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 21
nc 48
nop 1
1
<?php
2
3
namespace SilverStripe\SpamProtection\Extension;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Core\Config\Configurable;
7
use SilverStripe\Core\Extension;
8
use SilverStripe\Core\Injector\Injector;
9
10
/**
11
 * An extension to the {@link Form} class which provides the method
12
 * {@link enableSpamProtection()} helper.
13
 *
14
 * @package spamprotection
15
 */
16
17
class FormSpamProtectionExtension extends Extension
18
{
19
    use Configurable;
20
21
    /**
22
     * @config
23
     *
24
     * The default spam protector class name to use. Class should implement the
25
     * {@link SpamProtector} interface.
26
     *
27
     * @var string $spam_protector
28
     */
29
    private static $default_spam_protector;
0 ignored issues
show
Unused Code introduced by
The property $default_spam_protector 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...
30
31
    /**
32
     * @config
33
     *
34
     * The {@link enableSpamProtection} method will define which of the form
35
     * values correlates to this form mapped fields list. Totally custom forms
36
     * and subclassed SpamProtector instances are define their own mapping
37
     *
38
     * @var array $mappable_fields
39
     */
40
    private static $mappable_fields =  array(
0 ignored issues
show
Unused Code introduced by
The property $mappable_fields 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...
41
        'id',
42
        'title',
43
        'body',
44
        'contextUrl',
45
        'contextTitle',
46
        'authorName',
47
        'authorMail',
48
        'authorUrl',
49
        'authorIp',
50
        'authorId'
51
    );
52
53
    /**
54
     * @config
55
     *
56
     * The field name to use for the {@link SpamProtector} {@link FormField}
57
     *
58
     * @var string $spam_protector
59
     */
60
    private static $field_name = "Captcha";
0 ignored issues
show
Unused Code introduced by
The property $field_name 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...
61
62
    /**
63
     * Instantiate a SpamProtector instance
64
     *
65
     * @param array $options Configuration options
66
     * @return SpamProtector|null
67
     */
68
    public static function get_protector($options = null)
69
    {
70
        // generate the spam protector
71
        if (isset($options['protector'])) {
72
            $protector = $options['protector'];
73
        } else {
74
            $protector = Config::inst()->get(self::class, 'default_spam_protector');
75
        }
76
77
        if ($protector && class_exists($protector)) {
78
            return Injector::inst()->create($protector);
79
        } else {
80
            return null;
81
        }
82
    }
83
84
    /**
85
     * Activates the spam protection module.
86
     *
87
     * @param array $options
88
     * @return Object
89
     */
90
    public function enableSpamProtection($options = array())
91
    {
92
93
        // captcha form field name (must be unique)
94
        if (isset($options['name'])) {
95
            $name = $options['name'];
96
        } else {
97
            $name = Config::inst()->get(self::class, 'field_name');
98
        }
99
100
        // captcha field title
101
        if (isset($options['title'])) {
102
            $title = $options['title'];
103
        } else {
104
            $title = '';
105
        }
106
107
        // set custom mapping on this form
108
        $protector = self::get_protector($options);
109
110
        if (isset($options['mapping'])) {
111
            $protector->setFieldMapping($options['mapping']);
112
        }
113
114
        if ($protector) {
115
            // add the form field
116
            if ($field = $protector->getFormField($name, $title)) {
117
                $field->setForm($this->owner);
118
119
                // Add before field specified by insertBefore
120
                $inserted = false;
121
                if (!empty($options['insertBefore'])) {
122
                    $inserted = $this->owner->Fields()->insertBefore($field, $options['insertBefore']);
123
                }
124
                if (!$inserted) {
125
                    // Add field to end if not added already
126
                    $this->owner->Fields()->push($field);
127
                }
128
            }
129
        }
130
131
        return $this->owner;
132
    }
133
}
134