LoginUserValidator   F
last analyzed

Complexity

Total Complexity 62

Size/Duplication

Total Lines 327
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 62
eloc 178
dl 0
loc 327
rs 3.44
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
B isRegistrationUserValid() 0 35 7
A __construct() 0 2 1
A validatePasswordLength() 0 9 3
A validateEmailAddress() 0 4 3
A isInviteNewUserValid() 0 30 5
A areLoginCredentialsValid() 0 9 1
A IsForgottenPasswordUserValid() 0 9 1
B IsAdminUserUpdateValid() 0 35 7
A validateNameLength() 0 4 2
A validateEmailExists() 0 4 2
A IsVerificationValid() 0 20 3
A validatePasswordsMatch() 0 4 2
B IsResetPasswordRequestValid() 0 36 7
A IsResetPasswordTokenValid() 0 25 5
A validateUniqueEmail() 0 4 2
B IsUserProfileUpdateValid() 0 59 11

How to fix   Complexity   

Complex Class

Complex classes like LoginUserValidator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use LoginUserValidator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace PhpDraft\Domain\Validators;
4
5
use \Silex\Application;
6
use Symfony\Component\HttpFoundation\Request;
7
use PhpDraft\Domain\Entities\LoginUser;
8
use PhpDraft\Domain\Models\PhpDraftResponse;
9
use Egulias\EmailValidator\Validation\RFCValidation;
10
11
class LoginUserValidator {
12
  private $app;
13
14
  public function __construct(Application $app) {
15
    $this->app = $app;
16
  }
17
18
  public function isRegistrationUserValid(Request $request) {
19
    $valid = true;
20
    $errors = array();
21
22
    $password = $request->get('_password');
23
    $confirmPassword = $request->get('_confirmPassword');
24
    $emailAddress = $request->get('_email');
25
    $name = $request->get('_name');
26
    $recaptcha = $request->get('_recaptcha');
27
28
    if (empty($password)
29
      || empty($confirmPassword)
30
      || empty($emailAddress)
31
      || empty($name)
32
      || empty($recaptcha)) {
33
      $errors[] = "One or more missing fields.";
34
      $valid = false;
35
    }
36
37
    $this->validatePasswordsMatch($password, $confirmPassword, $errors, $valid);
38
39
    $this->validatePasswordLength($password, $errors, $valid);
40
41
    $this->validateNameLength($name, $errors, $valid);
42
43
    $this->validateEmailAddress($emailAddress, $errors, $valid);
44
45
    if (!$this->app['phpdraft.LoginUserRepository']->NameIsUnique($name)) {
46
      $errors[] = "Name already taken.";
47
      $valid = false;
48
    }
49
50
    $this->validateUniqueEmail($emailAddress, $errors, $valid);
51
52
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
53
  }
54
55
  public function isInviteNewUserValid(LoginUser $user, $message) {
56
    $valid = true;
57
    $errors = array();
58
59
    $emailAddress = $user->email;
60
    $name = $user->name;
61
62
    if (empty($emailAddress)
63
      || empty($name)) {
64
      $errors[] = "One or more missing fields.";
65
      $valid = false;
66
    }
67
68
    $this->validateNameLength($name, $errors, $valid);
69
70
    $this->validateEmailAddress($emailAddress, $errors, $valid);
71
72
    if (strlen($message) > 255) {
73
      $errors[] = "Message too long";
74
      $valid = false;
75
    }
76
77
    if (!$this->app['phpdraft.LoginUserRepository']->NameIsUnique($name)) {
78
      $errors[] = "Name already taken.";
79
      $valid = false;
80
    }
81
82
    $this->validateUniqueEmail($emailAddress, $errors, $valid);
83
84
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
85
  }
86
87
  public function IsVerificationValid(Request $request) {
88
    $valid = true;
89
    $errors = array();
90
91
    $emailAddress = $request->get('_email');
92
    $verificationToken = $this->app['phpdraft.SaltService']->UrlDecodeSalt($request->get('_verificationToken'));
93
94
    if (strlen($verificationToken) != 16) {
95
      $errors[] = "Verification token invalid.";
96
      $valid = false;
97
    }
98
99
    $this->validateEmailAddress($emailAddress, $errors, $valid);
100
101
    if (!$this->app['phpdraft.LoginUserRepository']->VerificationMatches($emailAddress, $verificationToken)) {
102
      $errors[] = "Verification token invalid.";
103
      $valid = false;
104
    }
105
106
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
107
  }
108
109
  public function areLoginCredentialsValid($emailAddress, $password) {
110
    $valid = true;
111
    $errors = array();
112
113
    $this->validateEmailAddress($emailAddress, $errors, $valid);
114
115
    $this->validatePasswordLength($password, $errors, $valid);
116
117
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
118
  }
119
120
  public function IsForgottenPasswordUserValid(Request $request) {
121
    $valid = true;
122
    $errors = array();
123
124
    $emailAddress = $request->get('_email');
125
126
    $this->validateEmailExists($emailAddress, $errors, $valid);
127
128
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
129
  }
130
131
  public function IsResetPasswordTokenValid($emailAddress, $verificationToken) {
132
    $valid = true;
133
    $errors = array();
134
135
    if (empty($emailAddress)
136
      || empty($verificationToken)) {
137
      $errors[] = "One or more missing fields";
138
      $valid = false;
139
    }
140
141
    if (strlen($verificationToken) != 16) {
142
      $errors[] = "Verification token invalid.";
143
      $valid = false;
144
    }
145
146
    if (!$this->app['phpdraft.LoginUserRepository']->VerificationMatches($emailAddress, $verificationToken)) {
147
      $errors[] = "Verification token invalid.";
148
      $valid = false;
149
    }
150
151
    $this->validateEmailAddress($emailAddress, $errors, $valid);
152
153
    $this->validateEmailExists($emailAddress, $errors, $valid);
154
155
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
156
  }
157
158
  public function IsResetPasswordRequestValid(Request $request) {
159
    $valid = true;
160
    $errors = array();
161
162
    $emailAddress = $request->get('_email');
163
    $password = $request->get('_password');
164
    $confirmPassword = $request->get('_confirmPassword');
165
    $verificationToken = $this->app['phpdraft.SaltService']->UrlDecodeSalt($request->get('_verificationToken'));
166
167
    if (empty($emailAddress)
168
      || empty($password)
169
      || empty($confirmPassword)
170
      || empty($verificationToken)) {
171
      $errors[] = "One or more missing fields.";
172
      $valid = false;
173
    }
174
175
    if (strlen($verificationToken) != 16) {
176
      $errors[] = "Verification token invalid.";
177
      $valid = false;
178
    }
179
180
    if (!$this->app['phpdraft.LoginUserRepository']->VerificationMatches($emailAddress, $verificationToken)) {
181
      $errors[] = "Verification token invalid.";
182
      $valid = false;
183
    }
184
185
    $this->validateEmailAddress($emailAddress, $errors, $valid);
186
187
    $this->validatePasswordsMatch($password, $confirmPassword, $errors, $valid);
188
189
    $this->validatePasswordLength($password, $errors, $valid);
190
191
    $this->validateEmailExists($emailAddress, $errors, $valid);
192
193
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
194
  }
195
196
  public function IsUserProfileUpdateValid(Request $request) {
197
    $valid = true;
198
    $errors = array();
199
200
    $emailAddress = strtolower($request->get('_email'));
201
    $name = $request->get('_name');
202
    $password = $request->get('_password');
203
    $newPassword = $request->get('_newPassword');
204
    $newConfirmedPassword = $request->get('_newConfirmedPassword');
205
206
    $currentUser = $this->app['phpdraft.LoginUserService']->GetCurrentUser();
207
208
    if (empty($currentUser) || $currentUser == null) {
209
      $valid = false;
210
      $errors[] = "Invalid user.";
211
212
      //Because we need to compare new & old values, we need a valid user record to proceed with validation.
213
      return $this->app['phpdraft.ResponseFactory']($valid, $errors);
214
    }
215
216
    //Password required to make any changes
217
    if (empty($password) || !$this->app['security.encoder.digest']->isPasswordValid($currentUser->password, $password, $currentUser->salt)) {
218
      $errors[] = "Incorrect password entered.";
219
      $valid = false;
220
    }
221
222
    //Need to verify new email
223
    if (empty($emailAddress)) {
224
      $errors[] = "Email address is missing.";
225
      $valid = false;
226
    } else if (!$this->app['phpdraft.StringsEqual']($emailAddress, $currentUser->email)) {
227
      $this->validateEmailAddress($emailAddress, $errors, $valid);
228
229
      $this->validateUniqueEmail($emailAddress, $errors, $valid);
230
    }
231
232
    //Need to verify new password, ensure old password is correct
233
    if (!empty($newPassword)) {
234
      $this->validatePasswordLength($newPassword, $errors, $valid);
235
236
      $this->validatePasswordsMatch($newPassword, $newConfirmedPassword, $errors, $valid);
237
    }
238
239
    //If the name has changed, ensure the new one is valid and unique
240
    if ($currentUser->name != $name) {
241
      if (empty($name)) {
242
        $errors[] = "Name is required.";
243
        $valid = false;
244
      }
245
246
      $this->validateNameLength($name, $errors, $valid);
247
248
      if (!$this->app['phpdraft.LoginUserRepository']->NameIsUnique($name)) {
249
        $errors[] = "Name already taken.";
250
        $valid = false;
251
      }
252
    }
253
254
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
255
  }
256
257
  public function IsAdminUserUpdateValid(LoginUser $user) {
258
    $valid = true;
259
    $errors = array();
260
261
    $loadedUser = $this->app['phpdraft.LoginUserRepository']->LoadById($user->id);
262
263
    if ($user->id == 0 || empty($loadedUser)) {
264
      $valid = false;
265
      $errors[] = "Invalid user.";
266
267
      //Because we need to compare new & old values, we need a valid user record to proceed with vaidation.
268
      return $this->app['phpdraft.ResponseFactory']($valid, $errors);
269
    }
270
271
    //Need to verify new email
272
    if (empty($user->email)) {
273
      $errors[] = "Email address is missing.";
274
      $valid = false;
275
    } else if (!$this->app['phpdraft.StringsEqual']($user->email, $loadedUser->email)) {
276
      $this->validateEmailAddress($user->email, $errors, $valid);
277
278
      $this->validateUniqueEmail($user->email, $errors, $valid);
279
    }
280
281
    if (strlen($user->name) > 100) {
282
      $errors[] = "Name is above maximum length";
283
      $valid = false;
284
    }
285
286
    if (!$this->app['phpdraft.LoginUserRepository']->NameIsUnique($user->name, $user->id)) {
287
      $errors[] = "Name already taken.";
288
      $valid = false;
289
    }
290
291
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
292
  }
293
294
  private function validatePasswordsMatch($password1, $password2, &$errors, &$valid) {
295
    if (!$this->app['phpdraft.StringsEqual']($password1, $password2)) {
296
      $errors[] = "Password values do not match.";
297
      $valid = false;
298
    }
299
  }
300
301
  private function validateUniqueEmail($emailAddress, &$errors, &$valid) {
302
    if (!$this->app['phpdraft.LoginUserRepository']->EmailIsUnique($emailAddress)) {
303
      $errors[] = "Email already registered.";
304
      $valid = false;
305
    }
306
  }
307
308
  private function validateEmailExists($emailAddress, &$errors, &$valid) {
309
    if (!$this->app['phpdraft.LoginUserRepository']->EmailExists($emailAddress)) {
310
      $errors[] = "Email invalid.";
311
      $valid = false;
312
    }
313
  }
314
315
  private function validateEmailAddress($emailAddress, &$errors, &$valid) {
316
    if (!$this->app['phpdraft.EmailValidator']->isValid($emailAddress, new RFCValidation()) || strlen($emailAddress) > 255) {
317
      $errors[] = "Email invalid.";
318
      $valid = false;
319
    }
320
  }
321
322
  private function validatePasswordLength($password, &$errors, &$valid) {
323
    if (strlen($password) < 8) {
324
      $errors[] = "Password is below minimum length.";
325
      $valid = false;
326
    }
327
328
    if (strlen($password) > 255) {
329
      $errors[] = "Password is above maximum length.";
330
      $valid = false;
331
    }
332
  }
333
334
  private function validateNameLength($name, &$errors, &$valid) {
335
    if (strlen($name) > 100) {
336
      $errors[] = "Name is above maximum length";
337
      $valid = false;
338
    }
339
  }
340
}
341