Completed
Push — master ( 5467e4...183ea4 )
by Boy
05:06 queued 01:02
created

Email   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 57
rs 10
c 2
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A unknown() 0 4 1
A __construct() 0 15 4
A getEmail() 0 4 1
A __toString() 0 4 1
A equals() 0 4 1
A jsonSerialize() 0 4 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\Stepup\Identity\Value;
20
21
use JsonSerializable;
22
use Surfnet\Stepup\Exception\InvalidArgumentException;
23
24
final class Email implements JsonSerializable
25
{
26
    /**
27
     * @var string
28
     */
29
    private $email;
30
31
    /**
32
     * @return self
33
     */
34
    public static function unknown()
35
    {
36
        return new self('[email protected]');
37
    }
38
39
    /**
40
     * @param string $email
41
     */
42
    public function __construct($email)
43
    {
44
        if (!is_string($email) || trim($email) === '') {
45
            throw InvalidArgumentException::invalidType('non-empty string', 'email', $email);
46
        }
47
48
        if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
49
            throw new InvalidArgumentException(sprintf(
50
                'Email given: "%s is not a RFC 822 (https://www.ietf.org/rfc/rfc0822.txt) compliant email address"',
51
                $email
52
            ));
53
        }
54
55
        $this->email = trim($email);
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function getEmail()
62
    {
63
        return $this->email;
64
    }
65
66
    public function __toString()
67
    {
68
        return $this->email;
69
    }
70
71
    public function equals(Email $other)
72
    {
73
        return $this->email === $other->email;
74
    }
75
76
    public function jsonSerialize()
77
    {
78
        return $this->email;
79
    }
80
}
81