Completed
Push — master ( 0c443a...572f35 )
by Guillermo
01:33
created

Player::setSecretSanta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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