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; |
||
244 | public function postSignup() |
||
245 | { |
||
246 | /************TEMP VARIABLE************/ |
||
247 | /* |
||
248 | * 0 - Disabled |
||
249 | * 1 - Enabled and no activation |
||
250 | * 2 - User activation |
||
251 | * 3 - Admin activation |
||
252 | */ |
||
253 | |||
254 | $signupStatus = \Base::getSetting('USER_REGISTRATION'); |
||
255 | /************TEMP VARIABLE************/ |
||
256 | |||
257 | $signupEnabled = $signupStatus != 0; |
||
258 | $userActivation = $signupStatus == 2; |
||
259 | $adminActivation = $signupStatus == 3; |
||
260 | |||
261 | if (!$signupEnabled) |
||
262 | return Redirect::to(URL::previous())->withInput()->with('error', Lang::get('base.auth.account.registration_disabled')); |
||
263 | |||
264 | $rules = array(); |
||
265 | |||
266 | // Declare the rules for the form validation |
||
267 | foreach(Sentinel::createModel()->getRegisterFields() as $fieldid => $field) |
||
268 | $rules[$fieldid] = $field['validator']; |
||
269 | |||
270 | $rules['g-recaptcha-response'] = 'required'; |
||
271 | |||
272 | // Create a new validator instance from our validation rules |
||
273 | $validator = Validator::make(Input::all(), $rules); |
||
274 | |||
275 | $err = false; |
||
276 | |||
277 | // If validation fails, we'll exit the operation now. |
||
278 | if ($validator->fails() || ($err = $this->captchaCheck()) == false) { |
||
279 | if ($err) |
||
280 | return Redirect::to(URL::previous())->withInput()->withErrors(['g-recaptcha-response' => Lang::get('base.captcha.error')]); |
||
281 | |||
282 | // Ooops.. something went wrong |
||
283 | return Redirect::to(URL::previous())->withInput()->withErrors($validator); |
||
284 | } |
||
285 | |||
286 | try { |
||
287 | $data = array(); |
||
288 | |||
289 | // Set the data to the user from the User class |
||
290 | foreach(Sentinel::createModel()->getRegisterFields() as $fieldid => $field) |
||
291 | $data[$fieldid] = Input::get($fieldid); |
||
292 | |||
293 | // Set the standard data to the user |
||
294 | $data['ip'] = Request::getClientIP(); |
||
295 | $data['status'] = 0; |
||
296 | $data['staff'] = 0; |
||
297 | if ($data['birthday'] != null) |
||
298 | $data['birthday'] = \Carbon\Carbon::createFromFormat('d/m/Y', $data['birthday']); |
||
299 | |||
300 | // Find the user if it exists and needs to be created |
||
301 | $user = Sentinel::getUserRepository()->findByCredentials(['email' => Input::get('email')]); |
||
302 | |||
303 | if ($user != null) |
||
304 | { |
||
305 | // Update the temporary user to the new one |
||
306 | if (Sentinel::validForUpdate($data, ['email' => Input::get('email')])) { |
||
307 | $testing = Sentinel::createModel()->getRegisterFields(); |
||
308 | $user = Sentinel::findById($user->id); |
||
309 | |||
310 | foreach($data as $fieldid => $field) |
||
311 | if (!isset($testing[$fieldid]) || $testing[$fieldid]['save'] == true) |
||
312 | $user[$fieldid] = $field; |
||
313 | |||
314 | $user['password'] = bcrypt($user['password']); |
||
315 | Sentinel::update($user, ['email' => Input::get('email')]); |
||
316 | } |
||
317 | else |
||
318 | return Redirect::to(URL::previous())->withInput()->with('error', Lang::get('base.auth.account.registration_failed')); |
||
319 | } |
||
320 | // Register the user |
||
321 | else |
||
322 | $user = Sentinel::register($data, false); |
||
323 | |||
324 | // If the user needs to activate the account send him an email |
||
325 | if ($userActivation) |
||
326 | { |
||
327 | $activation = Activation::create($user); |
||
328 | |||
329 | // Data to be used on the email view |
||
330 | $data = array( |
||
331 | 'user' => $user, |
||
332 | 'activationUrl' => URL::route('activate', [$user->id, $activation->code]), |
||
333 | ); |
||
334 | |||
335 | // Send the activation code through email |
||
336 | Mail::queue('emails.auth.register-activate', $data, function ($m) use ($user) { |
||
337 | $m->to($user->email, $user->first_name . ' ' . $user->last_name); |
||
338 | $m->subject(Lang::get('base.mails.welcome') . ' ' . $user->first_name); |
||
339 | }); |
||
340 | } |
||
341 | |||
342 | // If the administrator needs to activate the account send the user a warning saying that |
||
343 | if ($adminActivation) |
||
344 | { |
||
345 | Mail::queue('emails.auth.register-admin-activate', ['user' => $user], function ($m) use ($user) { |
||
346 | $m->to($user->email, $user->first_name . ' ' . $user->last_name); |
||
347 | $m->subject(Lang::get('base.mails.welcome') . ' ' . $user->first_name); |
||
348 | }); |
||
349 | } |
||
350 | |||
351 | // Log the user in |
||
352 | if (!$adminActivation && !$userActivation) |
||
353 | { |
||
354 | $activation = Activation::create($user); |
||
355 | |||
356 | if (Activation::complete($user, $activation->code)) { |
||
357 | Sentinel::login($user, false); |
||
358 | |||
359 | Mail::queue('emails.auth.activated', ['user' => $user], function ($m) use ($user) { |
||
360 | $m->to($user->email, $user->first_name . ' ' . $user->last_name); |
||
361 | $m->subject(Lang::get('base.mails.welcome') . ' ' . $user->first_name); |
||
362 | }); |
||
363 | } |
||
364 | } |
||
365 | |||
366 | Base::Log('New account registred for ' . $user->username . ' (' . $user->first_name . ' ' . $user->last_name . ') from IP ' . Request::ip() ); |
||
367 | |||
368 | // Redirect to the home page with success menu |
||
369 | 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')); |
||
370 | } catch (UserExistsException $e) { |
||
371 | $this->messageBag->add('email', Lang::get('base.auth.account.already_exists')); |
||
372 | } |
||
373 | |||
374 | Base::Log('New account registration attempt from IP ' . Request::ip() ); |
||
375 | |||
376 | // Ooops.. something went wrong |
||
377 | return Redirect::back()->withInput()->withErrors($this->messageBag); |
||
378 | } |
||
379 | |||
475 |
Adding a
@return
annotation 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.