| Total Complexity | 62 |
| Total Lines | 327 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 |
||
| 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) { |
||
| 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) { |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | private function validateNameLength($name, &$errors, &$valid) { |
||
| 335 | if (strlen($name) > 100) { |
||
| 336 | $errors[] = "Name is above maximum length"; |
||
| 338 | } |
||
| 339 | } |
||
| 340 | } |
||
| 341 |