Completed
Push — master ( 58d788...87a873 )
by Ronaldo
01:37
created

Email::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace WSW\Email;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Class Email
9
 *
10
 * @package WSW\Email
11
 * @author Ronaldo Matos Rodrigues <[email protected]>
12
 */
13
final class Email
14
{
15
    use Validator;
16
17
    /**
18
     * @var string
19
     */
20
    private $username;
21
22
    /**
23
     * @var string
24
     */
25
    private $hostname;
26
27
    /**
28
     * @param string $email
29
     */
30 8
    public function __construct($email)
31
    {
32 8
        if (!$this->emailIsValid($email)) {
33 1
            throw new InvalidArgumentException('You should inform a valid email.');
34
        }
35
36 7
        if (!$this->mxIsValid($email)) {
37 1
            throw new InvalidArgumentException('You must provide an email with valid MX.');
38
        }
39
40 6
        list($this->username, $this->hostname) = explode('@', $email);
41 6
    }
42
43
    /**
44
     * @return string
45
     */
46 2
    public function getEmail()
47
    {
48 2
        return sprintf('%s@%s', $this->username, $this->hostname);
49
    }
50
51
    /**
52
     * @return string
53
     */
54 1
    public function getUsername()
55
    {
56 1
        return $this->username;
57
    }
58
59
    /**
60
     * @return string
61
     */
62 2
    public function getHostname()
63
    {
64 2
        return $this->hostname;
65
    }
66
67
    /**
68
     * @return array
69
     */
70 1
    public function getMx()
71
    {
72 1
        getmxrr($this->getHostname(), $arr);
73
74 1
        return $arr;
75
    }
76
77
    /**
78
     * @return string
79
     */
80 1
    public function __toString()
81
    {
82 1
        return $this->getEmail();
83
    }
84
}
85