jeudi 14 mai 2020

How to prevent overwhelming in checking multiple methods return results and get Clean Code - PHP

How to Implement Logic (Class - Interface - Methods , ...) and working on their results. for example : to Check Request Validation

assume each user's request has 3 parameters
1-access_key -> Required
2-paraA -> not Required -> Default(x)
3-paraB -> not Required
-------------------------------------------------------------------------------
access_key -> when user register in website it will generate in DB

parameter A(paraA) -> if user role is 1 can not change and only will be (x) but if user role is 2 or 
grader than 2 it will be [a or b or c , ... or z]

parameter B(paraB) -> if exists in user's request it will be chack that is valid parameter or not => 
valid parameter [a,b,c,d,...,z]

at first we will check is user access_key valid or not

1)i want to check is access_key valid if valid then continue else return json result -> {'status' => 'error', 'code' => 201, 'message' => 'access_key is not valid'} and for check role and parameters ,...

i get overwhelmed with multiple if-then-else statement for example

checkAccessKey($access_key) {
is $access_key exist in DB ?
    -if Yes(true) -> return True and check other methods
    -else No(false)  -> return json Result & don't continue and don't check other methods
     }

checkParaA($paraA) {
is user($access_key)->role == 1 && $paraA == 'x' ?
    -if Yes(true) -> paraA = 'x' 
    -if No(false) -> return json Result && don'y continue and don't check  other methods
     }

     .
     .
     .

     at end i should check multi if-then-else
         if(checkAccessKey($access_key) {
            ...
              if(checkParaA($paraA) {
                 ...
                  .   
                  .
                  .
     /*   MULTIPLE IF-THEN-ELSE  */

when i implement this scenario i overwhelming with multi if-then-else statements what can i do? and how i can develop this scenario. how i can implement clean code and with good principle and best practice

Aucun commentaire:

Enregistrer un commentaire