Completed
Push — master ( 9a5776...45f1dd )
by Peter
01:54
created

SesIdentity::getDomain()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace PeterNijssen\Ses\Model;
4
5
use PeterNijssen\Ses\Exception\InvalidIdentityException;
6
7
/**
8
 * SES identity
9
 */
10
class SesIdentity
11
{
12
    /**
13
     * @var string
14
     */
15
    private $identity;
16
17
    /**
18
     * Identity constructor.
19
     *
20
     * @param string $identity emailAddress|domain
21
     *
22
     * @throws InvalidIdentityException
23
     */
24 4
    public function __construct($identity)
25
    {
26 4
        if (!preg_match('/^.+\@\S+\.\S+$/', $identity)
27 4
            && !preg_match('/^((\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,32}|[0-9]{1,32})(\]?))$/', $identity)) {
28 1
            throw new InvalidIdentityException("The identity is not a valid domain or email address");
29
        }
30
31
32 4
        $this->identity = $identity;
33 4
    }
34
35
    /**
36
     * Get the identity. If correct, this is either an email address or domain
37
     *
38
     * @return string
39
     */
40 1
    public function getIdentity()
41
    {
42 1
        return $this->identity;
43
    }
44
45
    /**
46
     * Get the type for this identity
47
     *
48
     * @return string
49
     */
50 1
    public function getType()
51
    {
52 1
        if (preg_match('/@/', $this->identity)) {
53 1
            return "email";
54
        }
55
56 1
        return "domain";
57
    }
58
59
    /**
60
     * Get the domain for this identity
61
     *
62
     * @return string
63
     */
64 1
    public function getDomain()
65
    {
66 1
        if (preg_match("/@([^\s]+)/iu", $this->identity, $info)) {
67 1
            return $info[1];
68
        }
69
70 1
        return $this->identity;
71
    }
72
}
73