【发布时间】:2014-01-03 12:34:21
【问题描述】:
我正在重构我的 Laravel 4 应用程序,我想要一些关于我的方法和最佳实践的建议。我正在努力做到尽可能 DRY,同时也遵循 SOLID 原则。
我有一个更新方法来更新我的应用程序中的委托。委托和事件之间存在多对多关系,我需要在更新时检查是否满足几个条件才能执行正确的操作/功能。
例如,到目前为止,我检查了代表是否是未分配给事件的浮动代表,如果是,则需要检查事件容量、重考容量和考试容量,同时还要检查是否已选择将委托移动到的事件。
public function update($id)
{
$delegate = $this->delegate->findById($id);
$input = Input::all();
if ($delegate->isFloater())
{
if (empty($input['new_event_id']))
{
$message = (object) array(
'title' => 'Oops!',
'content' => 'You did not select an event to move this floating delegate to.',
'alert_type' => 'error'
);
return Redirect::back()->withInput()->with('message', $message);
}
$newEvent = $this->event->findById($input['new_event_id']);
if ( ! $newEvent->hasCapacity())
{
$message = (object) array(
'title' => 'Oops!',
'content' => 'The event you are trying to move this floating delegate to has no availability.',
'alert_type' => 'error'
);
return Redirect::back()->withInput()->with('message', $message);
}
if ( ! $newEvent->hasResitCapacity($input))
{
$message = (object) array(
'title' => 'Oops!',
'content' => 'The event you are trying to move this floating delegate to has no resit spaces left.',
'alert_type' => 'error'
);
return Redirect::back()->withInput()->with('message', $message);
}
if ( ! $newEvent->hasExamCapacity($input))
{
$message = (object) array(
'title' => 'Oops!',
'content' => 'The event you are trying to move this floating delegate to has no exam spaces left.',
'alert_type' => 'error'
);
return Redirect::back()->withInput()->with('message', $message);
}
$this->delegate->moveFloater($id, $input, $newEvent);
$message = (object) array(
'title' => 'Excellent!',
'content' => 'This floating delegate was successfully moved.',
'alert_type' => 'success'
);
return Redirect::to('admin/events')->with('message', $message);
}
}
它肯定比我以前的要好得多,我正在将代码抽象为各种模型方法,例如 hasCapacity() 和 isFloater() 等,但该方法已经很大了,我正在重复有关消息的代码和重定向。
目前我不确定如何最好地处理这个问题,我正在寻找一些关于如何继续并进一步清理它的建议。
任何帮助将不胜感激。提前致谢。
【问题讨论】:
标签: refactoring laravel-4 dry solid-principles