-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Model Design question #43
Comments
Essentially it gives more flexibility. For example, Having said that, a better way to solve that might be: interface Form {
public function main($data);
public function submit($data, $callback = null);
} |
One place I have followed this is at https://github.com/solleer/User-Module. I feel like my form models are doing to much and that I am getting to much of a god class with the User class. |
I would agree. One way to tell is to notice how many parameters your constructor takes. It's not always a sure way to tell, but it's usually a good hint. I would think a single instance of |
I do wonder what the function 'validatePasswordConfirm` is doing in the class. As Robert C. Martin says: A class should have a single reason to change. This class has multiple reasons. Another good test is Misko Hevery's line "Describe the class job without using the word AND". I am struggling to come up with a description of this class that encompasses all the methods and doesn't use the word AND. There seem to be two different concerns here:
I'd separate these into two classes and I'm not sure why you need to use Respect\Validator just to check two strings are equal. Seems like an unnecessary dependency to me. The line
Doesn't sit well with me. This function knows a lot about |
I have now updated it to remove the About the current user, I have thought about extending the class also into a |
The places I would need both the |
The other issue with factoring out the current user stuff is that the save function uses it here if ($this->getUser($data->username) !== false && $data->username !== $this->getCurrentUser()->username) return false; |
I don't understand why a Or better yet, the property you store could even be Of course, that doesn't address whether all those properties and methods actually belong in the same class.. |
I do store the user_id and getCurrentUser is shorthand for what you said. The question is whether those current user methods belong in the user class since they have nothing to do with a regular user. |
One problem is that is CurrentUser extends User, then if on a separate project I want to extend User to change something for that project then I would have to redefine a CurrentUser object. So I think that composing the User object would be best for the CurrentUser to do. |
Generally speaking |
I have now factored the Current user out in solleer/Basic-User-Module@f3b62a8. I'm thinking about making an interface for User or something like that. |
I'd refactor authorize.php. see http://misko.hevery.com/code-reviewers-guide/flaw-digging-into-collaborators/ You'd be better off just passing the user instance into the class: <?php
namespace User\Model;
class Authorize {
private $model;
private $currentUser;
private $functions = [];
private $defaultFunctions = [
"user" => "\\User\\Model\\Authorize\\User"
];
private $id = false;
public function __construct(User $model, $currentUser) {
$this->model = $model;
$this->currentUser = $currentUser;
foreach ($this->defaultFunctions as $key => $function) $this->addFunction($key, new $function);
}
public function __call($name, $args) {
if (isset($this->functions[$name])) {
if ($this->id) $user = $this->model->getUser($this->id);
else $user = $this->currentUser;
$result = $this->functions[$name]->authorize($user, $args);
$this->id = false;
return $result;
}
throw new \Exception("Method \\User\\Model\\Authorize::" . $name . " does not exist");
}
public function id($id) {
$this->id = $id;
return $this;
}
public function addFunction($name, Authorizable $function) {
$this->functions[$name] = $function;
}
} |
Thanks, I made the change and updated the DI config. I'm trying to come up with a good way to make this system easily portable and customizable to any needs. I am working on an extension of this Module at https://github.com/solleer/Office365-User-Module in order to see how easily extensible the set up it to any needs. The problem I see is that I can't find a good way to do it without extending the User object. I will upload some code to the extension module and comment here when I do. |
I have added a bit of code to https://github.com/solleer/Office365-User-Module. I can't figure out how to make this easily extendable. |
@TomBZombie I am also questioning the need of the |
Also, should this function in Credentials move to the CurrentUser class public function checkCurrentUserSecurity($username, $password) {
$id = $this->validateUserCredential($username, $password);
if ($id === false || $id !== $status->getSigninVar()) return false;
return true;
} This function just feels like it doesn't truly belong in any of the current classes. |
The above function seems to best fit where it is or in the CurrentUser class but it doesn't seem to fit in either of those either. |
@TomBZombie Where do you think the checkCurrentUserSecurity should go. I think I've decided that it definitely will not go in current user because that would make CurrentUser too implementation specific. |
@TomBZombie Also I have an issue with https://github.com/solleer/Events-Module in usort($events, function ($event1, $event2) {
$date1 = new \DateTime($event1->start_date);
$date2 = new \DateTime($event2->start_date);
if ($date1 === $date2) return 0;
else return ($date1 < $date2) ? -1 : 1;
}); I thought about factoring it out but that would require a class to be passed to both classes that only is needed for the |
Also should the class CurrentUser implement User or stay the way it is. |
@TomBZombie Should the class CurrentUser implement User or stay the way it is. |
@TomBZombie In a discussion at https://www.sitepoint.com/community/t/mvc-refactor-take-2/194004/8 you mention having a success method in the model because it allows the rest to be reused. In another question you said that FormModels should only be to connect the domain model with the GUI and should have no business logic. What is the point then of separating out the success method if the FormModel just connects two things and has no main logic to reuse.
I have an interface that I implement
and I am starting to question the point of the success method if really all I do in
submit
is call a method on the model that the FormModel was given.The text was updated successfully, but these errors were encountered: