Refactor a lot of conditionals

Posted 1 year ago by luddinus

Hi.

How would yo refactor this?

(User wants to apply to an offer)

// the code is in a Controller at the moment...

public function store($offerId)
{
   // validate the request etc.

   $offer = Offer::find($offerId);

   $errors = [];
   if ($offer->hasExpired()) {
      $errors[] = 'offer_expired';
   }

   if ($offer->isNotVerified()){
      $errors[] = 'offer_not_verified';
   }


   // if... if...

   if (count($errors)) {
      throw new CantApplyException($user, $offer, $errors);
   }

   return view('some view');

}

Please sign in or create an account to participate in this conversation.