I spent a lot of time doing Go Horse Extreme programming, but now I want to be a better person.
How can I make this method smaller, better, more OOP?
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function store()
{
if (!User::is_valid(Input::all()))
{
Flash::error(User::$errors->first());
return Redirect::back()->withInput();
}
$existing_user = User::where('email', Input::get('email'))->first();
if (is_null($existing_user)) // The user does not exists.
{
// Prepare user data
$user_input = Input::only(
'firstName',
'lastName',
'email',
'password');
$confirmation_token = str_random(60);
$user_data = array_merge($user_input, ['confirmation_token' => $confirmation_token]);
// Store user
$new_user = User::create($user_data);
// Prepare email
$data['name'] = Input::get('firstName') . ' ' . Input::get('lastName');
$data['confirmation_token'] = $confirmation_token;
$mail_subject = 'Confirmação de e-mail';
// Send email
Mail::send('emails.auth.email_confirmation', $data, function ($message) use ($mail_subject)
{
$message->to(Input::get('email'))->subject($mail_subject);
});
// Log the new user in
Auth::login($new_user);
$flash_message = sprintf(
'Enviamos um email para <strong>%s</strong>. Confirme sua identidade',
Input::get('email')
);
Flash::warning($flash_message);
return Redirect::route('admin.dashboard');
} else
{
if ($existing_user->is_active())
{
//Try to validate
$stored = $existing_user->password;
$typed = Input::get('password');
$match = Hash::check($typed, $stored);
if (!$match)
{
Flash::warning(
'Você já possui uma conta.
Faça login para continuar.'
);
return Redirect::route('login');
} else
{
Auth::login($existing_user);
Flash::message(
'Você já possui uma conta.
Bem vindo de volta!'
);
return Redirect::route('admin.dashboard');
}
} else
{
Auth::login($existing_user);
$flash_message = sprintf(
'Enviamos um email para <strong>%s</strong>. Confirme sua identidade [Reenviar]',
$existing_user->email
);
Flash::warning($flash_message);
return Redirect::route('admin.dashboard');
}
}
}
It just handles user registration. Everything seems to be working when I run my Behat tests, but I feel (deep in my soul) that things could be a lot better.