|
1
|
|
|
<?php |
|
2
|
|
|
namespace PhpDraft\Domain\Repositories; |
|
3
|
|
|
|
|
4
|
|
|
use Silex\Application; |
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
6
|
|
|
use PhpDraft\Domain\Entities\Draft; |
|
7
|
|
|
use \PhpDraft\Domain\Entities\RoundTime; |
|
8
|
|
|
use PhpDraft\Domain\Models\RoundTimeCreateModel; |
|
9
|
|
|
|
|
10
|
|
|
class RoundTimeRepository { |
|
11
|
|
|
private $app; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(Application $app) { |
|
14
|
|
|
$this->app = $app; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/*public function GetPublicTimers($draftId) { |
|
18
|
|
|
//TODO: Implement |
|
19
|
|
|
}*/ |
|
20
|
|
|
|
|
21
|
|
|
public function GetDraftTimers(Draft $draft) { |
|
22
|
|
|
$draftId = (int)$draft->draft_id; |
|
23
|
|
|
|
|
24
|
|
|
$timerStmt = $this->app['db']->prepare("SELECT * FROM round_times |
|
25
|
|
|
WHERE draft_id = ? ORDER BY draft_round ASC"); |
|
26
|
|
|
|
|
27
|
|
|
$timerStmt->setFetchMode(\PDO::FETCH_CLASS, '\PhpDraft\Domain\Entities\RoundTime'); |
|
28
|
|
|
$timerStmt->bindParam(1, $draftId); |
|
29
|
|
|
|
|
30
|
|
|
if (!$timerStmt->execute()) { |
|
31
|
|
|
throw new \Exception("Unable to load round times."); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$timers = array(); |
|
35
|
|
|
|
|
36
|
|
|
while ($timer = $timerStmt->fetch()) { |
|
37
|
|
|
$timers[] = $timer; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$isStaticTime = false; |
|
41
|
|
|
|
|
42
|
|
|
if (count($timers) == 1 && $timers[0]->is_static_time) { |
|
43
|
|
|
$isStaticTime = true; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (empty($timers) || count($timers) != $draft->draft_rounds) { |
|
47
|
|
|
$timers = $this->_CoalesceDraftTimers($draft, $timers, $isStaticTime); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $timers; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function _CoalesceDraftTimers(Draft $draft, $existing_timers, $isStaticTime) { |
|
54
|
|
|
$coalescedTimers = array(); |
|
55
|
|
|
|
|
56
|
|
|
for ($i = 0; $i < $draft->draft_rounds; $i++) { |
|
57
|
|
|
if ($isStaticTime && $i == 0) { |
|
58
|
|
|
$timer = $existing_timers[$i]; |
|
59
|
|
|
$timer->draft_round = $i + 1; |
|
60
|
|
|
$coalescedTimers[] = $timer; |
|
61
|
|
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (array_key_exists($i, $existing_timers)) { |
|
65
|
|
|
$coalescedTimers[] = $existing_timers[$i]; |
|
66
|
|
|
} else { |
|
67
|
|
|
$newTimer = new RoundTime(); |
|
68
|
|
|
$newTimer->draft_id = $draft->draft_id; |
|
69
|
|
|
$newTimer->is_static_time = false; |
|
70
|
|
|
$newTimer->draft_round = $i + 1; |
|
71
|
|
|
$newTimer->round_time_seconds = 0; |
|
72
|
|
|
$coalescedTimers[] = $newTimer; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $coalescedTimers; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function Save(RoundTimeCreateModel $roundTimeCreateModel) { |
|
80
|
|
|
$insertTimeStmt = $this->app['db']->prepare("INSERT INTO round_times |
|
81
|
|
|
(draft_id, is_static_time, draft_round, round_time_seconds) |
|
82
|
|
|
VALUES |
|
83
|
|
|
(:draft_id, :is_static_time, :draft_round, :round_time_seconds)"); |
|
84
|
|
|
|
|
85
|
|
|
$newRoundTimes = array(); |
|
86
|
|
|
|
|
87
|
|
|
foreach ($roundTimeCreateModel->roundTimes as $roundTime) { |
|
88
|
|
|
$insertTimeStmt->bindValue(":draft_id", $roundTime->draft_id); |
|
89
|
|
|
$insertTimeStmt->bindValue(":is_static_time", $roundTime->is_static_time); |
|
90
|
|
|
$insertTimeStmt->bindValue(":draft_round", $roundTime->draft_round); |
|
91
|
|
|
$insertTimeStmt->bindValue(":round_time_seconds", $roundTime->round_time_seconds); |
|
92
|
|
|
|
|
93
|
|
|
if (!$insertTimeStmt->execute()) { |
|
94
|
|
|
throw new \Exception("Unable to save round times for $roundTime->draft_id"); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$roundTime->round_time_id = (int)$this->app['db']->lastInsertId(); |
|
98
|
|
|
$newRoundTimes[] = $roundTime; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $newRoundTimes; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function DeleteAll($draftId) { |
|
105
|
|
|
$deleteRoundTime = $this->app['db']->prepare("DELETE FROM round_times WHERE draft_id = ?"); |
|
106
|
|
|
$deleteRoundTime->bindParam(1, $draftId); |
|
107
|
|
|
|
|
108
|
|
|
if (!$deleteRoundTime->execute()) { |
|
109
|
|
|
throw new \Exception("Unable to delete round times: " . $this->app['db']->errorInfo()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function LoadByRound(Draft $draft) { |
|
116
|
|
|
$roundTime = new RoundTime(); |
|
117
|
|
|
|
|
118
|
|
|
$staticRoundTimeStmt = $this->app['db']->prepare("SELECT * FROM round_times WHERE draft_id = ? AND is_static_time = 1 LIMIT 1"); |
|
119
|
|
|
$staticRoundTimeStmt->setFetchMode(\PDO::FETCH_INTO, $roundTime); |
|
120
|
|
|
$staticRoundTimeStmt->bindParam(1, $draft->draft_id); |
|
121
|
|
|
|
|
122
|
|
|
if (!$staticRoundTimeStmt->execute()) { |
|
123
|
|
|
throw new \Exception("Unable to get static round time."); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if ($staticRoundTimeStmt->rowCount() == 1) { |
|
127
|
|
|
$staticRoundTimeStmt->fetch(); |
|
128
|
|
|
|
|
129
|
|
|
return $roundTime; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$roundTimeStmt = $this->app['db']->prepare("SELECT * FROM round_times WHERE draft_id = ? AND draft_round = ? LIMIT 1"); |
|
133
|
|
|
$roundTimeStmt->setFetchMode(\PDO::FETCH_INTO, $roundTime); |
|
134
|
|
|
$roundTimeStmt->bindParam(1, $draft->draft_id); |
|
135
|
|
|
$roundTimeStmt->bindParam(2, $draft->draft_current_round); |
|
136
|
|
|
|
|
137
|
|
|
if (!$roundTimeStmt->execute()) { |
|
138
|
|
|
throw new \Exception("Unable to load round time:" . $this->app['db']->errorInfo()); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if ($roundTimeStmt->rowCount() == 0) { |
|
142
|
|
|
return null; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if (!$roundTimeStmt->fetch()) { |
|
146
|
|
|
throw new \Exception("Unable to load round time:" . $this->app['db']->errorInfo()); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $roundTime; |
|
150
|
|
|
} |
|
151
|
|
|
} |