I'm currently facing problem with correct design patterns for following problem in MVC:
I have following classes:
GroupController
GroupService
GroupUserController
GroupUserService
LogService
(One Group may have many users)
For each controller I have CRUD
, which is available for user's with certain role.
One of the functionality is that logged user may create group (GroupService
). If he does that, then he must be assigned automatically to user group (GroupUserService
). I also need to log information to LogService
when and how (by CRUD
or by method available only for specific role) user created group.
Currently I have something like this:
// GroupController.php
// Create new group
public function setAction()
{
//code...
if ($this->getRequest()->isPost()) {
$data = $this->request->getPost()->toArray();
$form->setData($data);
if ($form->isValid()) {
$data = $form->getData();
$result = $groupService->save($data, $data['id']);
// Add user to that group
if ($result) {
$groupUserData = [
'group' => $result->getId(),
'user' => $user->getId()
];
$result2 = $groupUserService->save($groupUserData); // <---- service which shouldn't be here?
if ($result2) {
// Some other action let's say with another service
}
$logData = [
'msg' => 'New Group created!',
'created_at' => new \DateTime()
];
$logService->save($logData); // <---- this service shouldn't be also here?
}
$this->redirect()->toRoute($this->route);
}
}
return $this->viewModel;
}
For now, this action is doing many tasks instead of just one. Isn't that breaking of SOLID principle?
I think these services shouldn't be in this controller.
What design patterns should I use here? Chain of Responsibility? Observer? Mediator?
Such problem I have in many controllers. I mean many actions need use of multiple services
Aucun commentaire:
Enregistrer un commentaire