Passed
Push — master ( be0c82...f539f4 )
by Alain
02:33
created

EmailAddress::getDomain()   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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * BrightNucleus Chainmail Component.
4
 *
5
 * @package   BrightNucleus/Chainmail
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\ChainMail\Support;
13
14
use BrightNucleus\Chainmail\Exception\InvalidEmailAddress;
15
use Exception;
16
17
/**
18
 * Class EmailAddress.
19
 *
20
 * @since   1.0.0
21
 *
22
 * @package BrightNucleus\ChainMail
23
 * @author  Alain Schlesser <[email protected]>
24
 */
25
class EmailAddress
26
{
27
28
    /**
29
     * The local part of the email address (before the '@' sign).
30
     *
31
     * @var string
32
     */
33
    protected $localPart;
34
35
    /**
36
     * The domain part of the email address (after the '@' sign).
37
     *
38
     * @var Domain
39
     */
40
    protected $domain;
41
42
    /**
43
     * Instantiate an EmailAddress object.
44
     *
45
     * @param EmailAddress|string $email The email to parse.
46
     *
47
     * @throws InvalidEmailAddress If the email is not valid.
48
     */
49 8
    public function __construct($email)
50
    {
51 8
        if ($email instanceof EmailAddress) {
52 1
            $this->localPart = $email->getLocalPart();
53 1
            $this->domain    = $email->getDomain();
54
        } else {
55 7
            if ( ! $this->isValid($email)) {
56 1
                throw InvalidEmailAddress::from($email);
57
            }
58
59
            try {
60 6
                $parts           = explode('@', $email);
61 6
                $this->localPart = trim($parts[0]);
62 6
                $this->domain    = new Domain(trim($parts[1]));
63
            } catch (Exception $exception) {
64
                throw InvalidEmailAddress::from($email);
65
            }
66
        }
67 7
    }
68
69
    /**
70
     * Returns the local part of the email address.
71
     *
72
     * @return string
73
     */
74 2
    public function getLocalPart()
75
    {
76 2
        return $this->localPart;
77
    }
78
79
    /**
80
     * Returns the domain part of the email address.
81
     *
82
     * @return Domain
83
     */
84 2
    public function getDomain()
85
    {
86 2
        return $this->domain;
87
    }
88
89
    /**
90
     * Returns the entire email address.
91
     *
92
     * @return string
93
     */
94 2
    public function getAddress()
95
    {
96 2
        return $this->getLocalPart() . '@' . $this->getDomain();
97
    }
98
99
    /**
100
     * Convert the EmailAddress object to a string.
101
     *
102
     * @return string
103
     */
104 2
    public function __toString()
105
    {
106 2
        return $this->getAddress();
107
    }
108
109
    /**
110
     * Check whether an email is valid.
111
     *
112
     * @param string $email Email to check.
113
     *
114
     * @return bool Whether the email is valid.
115
     */
116 7
    protected function isValid($email)
117
    {
118 7
        return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
119
    }
120
}