I didn't change much.
I think you're correct in having getters as well as methods such as 'canViewPage'.
The Methods 'canViewPage' and 'hasPermissions' are just wrappers around the 'UserGroup' class, the logic is in 'UserGroup'. It makes sense to create wrappers around 'UserGroup', for example if you want to have also Permission based solely on the User(Example a root user) then you would just have to change these Methods. If you had invoked the Methods directly on the 'UserGroup' - not over the 'User' - you would have to go over all these places. Pherhaps it would be Usefull to have a 'Permissions' class, wich would have a constructor or factory method, where you could give an 'User' as Argument.
<?php
/* The class for constructing any user's information
*/
class User {
protected $userId, $email, $userGroup;
protected function getEmail() {
return $this->email;
}
protected function getUserId() {
return $this->userId;
}
public function __construct($userId, $email, $emailGroup) {
$this->userId = $userId;
$this->email = $email;
// You never use userGroup directly, you can just
// put the object as membervariable, so you don't
// have to instantiate the object twice
$this->userGroup = new UserGroup($emailGroup);
}
// Method for checking whether user can view a specific page
public function canViewPage($pageId) {
/*
if(condition){
return true;
else
return false;
is equals
return (bool) condition
*/
return (bool) $this->userGroup->canViewPage($pageId);
}
// Method for checking permissions
public function hasPermissions($alias) {
/* alias is the name of the permissions. ie can-book
* create usergroup object based on current user's usergroup */
return (bool) $this->userGroup->hasPermissions($alias);
}
}
class UserMapper {
// database connection
private $db;
public function __construct(\PDO $db)
{
$this->db = $db;
}
public function findByUserId ($userId) {
$userObject = new User();
$q = $this->db->prepare("SELECT userId, email, userGroup FROM user WHERE userId = :userId");
$q->bindValue(":userId", $id);
$q->setFetchMode( PDO::FETCH_INTO, $userObject);
$q->execute();
$q->fetch(PDO::FETCH_INTO);
return $userObject;
}
}
?>
I don't understand your Second Questions, pherhaps you made some typos?
You have '$getUserGroup', but use '$getUserGroupMapper' and you give the method 'findByUserId' a UserGroup.