I have the following function which handles requests. It has more than 130 lines
public function run() {
$objectRequests = json_decode(file_get_contents("php://input"));
if (isset($objectRequests)) {
$objectResponses = array();
foreach ($objectRequests as $objectRequest) {
$objectResponse = new stdClass();
$objectResponse->tid = $objectRequest->tid;
try {
switch($objectRequest->facadeFn) {
// feststellen ob ein Benutzer angemeldet ist
case 'idcardcreator.checkLogin':
$objectResponse->data = new stdClass();
if (isset($this->sUsername)) {
$objectResponse->data = array(
'username' => $this->sUsername
);
} else {
$objectResponse->data = array(
'username' => false
);
}
break;
// Liste mit Benutzernamen abrufen
case 'idcardcreator.list':
$objectResponse->rows = new stdClass();
$arrayReturn = $this->_searchADUser(null);
if ($arrayReturn instanceof Exception || $arrayReturn instanceof Error) {
$this->_writeLog($arrayReturn->getMessage());
$objectResponse->errorMsg = $arrayReturn->getMessage();
} else {
$objectResponse->rows = $arrayReturn;
}
break;
// Formulardaten für AD-Editor holen
case 'idcardcreator.loadEdit':
$objectResponse->formData = new stdClass();
$arrayReturn = $this->_searchADUser($objectRequest->data);
if ($arrayReturn instanceof Exception || $arrayReturn instanceof Error) {
$this->_writeLog($arrayReturn->getMessage());
$objectResponse->errorMsg = $arrayReturn->getMessage();
} else {
$objectResponse->formData = array(
'name' => $arrayReturn[0]['Vorname'] . ' ' . $arrayReturn[0]['Name'],
'userid' => $arrayReturn[0]['ID'] !== '--' ? $arrayReturn[0]['ID'] : '',
'validity' => $arrayReturn[0]['Gültigkeit'] !== '--' ? $arrayReturn[0]['Gültigkeit'] : '',
'position' => $arrayReturn[0]['Funktion'] !== '--' ? $arrayReturn[0]['Funktion'] : ''
);
}
break;
// Einloggen
case 'idcardcreator.login':
$objectResponse->data = new stdClass();
$arrayReturn = $this->_loginUser($objectRequest->data->formData);
if ($arrayReturn instanceof Exception || $arrayReturn instanceof Error) {
$this->_writeLog($arrayReturn->getMessage());
if ($arrayReturn->getCode() === 123) {
$objectResponse->errorMsg = $arrayReturn->getMessage();
} else {
$objectResponse->errorMsg = 'Anmeldung fehlgeschlagen.';
}
} else {
$objectResponse->data = array(
'success' => 'true'
);
// Benutzername in Session speichern
$_SESSION['username'] = $arrayReturn['username'];
}
break;
// Ausloggen
case 'idcardcreator.logout':
session_destroy();
require_once 'IDCardCreator_ImageManipulator.php';
$img = new IDCardCreator_ImageManipulator();
$img->deleteImgs();
break;
// Benutzer im ActiveDirectory suchen
case 'idcardcreator.search':
$objectResponse->rows = new stdClass();
$arrayReturn = $this->_searchADUser($objectRequest->data);
if ($arrayReturn instanceof Exception || $arrayReturn instanceof Error) {
$this->_writeLog($arrayReturn->getMessage());
$objectResponse->errorMsg = $arrayReturn->getMessage();
} else {
$objectResponse->rows = $arrayReturn;
}
break;
// Active Directory benutzer updaten
case 'idcardcreator.update':
$objectResponse->data = new stdClass();
$arrayReturn = $this->_updateADUser($objectRequest->data->formData);
if ($arrayReturn instanceof Exception || $arrayReturn instanceof Error) {
$this->_writeLog($arrayReturn->getMessage());
$objectResponse->errorMsg = $arrayReturn->getMessage();
} else {
$objectResponse->data = array(
'success' => 'true'
);
}
break;
default:
$objectResponse->errorMsg = 'Aktion nicht gefunden!';
}
} catch (Exception $ex) {
$this->_writeLog($ex->getMessage());
$objectResponse->errorMsg = $ex->getMessage();
}
$objectResponses[] = $objectResponse;
}
// Antwort ausgeben
print(json_encode($objectResponses));
} else {
echo file_get_contents('template/main.html');
}
}
I would like to reduce the size of this function to increase readability but I'm not sure how to achieve that. I thought of moving the code of each "case" to seperate functions. However my class allready contains 13 functions and I'm afraid it would get to crowded if a added another ~7 functions.
$objectResponse->data = ['username' => $this->sUsername ?? false];which will reduce the first case by 10 lines \$\endgroup\$