Passed
Push — master ( 4633d8...be0c82 )
by Alain
02:10
created

EmailAddress::getDomainPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
    public function __construct($email)
50
    {
51
        if (false === filter_var($email, FILTER_VALIDATE_EMAIL)) {
52
            throw InvalidEmailAddress::from($email);
0 ignored issues
show
Bug introduced by
It seems like $email defined by parameter $email on line 49 can also be of type object<BrightNucleus\Cha...l\Support\EmailAddress>; however, BrightNucleus\Chainmail\...lidEmailAddress::from() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
53
        }
54
55
        try {
56
            $parts           = explode('@', $email);
57
            $this->localPart = trim($parts[0]);
58
            $this->domain    = new Domain(trim($parts[1]));
59
        } catch (Exception $exception) {
60
            throw InvalidEmailAddress::from($email);
0 ignored issues
show
Bug introduced by
It seems like $email defined by parameter $email on line 49 can also be of type object<BrightNucleus\Cha...l\Support\EmailAddress>; however, BrightNucleus\Chainmail\...lidEmailAddress::from() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
61
        }
62
    }
63
64
    /**
65
     * Returns the local part of the email address.
66
     *
67
     * @return string
68
     */
69
    public function getLocalPart()
70
    {
71
        return $this->localPart;
72
    }
73
74
    /**
75
     * Returns the domain part of the email address.
76
     *
77
     * @return Domain
78
     */
79
    public function getDomainPart()
80
    {
81
        return $this->domain;
82
    }
83
84
    /**
85
     * Returns the entire email address.
86
     *
87
     * @return string
88
     */
89
    public function getAddress()
90
    {
91
        return $this->getLocalPart() . '@' . $this->getDomainPart();
92
    }
93
94
    /**
95
     * Convert the EmailAddress object to a string.
96
     *
97
     * @return string
98
     */
99
    public function __toString()
100
    {
101
        return $this->getAddress();
102
    }
103
}