[请益] 我该怎么改善这个肥controller action?

楼主: poopoo888888 (阿川)   2014-11-28 10:33:26
小弟最近在写一个专案管理软件(用Laravel)
这个网站有整合Google Drive SDK
我希望我的code漂亮遵循“Separation of Concern”,这样我单元测试比较容易
我有'Project'跟'Document'类别当作domain model
然后我用Decorator Pattern写了
'ProjectValidator'跟'DocumentPermissionManager' 类别
前者用来验证资料、后者处理Google Drive 权限API
写了半天,最后我有一个controller action还是很肥很丑:
(当管理者把一个专案状态设为ready)
public function postSendToReady()
{
$project = Project::find(Input::get('id'));
$project->status = Project::READY_STATUS;
$validator = new ProjectValidator($project);
if ( !$validator->validateAllDocumentsAreReady() ){
return Redirect::to('/pm')->with('messages', $validator->getErrors() );
}
$project->save();
foreach($project->documents as $document){
$permissionManager = new DocumentPermissionManager($document);
$permissionManager->insertPermission(Auth::user()->email);
// email failure should not stop the procedure. log it and go on
try{
Mailer::notifyAdministrators($document);
} catch (Exception $e) {
Log::error($e, array('url'=>Request::url()),
array('url'=>Request::url()));
}
try{
Mailer::notifyClients($document);
} catch (Exception $e) {
Log::error($e, array('url'=>Request::url()),
array('url'=>Request::url()));
}
}
return Redirect::to('/pm')->with('message', 'Updated project
successfully.');
}
这个controller action很肥、而且没有重用性
我该怎么办?我该多写哪几种类别吗?
我少写什么抽象化?有什么Design Pattern能帮助我吗?
感激不尽!!! <(_ _)>
作者: amhuang (阿铭)   2014-11-28 23:43:00
可以先了解 SOLID principles

Links booklink

Contact Us: admin [ a t ] ucptt.com