validation, redirection, user creation, view preparation and email notifications ! (That’s way too much code for a simple method. 60 lines in fact) public function post_index() { // Get the input $input = Input::all(); ! // Set the rules $rules = array( 'email' => 'required|email', ); ! // Make a validation object $validation = Validator::make($input, $rules); ! // Has the validation ran? if ($validation->fails()) // It failed, deal with it { return Redirect::to('invite')->with_input()->with_errors($validation); } else // Invite the user & send an email { // Check if the email is in the system already if ($invite = Invite::where('email', '=', Input::get('email'))->first()) { $invite->invite_code = Str::random(48); $invite->save(); } else { $invite = Invite::create(array( 'email' => Input::get('email'), 'used' => 0, 'invite_code' => Str::random(48) )); } ! // Get the email view $body = View::make('emails.user_invite_confirmation') ->with('invitee', Auth::user()) ->with('invite_link', URL::to('beta/register/?invite_code='.$invite->invite_code.'&email='.$invite->email)) ->render(); ! // Notify the person of their new invite $message = Message::to($invite->email) ->from('[email protected]', 'RigShare') ->subject('Your friend '.Auth::user()->full_name.' invited you to RigShare!') ->body($body) ->send(); ! if ($message->was_sent()) { Log::info('Email was sent.'); } ! if ( $invite == false ) { die('didn\'t work'); } ! return Redirect::to('beta/create_invite')->with('success', true); } }
another controller? Code duplication is bad and hard-to-maintain // Set the rules $rules = array( 'email' => 'required|email', ); ! // Make a validation object $validation = Validator::make($input, $rules); ! // Has the validation ran? if ($validation->fails()) // It failed, deal with it { return Redirect::to('invite')->withInput()->withErrors($validation); } else {} // Invite the user, send an email, and redirect !
validation code. The uniqueness check is built-in to Laravel’s validator. Again, what if we needed to create users elsewhere? // Check if the email is in the system already if ($invite = Invite::where('email', '=', Input::get('email'))->first()) { $invite->invite_code = Str::random(48); $invite->save(); } else { $invite = Invite::create(array( 'email' => Input::get('email'), 'used' => 0, 'invite_code' => Str::random(48) )); }
that nasty invite link? Could we store that in a configuration file? // Get the email view $body = View::make('emails.user_invite_confirmation') ->with('invitee', Auth::user()) ->with('invite_link', URL::to('beta/register/?invite_code='. $invite->invite_code.'&email='.$invite->email)) ->render();
an email is sent? NO! It just needs to handle HTTP Traffic. Also, what about those hardcoded emails? Why do you hate me, me? // Notify the person of their new invite $message = Message::to($invite->email) ->from('[email protected]', 'RigShare') ->subject('Your friend '.Auth::user()->full_name.' invited you to RigShare!') ->body($body) ->send();
Two, count em, two checks for things that would have Exceptions thrown. if ($message->wasSent()) { Log::info('Email was sent.'); } ! if ( $invite == false ) { die('didn\'t work'); } ! return Redirect::to('beta/create_invite')->with('success', true);
$input = Input::all(); $validation = $this->validator->validate($input); if ($validation->passes()) { // Save the invite to the database $invite = $this->invite->create($input['email']); ! // Show a success message to the user return View::make('invites.signup_thank_you'); } return Redirect::to('/')->withErrors($validation->errors())->withInput(); }
! class DbInviteRepository implements InviteRepositoryInterface { /** * Create an invite in the database */ public function create($email) { Invite::create(['email' => $email]); ! // Fire off the invite creation event Event::fire('invite.create', [$email]); } }
{ public function __construct(InviteMailer $mailer) { $this->mailer = $mailer; } public function onInviteCreate($email) { $this->mailer->welcome($email); $this->mailer->notifyAdmin($email); } public function subscribe($events) { $events->listen('invite.create', 'LDFW\Handlers \InviteEventHandler@onInviteCreate'); } }