Player::setName()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

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