Most of the Symfony projects I’ve worked on are rife with lines of code like this (in the actions):
$this->getUser()->getGuardUser()->getProfile()->getEmail()
To conform to the above ideals (The Law Of Demeter) the code should be re-written as:
$this->getUser()->getEmail()
My solution for that problem is using magic method __call() in myUser class:
public function __call($method, $arguments)
{
if(is_array($arguments))
{
return call_user_func_array(array($this->getGuardUser()->getProfile(), $method), $arguments);
}
else
{
return $this->getGuardUser()->getProfile()->$method();
}
}
It's possible because both tables have 1:1 relationship. This way we basically merge both objects and don't have to create dozens of "shortcut" methods.
No comments:
Post a Comment