[請益] 我該怎麼改善這個肥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