FormSpamProtectionExtension::get_protector()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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