Completed
Push — master ( ef6de7...8edde9 )
by Guillermo
14:11
created

Player::setName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace SecretSanta;
4
5
use SecretSanta\Exceptions\PlayerException;
6
use Ramsey\Uuid\Uuid;
7
8
/**
9
 * Class Player
10
 * @package SecretSanta
11
 */
12
class Player
13
{
14
    /** @var string */
15
    private $id;
16
    /** @var string */
17
    private $name;
18
    /** @var string */
19
    private $email;
20
    /** @var Player */
21
    private $secretSanta;
22
23
    /**
24
     * Player constructor.
25
     * @param string $name
26
     * @param string $email
27
     */
28
    private function __construct(string $name, string $email)
29
    {
30
        $this->setName($name);
31
        $this->setEmail($email);
32
    }
33
34
    /**
35
     * @param string $name
36
     * @param string $email
37
     * @return Player
38
     */
39
    public static function create(string $name, string $email)
40
    {
41
        return new self($name, $email);
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function id()
48
    {
49
        if (is_null($this->id)) {
50
            $this->generateId();
51
        }
52
53
        return $this->id;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function name()
60
    {
61
        return $this->name;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function email()
68
    {
69
        return $this->email;
70
    }
71
    
72
    /**
73
     * @throws PlayerException
74
     * @return Player
75
     */
76
    public function secretSanta()
77
    {
78
        if (is_null($this->secretSanta)) {
79
            throw new PlayerException("Secret Santa is not yet generated");
80
        }
81
        
82
        return $this->secretSanta;
83
    }
84
    
85
    /**
86
     * @param Player $secretSanta
87
     * @return Player
88
     */
89
    public function setSecretSanta(Player $secretSanta)
90
    {
91
        $this->secretSanta = $secretSanta;
92
        
93
        return $this;
94
    }
95
96
    /**
97
     * @param string $name
98
     * @throws PlayerException
99
     * @return Player
100
     */
101
    private function setName(string $name)
102
    {
103
        if (strlen($name) < 3) {
104
            throw new PlayerException("Name must have more than 3 characters");
105
        }
106
107
        $this->name = $name;
108
        
109
        return $this;
110
    }
111
112
    /**
113
     * @param string $email
114
     * @throws PlayerException
115
     * @return Player
116
     */
117
    private function setEmail(string $email)
118
    {
119
        if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
120
            throw new PlayerException("Email must be a valid format");
121
        }
122
123
        $this->email = $email;
124
        
125
        return $this;
126
    }
127
128
    /**
129
     * @return Player
130
     */
131
    private function generateId()
132
    {
133
        $this->id = Uuid::uuid5(Uuid::NAMESPACE_DNS, $this->email)->toString();
134
        
135
        return $this;
136
    }
137
}
138