Created
March 27, 2013 13:11
-
-
Save wouterj/5254054 to your computer and use it in GitHub Desktop.
Simple User Management example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Authentication | |
{ | |
/** | |
* @var UserProviderInterface | |
*/ | |
private $userprovider; | |
/** | |
* @var User | |
*/ | |
private $_user; | |
/** | |
* @param UserProviderInterface $userprovider | |
*/ | |
public function __construct($userprovider) | |
{ | |
$this->setUserProvider($userprovider); | |
} | |
public function getUser() | |
{ | |
if (null === $this->_user) { | |
$this->_user = $this->createUser(); | |
} | |
return $this->_user; | |
} | |
protected function createUser() | |
{ | |
try { | |
$user = $this->userprovider->getUser(); | |
} catch (\Exception $e) { | |
// als er iets fout is gegaan (\Exception moet preciezer) | |
$user = new User('anonymous'); | |
} | |
return $user; | |
} | |
private function setUserProvider(UserProviderInterface $userprovider) | |
{ | |
$this->userpovider = $userprovider; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Role implements RoleInterface | |
{ | |
private $role; | |
public function __construct($role) | |
{ | |
$this->setRole($role); | |
} | |
public function getRole() | |
{ | |
return $this->role; | |
} | |
/** | |
* @param string $role | |
*/ | |
private function setRole($role) | |
{ | |
$this->role = $role; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface RoleInterface | |
{ | |
/** | |
* @return string | |
*/ | |
public function getRole(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User | |
{ | |
private $name; | |
/** | |
* @var RoleInterface | |
*/ | |
private $role; | |
/** | |
* @param string $name | |
*/ | |
public function __construct($name) | |
{ | |
$this->setName($name); | |
} | |
/** | |
* @param string $name | |
*/ | |
public function setName($name) | |
{ | |
$this->name = $name; | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function setRole(RoleInterface $role) | |
{ | |
$this->role = $role; | |
} | |
/** | |
* @return RoleInterface | |
*/ | |
public function getRole() | |
{ | |
return $this->role; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment