1
|
|
|
<?php |
2
|
|
|
namespace PhpDraft\Domain\Validators; |
3
|
|
|
|
4
|
|
|
use \Silex\Application; |
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use PhpDraft\Domain\Entities\LoginUser; |
7
|
|
|
use PhpDraft\Domain\Entities\Draft; |
8
|
|
|
use PhpDraft\Domain\Models\PhpDraftResponse; |
9
|
|
|
use Symfony\Component\Security\Core\Util\StringUtils; |
10
|
|
|
use Egulias\EmailValidator\EmailValidator; |
11
|
|
|
|
12
|
|
|
class DraftValidator { |
13
|
|
|
private $app; |
14
|
|
|
|
15
|
|
|
public function __construct(Application $app) { |
16
|
|
|
$this->app = $app; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function IsDraftViewableForUser($draft_id, Request $request) { |
20
|
|
|
$draft = $this->app['phpdraft.DraftRepository']->Load($draft_id); |
21
|
|
|
$current_user = $this->app['phpdraft.LoginUserService']->GetUserFromHeaderToken($request); |
22
|
|
|
$draft_password = $request->headers->get(DRAFT_PASSWORD_HEADER, ''); |
|
|
|
|
23
|
|
|
|
24
|
|
|
if($current_user != null && ($draft->commish_id == $current_user->id || $this->app['phpdraft.LoginUserService']->CurrentUserIsAdmin($current_user))) { |
25
|
|
|
return true; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if(empty($draft->draft_password) || ($draft->draft_password == $draft_password)) { |
29
|
|
|
return true; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function IsDraftEditableForUser(Draft $draft, LoginUser $current_user) { |
36
|
|
|
if(!empty($current_user) && !empty($draft) && $draft->commish_id == $current_user->id) { |
37
|
|
|
return true; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if(!empty($current_user) && $this->app['phpdraft.LoginUserService']->CurrentUserIsAdmin($current_user)) { |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function IsDraftValidForCreateAndUpdate(Draft $draft) { |
48
|
|
|
$valid = true; |
49
|
|
|
$errors = array(); |
50
|
|
|
$draft_sports = $this->app['phpdraft.DraftDataRepository']->GetSports(); |
51
|
|
|
$draft_styles = $this->app['phpdraft.DraftDataRepository']->GetStyles(); |
52
|
|
|
|
53
|
|
|
if(empty($draft->commish_id) |
54
|
|
|
|| empty($draft->draft_name) |
55
|
|
|
|| empty($draft->draft_sport) |
56
|
|
|
|| empty($draft->draft_style)) { |
57
|
|
|
$errors[] = "One or more missing fields."; |
58
|
|
|
$valid = false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if(!empty($draft->draft_password) && strlen($draft->draft_password) > 255) { |
62
|
|
|
$errors[] = "Password is above maximum length."; |
63
|
|
|
$valid = false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if(strlen($draft->draft_name) > 255) { |
67
|
|
|
$errors[] = "Draft name is above maximum length"; |
68
|
|
|
$valid = false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if(strlen($draft->draft_sport) < 3 || strlen($draft->draft_sport) > 4 || strlen($draft_sports[$draft->draft_sport]) == 0) { |
72
|
|
|
$errors[] = "Draft sport is an invalid value."; |
73
|
|
|
$valid = false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if(!array_key_exists($draft->draft_style, $draft_styles)) { |
77
|
|
|
|
78
|
|
|
$errors[] = "Draft style is an invalid value."; |
79
|
|
|
$valid = false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if(!$this->app['phpdraft.DraftRepository']->NameIsUnique($draft->draft_name, $draft->draft_id)) { |
83
|
|
|
$errors[] = "Draft name is already taken, please choose a different name."; |
84
|
|
|
$valid = false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if($draft->draft_rounds < 1 || $draft->draft_rounds > 30) { |
88
|
|
|
$errors[] = "Invalid number of draft rounds."; |
89
|
|
|
$valid = false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return new PhpDraftResponse($valid, $errors); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function IsDraftStatusValid(Draft $draft, $old_status) { |
96
|
|
|
$valid = true; |
97
|
|
|
$errors = array(); |
98
|
|
|
$draft_statuses = $this->app['phpdraft.DraftDataRepository']->GetStatuses(); |
99
|
|
|
|
100
|
|
|
if($old_status == "complete") { |
101
|
|
|
$valid = false; |
102
|
|
|
$errors[] = "The draft is completed, therefore its status cannot be changed."; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if($draft->draft_status == "complete") { |
106
|
|
|
$valid = false; |
107
|
|
|
$errors[] = "You cannot set the draft as completed manually."; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if(empty($draft->draft_status)) { |
111
|
|
|
$errors[] = "One or more missing fields."; |
112
|
|
|
$valid = false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if(!array_key_exists($draft->draft_status, $draft_statuses)) { |
116
|
|
|
$errors[] = "Draft status is an invalid value."; |
117
|
|
|
$valid = false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if($draft->draft_status == "in_progress" && !$this->app['phpdraft.ManagerRepository']->DraftHasManagers($draft->draft_id)) { |
121
|
|
|
$valid = false; |
122
|
|
|
$errors[] = "A draft must have at least 2 managers before it can begin."; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return new PhpDraftResponse($valid, $errors); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function IsDraftSettingUp(Draft $draft) { |
129
|
|
|
$valid = true; |
130
|
|
|
$errors = array(); |
131
|
|
|
$draft_statuses = $this->app['phpdraft.DraftDataRepository']->GetStatuses(); |
132
|
|
|
$current_status_text = strtolower($draft_statuses[$draft->draft_status]); |
133
|
|
|
$is_setting_up = $this->app['phpdraft.DraftService']->DraftSettingUp($draft); |
134
|
|
|
|
135
|
|
|
if(!$is_setting_up) { |
136
|
|
|
$valid = false; |
137
|
|
|
$errors[] = "Unable to work on draft #$draft->draft_id: draft is $current_status_text"; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return new PhpDraftResponse($valid, $errors); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function IsDraftSettingUpOrInProgress(Draft $draft) { |
144
|
|
|
$valid = true; |
145
|
|
|
$errors = array(); |
146
|
|
|
$draft_statuses = $this->app['phpdraft.DraftDataRepository']->GetStatuses(); |
147
|
|
|
$current_status_text = strtolower($draft_statuses[$draft->draft_status]); |
148
|
|
|
$is_setting_up = $this->app['phpdraft.DraftService']->DraftSettingUp($draft); |
149
|
|
|
$is_in_progress = $this->app['phpdraft.DraftService']->DraftInProgress($draft); |
150
|
|
|
|
151
|
|
|
if(!$is_setting_up && !$is_in_progress) { |
152
|
|
|
$valid = false; |
153
|
|
|
$errors[] = "Unable to work on draft #$draft->draft_id: draft is $current_status_text"; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return new PhpDraftResponse($valid, $errors); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function IsDraftInProgress(Draft $draft) { |
160
|
|
|
$valid = true; |
161
|
|
|
$errors = array(); |
162
|
|
|
$draft_statuses = $this->app['phpdraft.DraftDataRepository']->GetStatuses(); |
163
|
|
|
$current_status_text = strtolower($draft_statuses[$draft->draft_status]); |
164
|
|
|
$is_in_progress = $this->app['phpdraft.DraftService']->DraftInProgress($draft); |
165
|
|
|
|
166
|
|
|
if(!$is_in_progress) { |
167
|
|
|
$valid = false; |
168
|
|
|
$errors[] = "Unable to work on draft #$draft->draft_id: draft is $current_status_text"; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return new PhpDraftResponse($valid, $errors); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function IsDraftInProgressOrComplete(Draft $draft) { |
175
|
|
|
$valid = true; |
176
|
|
|
$errors = array(); |
177
|
|
|
$draft_statuses = $this->app['phpdraft.DraftDataRepository']->GetStatuses(); |
178
|
|
|
$current_status_text = strtolower($draft_statuses[$draft->draft_status]); |
179
|
|
|
$is_in_progress = $this->app['phpdraft.DraftService']->DraftInProgress($draft); |
180
|
|
|
$is_complete = $this->app['phpdraft.DraftService']->DraftComplete($draft); |
181
|
|
|
|
182
|
|
|
if(!$is_complete && !$is_in_progress) { |
183
|
|
|
$valid = false; |
184
|
|
|
$errors[] = "Unable to work on draft #$draft->draft_id: draft is $current_status_text"; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return new PhpDraftResponse($valid, $errors); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function IsDraftInProgressOrCompletedInLessThanTen(Draft $draft) { |
191
|
|
|
$valid = true; |
192
|
|
|
$errors = array(); |
193
|
|
|
$draft_statuses = $this->app['phpdraft.DraftDataRepository']->GetStatuses(); |
194
|
|
|
$current_status_text = strtolower($draft_statuses[$draft->draft_status]); |
195
|
|
|
$is_in_progress = $this->app['phpdraft.DraftService']->DraftInProgress($draft); |
196
|
|
|
$is_complete = $this->app['phpdraft.DraftService']->DraftComplete($draft); |
197
|
|
|
$isLessThanTenMinutes = false; |
198
|
|
|
|
199
|
|
|
if($is_complete) { |
200
|
|
|
$utc_timezone = new \DateTimeZone("UTC"); |
201
|
|
|
$now_utc = new \DateTime(null, $utc_timezone); |
202
|
|
|
$end_time = new \DateTime($draft->draft_end_time, $utc_timezone); |
203
|
|
|
$now_seconds = $now_utc->getTimestamp(); |
204
|
|
|
$end_seconds = $end_time->getTimestamp(); |
205
|
|
|
$seconds_elapsed = $end_seconds - $now_seconds; |
206
|
|
|
|
207
|
|
|
if($seconds_elapsed < 600) { |
208
|
|
|
$isLessThanTenMinutes = true; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if(!$is_complete && !$is_in_progress) { |
213
|
|
|
$valid = false; |
214
|
|
|
$errors[] = "Unable to work on draft #$draft->draft_id: draft is $current_status_text"; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if($is_complete && !$isLessThanTenMinutes) { |
218
|
|
|
$valid = false; |
219
|
|
|
$errors[] = "Unable to work on draft #$draft->draft_id: draft is complete and beyond the ten minute grace period."; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return new PhpDraftResponse($valid, $errors); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function IsDraftComplete(Draft $draft) { |
226
|
|
|
$valid = true; |
227
|
|
|
$errors = array(); |
228
|
|
|
$draft_statuses = $this->app['phpdraft.DraftDataRepository']->GetStatuses(); |
229
|
|
|
$current_status_text = strtolower($draft_statuses[$draft->draft_status]); |
230
|
|
|
$is_complete = $this->app['phpdraft.DraftService']->DraftComplete($draft); |
231
|
|
|
|
232
|
|
|
if(!$is_complete) { |
233
|
|
|
$valid = false; |
234
|
|
|
$errors[] = "Unable to work on draft #$draft->draft_id: draft is $current_status_text"; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return new PhpDraftResponse($valid, $errors); |
238
|
|
|
} |
239
|
|
|
} |