Can you please provide some improvement advice for the following function? It is working OK now, but as I have zero experience with PHP I guess there is a lot of room for improvement.
function addNewUser($ES_Client, $username, $password, $fullname, $email, $function, $department, $access){
auditLog($ES_Client, "Creating new user: ".$username.", ".$fullname.", ".$email.", ".$function.", ".$department.", ".$access);
$checkIfUserExists = getUserDetails($ES_Client,$username)['hits']['total'];
if($checkIfUserExists != 0){
auditLog($ES_Client, "User ".$username." already exists! Operation aborted.");
return -1;
} else {
$hashed_password = crypt($password);
$userAddQuery= [
'index' => 'system',
'type' => 'users',
'body' => [
'username' => $username,
'password' => $hashed_password,
'fullname' => $fullname,
'email' => $email,
'function' => $function,
'department' => $department,
'access' => $access,
'lastlogin' => round(microtime(true) * 1000)
]
];
try{
$userAddResponse = $ES_Client->index($userAddQuery);
if($userAddResponse['created'] == 1)
{
auditLog($ES_Client, "User ".$username." added successfully!");
return 0;
}
else{
auditLog($ES_Client, "Error adding user ".$username."! Did not receive acknowledgement from elastic.");
return -1;
}
} catch(Exception $e) {
auditLog($ES_Client, "Error adding user ".$username."! ".$e->getMessage());
return -1;
}
}
}