Address   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 48
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidEmail() 0 3 1
A getEmail() 0 3 1
A __construct() 0 14 4
A getName() 0 3 1
1
<?php
2
3
namespace Kodus\Mail;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
8
/**
9
 * This model represents an e-mail address and (optionally) associated display-name.
10
 */
11
class Address
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $email;
17
18
    /**
19
     * @var string|null
20
     */
21
    protected $name;
22
23
    /**
24
     * @param string      $email valid e-mail address
25
     * @param string|null $name  display name (optional)
26
     *
27
     * @throws InvalidArgumentException for invalid e-mail address
28
     * @throws RuntimeException on attempted CRLF name injection
29
     */
30 21
    public function __construct(string $email, ?string $name = null)
31
    {
32 21
        if (! self::isValidEmail($email)) {
33 1
            throw new InvalidArgumentException("invalid e-mail address");
34
        }
35
36 20
        if (! empty($name)) {
37 12
            if (preg_match('/[\r\n]/', $name)) {
38 1
                throw new RuntimeException("CR/LF injection detected");
39
            }
40 11
        }
41
42 19
        $this->email = $email;
43 19
        $this->name = $name;
44 19
    }
45
46
    public static function isValidEmail(string $email): bool
47
    {
48
        return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
49
    }
50
51 21
    public function getEmail(): string
52
    {
53 21
        return $this->email;
54
    }
55
56
    public function getName(): ?string
57
    {
58
        return $this->name;
59 17
    }
60
}
61