samedi 18 juillet 2015

Laravel controller method is getting messy

I have a controller with a method to process a booking. The method is getting quite large and after developing a good handful of apps I realized I'm doing something wrong because many of my controllers methods are large and messy.

For example

function processBooking($id) {
   // validate
   // if validaton fails return errors
   // if validation pass do the following
   $booking = getDetailsFromRepository($id)
   // check if client is paying with credits
      // process booking with credits
      // update client credids
      // check if any errors with stripe and return readable message to user
   // else process booking with credit card
      // set all date for Stripe::charge
      // capture charge
      // check if any errors with stripe and return readable message to user
   // if everything went well
      // save payment in db
      // save booking
      // return success message to user
}

most steps are functions from repository class. The whole method is about 300 lines.

After a bit of research I came across services so I created a ProcessBookingService.php class in which I chunck all ifs and checks down to multiple functions and I reduced the controller method down to 50 lines which makes it clean and readable.

The problem is that on the old method I would return a different message back to the user based on what went wrong ex: the card was declined, not enough points, etc.

Since most of the logic happens in the Service class, on the functions that I need to return a message back to the user I set a variable with a message and return that. Now the controllers gets lots of ifs ex:

$booking = $booking->process($data);

if($booking['success']) // redirect with success
else if($booking['card_declined']) // redirect with error
else if($booking['not_enough_points']) // redirect with error
else if($booking['stripe_request_error']) // redirect with error
else // unknown error on the server redirect with error

Is this correct from design and programming point of view? Any other suggestions would be much appreciated.

Thanks

Aucun commentaire:

Enregistrer un commentaire