Author   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 92
ccs 27
cts 27
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A setName() 0 4 1
A getEmail() 0 4 1
A __toString() 0 4 1
A __construct() 0 12 2
A setEmail() 0 7 2
A toArray() 0 7 1
1
<?php
2
namespace Samurai\Project;
3
4
/**
5
 * Class Author
6
 * @package Samurai\Project
7
 * @author Raphaël Lefebvre <[email protected]>
8
 */
9
class Author 
10
{
11
    /**
12
     * @var string
13
     */
14
    private $name;
15
16
    /**
17
     * @var string
18
     */
19
    private $email;
20
21
    /**
22
     * $author must be formatted like an email of the RFC 822
23
     * ex: name <[email protected]>
24
     * @param string $author
25
     */
26 14
    public function __construct($author)
27
    {
28 14
        if (preg_match('/^(?P<name>[- \.,\p{L}\'’]+) <(?P<email>.+?)>$/u', $author, $match)) {
29 11
            $this->setName($match['name']);
30 11
            $this->setEmail($match['email']);
31 10
        }else{
32 4
            throw new \InvalidArgumentException(sprintf(
33 4
                'Invalid author "%s".  Must be in the format: "name <[email protected]>"',
34
                $author
35 4
            ));
36
        }
37 10
    }
38
39
    /**
40
     * Getter of $name
41
     *
42
     * @return string
43
     */
44 10
    public function getName()
45
    {
46 10
        return $this->name;
47
    }
48
49
    /**
50
     * Setter of $name
51
     *
52
     * @param string $name
53
     */
54 11
    public function setName($name)
55
    {
56 11
        $this->name = (string)$name;
57 11
    }
58
59
    /**
60
     * Getter of $email
61
     *
62
     * @return string
63
     */
64 10
    public function getEmail()
65
    {
66 10
        return $this->email;
67
    }
68
69
    /**
70
     * Setter of $email
71
     *
72
     * @param string $email
73
     */
74 11
    public function setEmail($email)
75
    {
76 11
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
77 1
            throw new \InvalidArgumentException(sprintf('Invalid email "%s"', $email));
78
        }
79 10
        $this->email = (string)$email;
80 10
    }
81
82
    /**
83
     * @return array
84
     */
85 5
    public function toArray()
86
    {
87
        return[
88 5
            'name' => $this->getName(),
89 5
            'email' => $this->getEmail(),
90 5
        ];
91
    }
92
93
    /**
94
     * @return string
95
     */
96 5
    public function __toString()
97
    {
98 5
        return $this->getName() . ' <' . $this->getEmail() . '>';
99
    }
100
}
101