jeudi 6 février 2020

PHP - Laravel - Best way to manage conditional rules

I have a app using Laravel framework and there are some conditional rules that I dont know what is the best way to code and maintain.

Use case: conditionally apply promotion code

  • promo code can be applied within specific date or date range
  • promo code can be applied on order >= $100
  • promo code can be applied for specific item
  • ...

Basic solution is to write multiple IF ELSE statements to check 1 by 1. For example:

if ($promo->specific_date) {

} 
elseif ($promo->date_range >= 'date' && $promo->specific_date <= 'date') {

}

if ($totalAmount < 100) {
    // Dont allow
}

if (! $promo->allowed_items) {
    // Dont allow
}

// More conditions ...

I can see that code will be problematic in testing and maintaining.

So Im wondering if there is a better way to handle this?

Thanks,

Aucun commentaire:

Enregistrer un commentaire