Completed
Push — master ( 6c3848...bdcf35 )
by Basil
06:33
created

EmailLink::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace luya\web;
4
5
use yii\validators\EmailValidator;
6
use yii\base\InvalidConfigException;
7
8
/**
9
 * Email Link.
10
 *
11
 * Represent a {{luya\web\LinkInterface}} of an e-mail mailto link.
12
 *
13
 * @author Basil Suter <[email protected]>
14
 * @since 1.0.0
15
 */
16
class EmailLink extends BaseLink
17
{
18
    private $_email;
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function init()
24
    {
25
        parent::init();
26
        
27
        if ($this->email === null) {
0 ignored issues
show
Bug introduced by
The property email does not seem to exist. Did you mean _email?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
28
            throw new InvalidConfigException('The email attribute can not be empty and must be set trough configuration array.');
29
        }
30
    }
31
    
32
    /**
33
     * Setter method for e-mail.
34
     *
35
     * If no valid email is provided, not value is set.
36
     *
37
     * @param string $email The e-mail which should be used for the mailto link.
38
     */
39
    public function setEmail($email)
40
    {
41
        $validator = new EmailValidator();
42
        if ($validator->validate($email)) {
43
            $this->_email = $email;
44
        } else {
45
            $this->_email = false;
46
        }
47
    }
48
    
49
    /**
50
     * Getter method for the e-mail.
51
     *
52
     * @return string|boolean Returns the e-mail from the setter method, if mail is not valid false is returned.
53
     */
54
    public function getEmail()
55
    {
56
        return $this->_email;
57
    }
58
    
59
    /**
60
     * @inheritdoc
61
     */
62
    public function getHref()
63
    {
64
        return empty($this->getEmail()) ? null : 'mailto:' . $this->getEmail();
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function getTarget()
71
    {
72
        return '_blank';
73
    }
74
}
75