DraftValidator::IsDraftSettingUp()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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