Laravel controller doing too many things #38
Replies: 6 comments 6 replies
-
I think this function has multiple responsibilities, such as handling updating or creating activity records, checking for existing activities, and sending emails. |
Beta Was this translation helpful? Give feedback.
-
As in the above-provided code, the store function is performing the following tasks:
One way that I can think of to resolve this is by creating separate functions to handle different responsibilities and then using them here. |
Beta Was this translation helpful? Give feedback.
-
In this Not sure about create or update function that it work properly. For that need to test first. public function store(Request $request)
{
$data = $request->only([
'activity_name',
'activity_start_date',
'activity_city',
'timeslot',
'pickup_preference',
'user_name'
]);
$userEmail = $request->user_email;
if ($this->hasAlreadyAppliedForActivity($request)) {
return redirect()->back()->withErrors([
'error' => 'We are sorry, but you have already applied for an activity that is scheduled for the same day as this one. Please choose another activity to apply for.'
]);
}
// create and update record
$activityUser = ActivityUser::updateOrCreate(
[
'user_id' => $request->input('user_id'),
'activity_id' => $request->input('activity_id')
],
[
'my_pickup_preference' => $request->input('pickup_preference'),
'my_timeslot_preference' => $request->input('timeslot'),
'has_withdrawn' => false,
'reason_for_withdrawal' => null
]
);
$activityUser->save();
Mail::to($userEmail)->queue(new ActivityInfo($data));
return redirect()->route('activities.index')->with([
'success' => 'Thank you for your interest in applying for the volunteering activity. You can now view the details of the activity in the `My Activities` section.'
]);
}
public function hasAlreadyAppliedForActivity(Request $request)
{
// Check if user has already applied for an activity on the same day
$activity = Activities::find($request->input('activity_id'));
$existingActivityUser = ActivityUser::where('user_id', $request->input('user_id'))
->whereHas('activity', function ($query) use ($activity) {
$query->where('start_date', $activity->start_date);
})
->where('has_withdrawn', 0)
->first();
return $existingActivityUser;
}
|
Beta Was this translation helpful? Give feedback.
-
From my understanding the SRP (single responsibility function ) tell that a function or class has only one functionality not multiple. so we can now optimize the code to something like that : // checkUserExistingActivity method checks if the user has already applied for an activity for same day.
private function checkUserExistingActivity(Request $request)
{
// checking existing activity user code is something like that
$activity = Activities::find($request->input('activity_id'));
$existingActivityUser = ActivityUser::where('user_id', $request->input('user_id'))
->whereHas('activity', function ($query) use ($activity) {
$query->where('start_date', $activity->start_date);
})
->where('has_withdrawn', 0)
->first();
}
// and then create different function for update user activty.
private function updateUserActivity($request)
{
// update user activity code
}
// and then same for sending email
private function sendEmailActivity ($userEmail, $data)
{
// function to send an email with the activity information to the user.
}
// so on with this approach, we have different functions for different functionality, it avoids a Single function having multiple functionality. |
Beta Was this translation helpful? Give feedback.
-
Reading the code, my understanding is as follows:
I'd split the function in the following parts and then use approaches from Laravel to simply this. The function will look like this: // 1. data validation (required inputs and pouplating `$data` variable)
// 2. business logic validation (no conflicting application)
// 3. create new application or update existing
// 4. send the email I will try to add actual Laravel code as I find some more time. Meanwhile if someone wants to help feel free comment :) |
Beta Was this translation helpful? Give feedback.
-
Everyone has covered pretty much in their comments. I can suggest some code optimization.
we can use the direct create method like this way I will check more and share |
Beta Was this translation helpful? Give feedback.
-
Questions
In the following PHP/Laravel code:
Beta Was this translation helpful? Give feedback.
All reactions