| Conditions | 21 |
| Paths | 1991 |
| Total Lines | 135 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php namespace jlourenco\base\Controllers; |
||
| 225 | public function postSignup() |
||
| 226 | {
|
||
| 227 | /************TEMP VARIABLE************/ |
||
| 228 | /* |
||
| 229 | * 0 - Disabled |
||
| 230 | * 1 - Enabled and no activation |
||
| 231 | * 2 - User activation |
||
| 232 | * 3 - Admin activation |
||
| 233 | */ |
||
| 234 | |||
| 235 | $signupStatus = \Base::getSetting('USER_REGISTRATION');
|
||
| 236 | /************TEMP VARIABLE************/ |
||
| 237 | |||
| 238 | $signupEnabled = $signupStatus != 0; |
||
| 239 | $userActivation = $signupStatus == 2; |
||
| 240 | $adminActivation = $signupStatus == 3; |
||
| 241 | |||
| 242 | if (!$signupEnabled) |
||
| 243 | return Redirect::to(URL::previous())->withInput()->with('error', Lang::get('base.auth.account.registration_disabled'));
|
||
| 244 | |||
| 245 | $rules = array(); |
||
| 246 | |||
| 247 | // Declare the rules for the form validation |
||
| 248 | foreach(Sentinel::createModel()->getRegisterFields() as $fieldid => $field) |
||
| 249 | $rules[$fieldid] = $field['validator']; |
||
| 250 | |||
| 251 | $rules['g-recaptcha-response'] = 'required'; |
||
| 252 | |||
| 253 | // Create a new validator instance from our validation rules |
||
| 254 | $validator = Validator::make(Input::all(), $rules); |
||
| 255 | |||
| 256 | $err = false; |
||
| 257 | |||
| 258 | // If validation fails, we'll exit the operation now. |
||
| 259 | if ($validator->fails() || ($err = $this->captchaCheck()) == false) {
|
||
| 260 | if ($err) |
||
| 261 | return Redirect::to(URL::previous())->withInput()->withErrors(['g-recaptcha-response' => Lang::get('base.captcha.error')]);
|
||
| 262 | |||
| 263 | // Ooops.. something went wrong |
||
| 264 | return Redirect::to(URL::previous())->withInput()->withErrors($validator); |
||
| 265 | } |
||
| 266 | |||
| 267 | try {
|
||
| 268 | $data = array(); |
||
| 269 | |||
| 270 | // Set the data to the user from the User class |
||
| 271 | foreach(Sentinel::createModel()->getRegisterFields() as $fieldid => $field) |
||
| 272 | $data[$fieldid] = Input::get($fieldid); |
||
| 273 | |||
| 274 | // Set the standard data to the user |
||
| 275 | $data['ip'] = Request::getClientIP(); |
||
| 276 | $data['status'] = 0; |
||
| 277 | $data['staff'] = 0; |
||
| 278 | if ($data['birthday'] != null) |
||
| 279 | $data['birthday'] = \Carbon\Carbon::createFromFormat('d/m/Y', $data['birthday']);
|
||
| 280 | |||
| 281 | // Find the user if it exists and needs to be created |
||
| 282 | $user = Sentinel::getUserRepository()->findByCredentials(['email' => Input::get('email')]);
|
||
| 283 | |||
| 284 | if ($user != null) |
||
| 285 | {
|
||
| 286 | // Update the temporary user to the new one |
||
| 287 | if (Sentinel::validForUpdate($data, ['email' => Input::get('email')])) {
|
||
| 288 | $testing = Sentinel::createModel()->getRegisterFields(); |
||
| 289 | $user = Sentinel::findById($user->id); |
||
| 290 | |||
| 291 | foreach($data as $fieldid => $field) |
||
| 292 | if (!isset($testing[$fieldid]) || $testing[$fieldid]['save'] == true) |
||
| 293 | $user[$fieldid] = $field; |
||
| 294 | |||
| 295 | $user['password'] = bcrypt($user['password']); |
||
| 296 | Sentinel::update($user, ['email' => Input::get('email')]);
|
||
| 297 | } |
||
| 298 | else |
||
| 299 | return Redirect::to(URL::previous())->withInput()->with('error', Lang::get('base.auth.account.registration_failed'));
|
||
| 300 | } |
||
| 301 | // Register the user |
||
| 302 | else |
||
| 303 | $user = Sentinel::register($data, false); |
||
| 304 | |||
| 305 | // If the user needs to activate the account send him an email |
||
| 306 | if ($userActivation) |
||
| 307 | {
|
||
| 308 | $activation = Activation::create($user); |
||
| 309 | |||
| 310 | // Data to be used on the email view |
||
| 311 | $data = array( |
||
| 312 | 'user' => $user, |
||
| 313 | 'activationUrl' => URL::route('activate', [$user->id, $activation->code]),
|
||
| 314 | ); |
||
| 315 | |||
| 316 | // Send the activation code through email |
||
| 317 | Mail::queue('emails.auth.register-activate', $data, function ($m) use ($user) {
|
||
| 318 | $m->to($user->email, $user->first_name . ' ' . $user->last_name); |
||
| 319 | $m->subject(Lang::get('base.mails.welcome') . ' ' . $user->first_name);
|
||
| 320 | }); |
||
| 321 | } |
||
| 322 | |||
| 323 | // If the administrator needs to activate the account send the user a warning saying that |
||
| 324 | if ($adminActivation) |
||
| 325 | {
|
||
| 326 | Mail::queue('emails.auth.register-admin-activate', ['user' => $user], function ($m) use ($user) {
|
||
| 327 | $m->to($user->email, $user->first_name . ' ' . $user->last_name); |
||
| 328 | $m->subject(Lang::get('base.mails.welcome') . ' ' . $user->first_name);
|
||
| 329 | }); |
||
| 330 | } |
||
| 331 | |||
| 332 | // Log the user in |
||
| 333 | if (!$adminActivation && !$userActivation) |
||
| 334 | {
|
||
| 335 | $activation = Activation::create($user); |
||
| 336 | |||
| 337 | if (Activation::complete($user, $activation->code)) {
|
||
| 338 | Sentinel::login($user, false); |
||
| 339 | |||
| 340 | Mail::queue('emails.auth.activated', ['user' => $user], function ($m) use ($user) {
|
||
| 341 | $m->to($user->email, $user->first_name . ' ' . $user->last_name); |
||
| 342 | $m->subject(Lang::get('base.mails.welcome') . ' ' . $user->first_name);
|
||
| 343 | }); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | Base::Log('New account registred for ' . $user->username . ' (' . $user->first_name . ' ' . $user->last_name . ') from IP ' . Request::ip() );
|
||
| 348 | |||
| 349 | // Redirect to the home page with success menu |
||
| 350 | return Redirect::to("login")->with('success', Lang::get('base.auth.signup.success') . $adminActivation ? Lang::get('base.auth.signup.admin') : $userActivation ? Lang::get('base.auth.signup.self') : Lang::get('base.auth.signup.ready'));
|
||
| 351 | } catch (UserExistsException $e) {
|
||
| 352 | $this->messageBag->add('email', Lang::get('base.auth.account.already_exists'));
|
||
| 353 | } |
||
| 354 | |||
| 355 | Base::Log('New account registration attempt from IP ' . Request::ip() );
|
||
| 356 | |||
| 357 | // Ooops.. something went wrong |
||
| 358 | return Redirect::back()->withInput()->withErrors($this->messageBag); |
||
| 359 | } |
||
| 360 | |||
| 456 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.