I don't really have a problem with me code, more like a problem with how my code is structured.
My project is written in Laravel and I use the resource controllers that laravel generate for us. Inside my Update() function I do a lot of data processing.
Some examples:
Update()
public function update(Request $request)
{
$post = (object) $request->all();
unset($post->XDEBUG_SESSION_START);
$this->preparePassword($post);
$this->prepareMail($post);
$this->prepareAvailability($post);
$this->prepareZip($post);
$this->prepareSofttags($post);
$user = User::find(Auth::id());
$user->update((array) $post);
}
The update calls functions to process the data. Some examples:
private function prepareAvailability(stdClass &$data) : void
{
// If no days or a date is selected, return
if (empty($data->available_days) && empty($data->available_from)) {
return;
}
// when days are given (available)
if ($data->available_days) {
$days = (int) $data->available_days;
$avail_level = 0.2 * $days;
$data->avail_level = $avail_level;
$data->avail_from = mysqldate(strtotime('today'), true);
unset($data->available_days);
}
// When a date is given (no available)
if ($data->available_from) {
$data->avail_level = 0;
$data->avail_from = mysqldate(strtotime($data->available_from), true);
unset($data->available_from);
}
}
and:
private function prepareZip(stdClass &$data) : void
{
if (empty($data->zip)) {
return;
}
$zip4d = (int) substr($data->zip, 0, 4);
$zipData = Zip::check($zip4d);
$data->city = $zipData->name;
}
I just showed you 2 functies even tho I have a lot more and there will be more. Now my controller is filled with private functions that just doing some processin data in many ways.
Is there a better way, I was thinking about traits but traits should be compatible with every class. Not specific to one controller.
Any thoughts about this anyone?
P.S. I use some Helper functions if you dont understand some function that are used in the code like 'mysqldate'