Failed Conditions
Push — psr2 ( 8eb28c...c68e26 )
by Andreas
28s queued 20s
created

SettingEmail   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
C update() 0 45 11
1
<?php
2
3
namespace dokuwiki\plugin\config\core\Setting;
4
5
/**
6
 * Class setting_email
7
 */
8
class SettingEmail extends SettingString {
9
    protected $multiple = false;
10
    protected $placeholders = false;
11
12
    /** @inheritdoc */
13
    public function update($input) {
14
        if(is_null($input)) return false;
15
        if($this->isProtected()) return false;
16
17
        $value = is_null($this->local) ? $this->default : $this->local;
18
        if($value == $input) return false;
19
        if($input === '') {
20
            $this->local = $input;
21
            return true;
22
        }
23
        $mail = $input;
24
25
        if($this->placeholders) {
26
            // replace variables with pseudo values
27
            $mail = str_replace('@USER@', 'joe', $mail);
28
            $mail = str_replace('@NAME@', 'Joe Schmoe', $mail);
29
            $mail = str_replace('@MAIL@', '[email protected]', $mail);
30
        }
31
32
        // multiple mail addresses?
33
        if($this->multiple) {
34
            $mails = array_filter(array_map('trim', explode(',', $mail)));
35
        } else {
36
            $mails = array($mail);
37
        }
38
39
        // check them all
40
        foreach($mails as $mail) {
41
            // only check the address part
42
            if(preg_match('#(.*?)<(.*?)>#', $mail, $matches)) {
43
                $addr = $matches[2];
44
            } else {
45
                $addr = $mail;
46
            }
47
48
            if(!mail_isvalid($addr)) {
49
                $this->error = true;
50
                $this->input = $input;
51
                return false;
52
            }
53
        }
54
55
        $this->local = $input;
56
        return true;
57
    }
58
}
59