EmailAddress   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A __toString() 0 4 1
A getValue() 0 4 1
A getRecipient() 0 4 1
A getDomain() 0 4 1
A getTld() 0 4 1
A getValueAsArray() 0 8 1
A isEqualTo() 0 4 1
A explodeEmail() 0 9 1
1
<?php
2
3
namespace Email;
4
5
use Email\Exception\InvalidEmailAddressException;
6
7
/**
8
 * Class EmailAddress
9
 */
10
class EmailAddress
11
{
12
    /**
13
     * @var
14
     */
15
    private $recipient;
16
17
    /**
18
     * @var
19
     */
20
    private $domain;
21
22
    /**
23
     * @var
24
     */
25
    private $tld;
26
27
    /**
28
     * EmailAddress constructor.
29
     * @param $email
30
     * @throws InvalidEmailAddressException
31
     */
32
    public function __construct($email)
33
    {
34
        if (false === filter_var($email, FILTER_VALIDATE_EMAIL)) {
35
            throw new InvalidEmailAddressException("{$email} is not a valid email address");
36
        }
37
38
        $this->explodeEmail($email);
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function __toString() : string
45
    {
46
        return $this->getValue();
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getValue()
53
    {
54
        return "{$this->recipient}@{$this->domain}.{$this->tld}";
55
    }
56
57
    /**
58
     * @return mixed
59
     */
60
    public function getRecipient() : string
61
    {
62
        return $this->recipient;
63
    }
64
65
    /**
66
     * @return mixed
67
     */
68
    public function getDomain() : string
69
    {
70
        return $this->domain;
71
    }
72
73
    /**
74
     * @return mixed
75
     */
76
    public function getTld() : string
77
    {
78
        return $this->tld;
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    public function getValueAsArray() : array
85
    {
86
        return [
87
            'recipient' => $this->getRecipient(),
88
            'domain'    => $this->getDomain(),
89
            'tld'       => $this->getTld()
90
        ];
91
    }
92
93
    public function isEqualTo(EmailAddress $email) : bool
94
    {
95
        return $this->getValue() === $email->getValue();
96
    }
97
98
    /**
99
     * @param $email
100
     * @return EmailAddress
101
     */
102
    private function explodeEmail($email)
103
    {
104
        list($recipient, $domain) = explode("@", $email);
105
        list($domain, $tld) = explode(".", $domain, 2);
106
107
        $this->recipient = $recipient;
108
        $this->domain    = $domain;
109
        $this->tld       = $tld;
110
    }
111
}
112