UserEmail   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A email() 0 4 1
A localPart() 0 4 1
A domain() 0 4 1
A equals() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the BenGorUser package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorUser\User\Domain\Model;
14
15
use BenGorUser\User\Domain\Model\Exception\UserEmailInvalidException;
16
17
/**
18
 * User email domain class.
19
 *
20
 * @author Beñat Espiña <[email protected]>
21
 * @author Gorka Laucirica <[email protected]>
22
 */
23
final class UserEmail
24
{
25
    /**
26
     * The email in plain string.
27
     *
28
     * @var string
29
     */
30
    private $email;
31
32
    /**
33
     * The domain of email, for example: "gmail.com".
34
     *
35
     * @var string
36
     */
37
    private $domain;
38
39
    /**
40
     * The part that exists before the @.
41
     *
42
     * @var string
43
     */
44
    private $localPart;
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param string $anEmail An email in primitive string
50
     *
51
     * @throws UserEmailInvalidException when the email is not valid
52
     */
53
    public function __construct($anEmail)
54
    {
55
        if (!filter_var($anEmail, FILTER_VALIDATE_EMAIL)) {
56
            throw new UserEmailInvalidException();
57
        }
58
        $this->email = $anEmail;
59
        $this->localPart = implode(explode('@', $this->email, -1), '@');
60
        $this->domain = str_replace($this->localPart . '@', '', $this->email);
61
    }
62
63
    /**
64
     * Gets the email.
65
     *
66
     * @return string
67
     */
68
    public function email()
69
    {
70
        return $this->email;
71
    }
72
73
    /**
74
     * Gets the local part of email address, for example: "info" into "[email protected]".
75
     *
76
     * @return string
77
     */
78
    public function localPart()
79
    {
80
        return $this->localPart;
81
    }
82
83
    /**
84
     * Gets the domain of email address, for example: "gmail.com".
85
     *
86
     * @return string
87
     */
88
    public function domain()
89
    {
90
        return $this->domain;
91
    }
92
93
    /**
94
     * Method that checks if the email given is equal to the current.
95
     *
96
     * @param UserEmail $anEmail The email
97
     *
98
     * @return bool
99
     */
100
    public function equals(UserEmail $anEmail)
101
    {
102
        return strtolower((string) $this) === strtolower((string) $anEmail);
103
    }
104
105
    /**
106
     * Magic method that represents the user email in string format.
107
     *
108
     * @return string
109
     */
110
    public function __toString()
111
    {
112
        return $this->email;
113
    }
114
}
115