-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
134 lines (118 loc) · 2.85 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
const DRYRUN = true;
$people = json_decode(file_get_contents("list.json"));
$result = null;
$i = 0;
while($result === null && $i < 2) {
try {
$result = select($people);
} catch (Exception $e) {
echo "NOTICE: Round " . ($i + 1) . " missed !" . PHP_EOL;
$i++;
}
}
if ($result === null) {
echo "ERROR: No match found !" . PHP_EOL;
} else {
send($people, $result);
}
/**
* @param stdClass $people
*
* @return string[]
**/
function getReceivers(stdClass $people)
{
$receivers = [];
foreach ($people as $name => $properties) {
$receivers[] = $name;
}
return $receivers;
}
/**
* @param stdClass $people
*
* @return string[]
**/
function getExceptions(stdClass $people)
{
$exceptions = [];
foreach ($people as $name => $properties) {
$exceptions[$name] = $properties->exceptions;
$exceptions[$name][] = $name;
}
return $exceptions;
}
/**
* @param stdClass $people
*
* @return array
*
* @throws Exception
**/
function select(stdClass $people)
{
$result = [];
$receivers = getReceivers($people);
$exceptions = getExceptions($people);
foreach ($people as $name => $properties) {
$receiver = null;
$j = 0;
while($receiver === null && $j < 10) {
$key = mt_rand(0, count($receivers) - 1);
if (!in_array($receivers[$key], $exceptions[$name])) {
$receiver = $receivers[$key];
}
$j++;
}
if ($receiver === null) {
throw new Exception("Missed round !");
}
$result[$name] = $receiver;
$exceptions[$receiver][] = $name;
array_splice($receivers, $key, 1);
}
return $result;
}
/**
* @param stdClass $people
* @param array $result
*
* @return void
**/
function send(stdClass $people, array $result)
{
$subject = "Secret Santa";
$from = "Père Noël<[email protected]>";
$messageModel = "Bonjour %s," . PHP_EOL . PHP_EOL .
"Cette année, tu devras faire un cadeau à %s." . PHP_EOL .
"Le budget maximum pour ce cadeau est de 30€" . PHP_EOL . PHP_EOL .
"Joyeux Noël !" . PHP_EOL .
"Le Père Noël";
$headers = "From: $from\r\n" .
"Reply-To: $from\r\n" .
"X-Mailer: PHP/" .phpversion();
foreach ($people as $name => $properties) {
$to = $properties->email;
if ($properties->email == null) {
$sendToCount = count($properties->sendTo);
$key = mt_rand(0, $sendToCount - 1);
if ($properties->sendTo[$key] === $result[$name]) {
$key = ($key + 1) % $sendToCount;
}
$senTo = $properties->sendTo[$key];
$to = $people->$senTo->email;
}
$message = sprintf($messageModel, $name, $result[$name]);
if (DRYRUN) {
$data = $from . PHP_EOL .
$to . PHP_EOL .
$subject . PHP_EOL .
$message;
file_put_contents("test/$name.txt", $data);
} else {
mail($to , $subject , $message, $headers);
echo "INFO: Email sent to $to." . PHP_EOL;
}
}
}