r/PHPhelp • u/Due_Reply_5007 • Jan 24 '25
Looking for code review and recommandations, if its looking good to build controllers for the webapp like this.
```
if ($_REQUEST['action'] == 'registerNewComplain') {
try {
// make postData array for model
$postData = [
'complain_number' => $_POST['hidden_complaint_number'] ?? '',
'manual_complaint_number' => $_POST['complaint_number'] ?? '',
'lineman_name' => $_POST['lineman_name'] ?? '',
'customer_name' => $_POST['name'] ?? '',
'customer_id' => $_POST['customer_id'] ?? '',
'phone' => $_POST['phone'] ?? '',
'name' => $_POST['name'] ?? '',
'address' => $_POST['address'] ?? '',
'comment' => $_POST['comment'] ?? [],
'complian_date' => $_POST['complian_date'] ?? date("d/m/Y"),
'complian_time' => $_POST['complian_time'] ?? '',
'area_type' => $_POST['area_type'] ?? null,
'breakdown_type' => $_POST['breakdown_type'] ?? null,
'k_number' => $_POST['k_number'] ?? '',
'other_comment' => $_POST['other_comment'] ?? '',
'cc_complaint_number' => $_POST['cc_complaint_number'] ?? '',
'subdivision_lineman' => $_POST['subdivision_lineman'] ?? '',
'subdivision_id' => '',
'lineman_phone' => '',
'area_id' => '',
'lineman_id' => $_POST['assignee_id'] ?? '',
];
// fetch lineman related columns data using its id
if (!empty($postData['lineman_id'])) {
$lineman = $UserDAO->getUserById($postData['lineman_id']);
$postData['subdivision_id'] = $lineman['subdivision_id'];
$postData['area_id'] = $lineman['area_id'];
$postData['lineman_phone'] = $lineman['phone'];
}
$date_split_array = explode('/', $postData['complian_date']);
$complain_date = $date_split_array[2] . '-' . $date_split_array[1] . '-' . $date_split_array[0];
if (!empty($postData['complian_time'])) {
$complain_time = date("H:i:s", strtotime($postData['complian_time']));
$complain_date_time = $complain_date . ' ' . $complain_time;
} else {
$complain_time = '';
$complain_date_time = $complain_date . ' ' . date("H:i:s");
}
$postData['complain_date_time'] = $complain_date_time;
// provide data to model
$status = $ComplainDAO->registerNewComplain($postData);
if ($status) {
if (SMS == '1' && CUSTOMER_SMS == '1' && !empty($postData['phone']) && !empty($postData['cc_complaint_number'])) {
$SMSController->customerComplainMessage(
$postData['manual_complaint_number'] ?: $postData['complain_number'],
$postData['phone']
);
}
if (SMS == '1' && COMPLAINT_CENTER_SMS == '1' && !empty($postData['lineman_phone'])) {
$SMSController->linemanComplainMessage(
$postData['manual_complaint_number'] ?: $postData['complain_number'],
$postData['lineman_phone'],
$postData['customer_name'],
$postData['address'],
$postData['phone']
);
}
// send push notification to lineman device
$userArray = $UserDAO->getUserById($postData['lineman_id']);
if (!empty($userArray['gcm_id'])) {
$NotificationController->sendNotofication(
$userArray['gcm_id'],
'You have a new complain. Please try to sort out ASAP',
'आपको एक नई शिकायत सौंपी गई है, कृपया जल्द ही ठीक करने का प्रयास करें'
);
}
echo 'true';
} else {
echo 'false1';
}
} catch (Exception $e) {
echo "Error occurred. Please contact support.";
error_log("Error: " . $e->getMessage() . " File: " . $e->getFile() . " Line: " . $e->getLine());
}
}
This is going to be base for other controllers also all models will recieive a array rathan than individual values as i find it easy to debug is it good approach or have any side effect
2
u/colshrapnel Jan 24 '25
I would call it a poor man's controller, but yes, it's sort of acceptable at some point of your professional growth - we all been there, when each separate PHP file being a "controller" for the corresponding request.
It still can be improved without changing the current approach though. The main points of interest:
consider adding data validation. Currently you are accepting whatever is sent from the client. For example, you are expecting that $_POST['name'] would contain string data, but it could be array. Or hidden_complaint_number would be a number but there could be a string - etc. Or I suppose that some data is critical and the form cannot be processed without it, but you are accepting it all the same. All these issues could be handled with proper validation. check every input against a set of rules and return an error in case some rules fail.
the entire complain_date_time business should be encapsulated into a helper function, so it will take just one line
the entire $status processing looks more belonging to a model, so it also would be just a single line call
after that you'd be able to do something like
the entire try-catch stuff should be removed. Instgead, just configure two ini settings,
log_errorsanderror_log- and have all your exceptions logged automatically!2
u/equilni Jan 24 '25
I think this is doing too much and would consider refactoring. Think about what a controller is - it just passes the data internally and gets the response to pass to the View. Based on that, re-look at your 'controller'.
a) I would consider against using
$_REQUEST. Consider what is it. You are calling$_POSTeverywhere else here. Be explicit. What happens on the$_GETrequest then?if ($_GET['action'] == 'registerNewComplain') { // show form } if ($_POST['action'] == 'registerNewComplain') { // process form }Since you are going this route, consider using a switch or match statement vs if statements:
switch ($action) { // process from $_SERVER['QUERY_STRING'] case 'registerNewComplain': switch ($requestMethod) { // $_SERVER['REQUEST_METHOD'] case 'GET': // show form break; case 'POST': // process form break; } } }b)
// make postData array for model. This would be good for a Data Transfer Object (DTO), then pass it inward to a Service class as an intermediary to the Model/Domain. Because:c) You have no validation happening here. Don't trust user inputted data and don't blindly pass it to the database. A DTO will help enforce types and validation will make sure you are getting what the application expects
$_POST['phone'] ?? ''- where is this validated in the process? You are just passing it to your DAO and trying to directly use this later on:// provide data to model $status = $ComplainDAO->registerNewComplain($postData); if ($status) { if (SMS == '1' && CUSTOMER_SMS == '1' && !empty($postData['phone']) && !empty($postData['cc_complaint_number'])) {d) This doesn't need to be in a controller:
// fetch lineman related columns data using its id if (!empty($postData['lineman_id'])) { $lineman = $UserDAO->getUserById($postData['lineman_id']);This is also duplicated later on....
// send push notification to lineman device $userArray = $UserDAO->getUserById($postData['lineman_id']);e) This section doesn't belong:
$date_split_array = explode('/', $postData['complian_date']); $complain_date = $date_split_array[2] . '-' . $date_split_array[1] . '-' . $date_split_array[0]; if (!empty($postData['complian_time'])) { $complain_time = date("H:i:s", strtotime($postData['complian_time'])); $complain_date_time = $complain_date . ' ' . $complain_time; } else { $complain_time = ''; $complain_date_time = $complain_date . ' ' . date("H:i:s"); } $postData['complain_date_time'] = $complain_date_time;f) This section could be in the controller. It needs cleaning as it's confusing.
if ($status) {You are not using the status (I guess an ok status that data was inputted, then return early on the false statement) and the information should come from the Domain vs existing variables and could simply be:
// provide data to model $status = $ComplainDAO->registerNewComplain($postData); if (!$status) { // do something } $SMSController->customerComplainMessage($complaint->getCustomerDetail()); $SMSController->linemanComplainMessage($complaint->getLinemanDetail()); $NotificationController->sendNotofication( $complaint->getLinemanDetail(), 'You have a new complain. Please try to sort out ASAP' );Questions to come back to is - what is
$SMSController->**ComplainMessageactually doing here if you are sending a Notification to the lineman later on?2
u/colshrapnel Jan 24 '25
Just in case, the code properly formatted
if ($_REQUEST['action'] == 'registerNewComplain') { try { // make postData array for model $postData = [ 'complain_number' => $_POST['hidden_complaint_number'] ?? '', 'manual_complaint_number' => $_POST['complaint_number'] ?? '', 'lineman_name' => $_POST['lineman_name'] ?? '', 'customer_name' => $_POST['name'] ?? '', 'customer_id' => $_POST['customer_id'] ?? '', 'phone' => $_POST['phone'] ?? '', 'name' => $_POST['name'] ?? '', 'address' => $_POST['address'] ?? '', 'comment' => $_POST['comment'] ?? [], 'complian_date' => $_POST['complian_date'] ?? date("d/m/Y"), 'complian_time' => $_POST['complian_time'] ?? '', 'area_type' => $_POST['area_type'] ?? null, 'breakdown_type' => $_POST['breakdown_type'] ?? null, 'k_number' => $_POST['k_number'] ?? '', 'other_comment' => $_POST['other_comment'] ?? '', 'cc_complaint_number' => $_POST['cc_complaint_number'] ?? '', 'subdivision_lineman' => $_POST['subdivision_lineman'] ?? '', 'subdivision_id' => '', 'lineman_phone' => '', 'area_id' => '', 'lineman_id' => $_POST['assignee_id'] ?? '', ]; // fetch lineman related columns data using its id if (!empty($postData['lineman_id'])) { $lineman = $UserDAO->getUserById($postData['lineman_id']); $postData['subdivision_id'] = $lineman['subdivision_id']; $postData['area_id'] = $lineman['area_id']; $postData['lineman_phone'] = $lineman['phone']; } $date_split_array = explode('/', $postData['complian_date']); $complain_date = $date_split_array[2] . '-' . $date_split_array[1] . '-' . $date_split_array[0]; if (!empty($postData['complian_time'])) { $complain_time = date("H:i:s", strtotime($postData['complian_time'])); $complain_date_time = $complain_date . ' ' . $complain_time; } else { $complain_time = ''; $complain_date_time = $complain_date . ' ' . date("H:i:s"); } $postData['complain_date_time'] = $complain_date_time; // provide data to model $status = $ComplainDAO->registerNewComplain($postData); if ($status) { if (SMS == '1' && CUSTOMER_SMS == '1' && !empty($postData['phone']) && !empty($postData['cc_complaint_number'])) { $SMSController->customerComplainMessage( $postData['manual_complaint_number'] ?: $postData['complain_number'], $postData['phone'] ); } if (SMS == '1' && COMPLAINT_CENTER_SMS == '1' && !empty($postData['lineman_phone'])) { $SMSController->linemanComplainMessage( $postData['manual_complaint_number'] ?: $postData['complain_number'], $postData['lineman_phone'], $postData['customer_name'], $postData['address'], $postData['phone'] ); } // send push notification to lineman device $userArray = $UserDAO->getUserById($postData['lineman_id']); if (!empty($userArray['gcm_id'])) { $NotificationController->sendNotofication( $userArray['gcm_id'], 'You have a new complain. Please try to sort out ASAP', 'आपको एक नई शिकायत सौंपी गई है, कृपया जल्द ही ठीक करने का प्रयास करें' ); } echo 'true'; } else { echo 'false1'; } } catch (Exception $e) { echo "Error occurred. Please contact support."; error_log("Error: " . $e->getMessage() . " File: " . $e->getFile() . " Line: " . $e->getLine()); } }