1 | <?php |
||
11 | class SecretSanta |
||
12 | { |
||
13 | /** @var PlayersCollection */ |
||
14 | private $players; |
||
15 | /** @var array */ |
||
16 | private $combination; |
||
17 | |||
18 | /** |
||
19 | * SecretSanta constructor. |
||
20 | */ |
||
21 | 12 | public function __construct() |
|
25 | |||
26 | /** |
||
27 | * @param string $name |
||
28 | * @param string $email |
||
29 | * @return SecretSanta |
||
30 | */ |
||
31 | 5 | public function addPlayer($name, $email) |
|
37 | |||
38 | /** |
||
39 | * @param string $name |
||
40 | * @param string $email |
||
41 | * @param string $coupleName |
||
42 | * @param string $coupleEmail |
||
43 | * @return SecretSanta |
||
44 | */ |
||
45 | 11 | public function addCouple($name, $email, $coupleName, $coupleEmail) |
|
54 | |||
55 | /** |
||
56 | * @return Player[] |
||
57 | * @throws SecretSantaException |
||
58 | */ |
||
59 | 9 | public function play() |
|
71 | |||
72 | /** |
||
73 | * @throws SecretSantaException |
||
74 | */ |
||
75 | 9 | private function combinePlayers() |
|
76 | { |
||
77 | 9 | if (count($this->players) < 4) { |
|
78 | 1 | throw new SecretSantaException("Not enough players to play, at least 4 players are required"); |
|
79 | } |
||
80 | |||
81 | 8 | $retry = count($this->players) + $this->players->countExcludePlayers(); |
|
82 | |||
83 | 8 | while (!$this->tryMatchSecretSantaPlayers() && $retry > 0 ) { |
|
84 | $retry--; |
||
85 | } |
||
86 | |||
87 | 8 | if (!$this->isValidCombination()) { |
|
88 | throw new SecretSantaException("Not enough players to play"); |
||
89 | } |
||
90 | 8 | } |
|
91 | |||
92 | /** |
||
93 | * @return bool |
||
94 | */ |
||
95 | 8 | private function tryMatchSecretSantaPlayers() |
|
111 | |||
112 | /** |
||
113 | * @param Player $player |
||
114 | * @param Player $secretPlayer |
||
115 | * @return bool |
||
116 | */ |
||
117 | 8 | private function isValidSecretSanta($player, $secretPlayer) |
|
127 | |||
128 | /** |
||
129 | * @return bool |
||
130 | */ |
||
131 | 8 | private function isValidCombination() |
|
135 | |||
136 | 8 | private function associatePlayers() |
|
146 | } |
||
147 |