I would appreciate a thorough review of my updated User
entity class. It's still an anemic domain model, but behavior (like upgradeToPremium()
, etc) will be added as I'm developing my application further.
This is the first time that I'm commenting my code properly according to the phpDocumenter docs. So some feedback on that would also be highly appreciated.
Any other suggestions are always welcome.
User class:
namespace Model\Domain\User;
use Model\Domain\Entity\Entity;
use Model\Domain\User\Value\Email;
use Model\Domain\User\Value\Token;
use Model\Domain\User\Value\FullName;
use Model\Domain\User\Value\Password;
use Model\Domain\User\Authentication;
use Model\Domain\User\Value\SeriesNumber;
use Model\Domain\User\Value\RegisterDateTime;
/**
* User entity of the problem domain.
*
* @author John Doe <http://google.com>
*/
class User extends Entity
{
/**
* @var FullName $fullName A full name value object.
*/
private $fullName;
/**
* @var Email $email An email value object.
*/
private $email;
/**
* @var Password $password A password value object.
*/
private $password;
/**
* @var RegisterDateTime $registerDateTime A register date and time value object.
*/
private $registerDateTime;
/**
* @var array $authentications A list of Authentication child entities.
*/
private $authentications = [];
/**
* Gets the full name value object.
*
* @return FullName A FullName instance.
*/
public function getFullName()
{
return $this->fullName;
}
/**
* Sets the FullName instance.
*
* @param FullName $fullName A FullName instance.
*
* @return self The current User entity.
*/
public function setFullName(FullName $fullName)
{
$this->fullName = $fullName;
return $this;
}
/**
* Gets the email address.
*
* @return string An email address.
*/
public function getEmail()
{
return (string) $this->email;
}
/**
* Sets the Email instance.
*
* @param Email $email An Email instance.
*
* @return self The current User entity.
*/
public function setEmail(Email $email)
{
$this->email = $email;
return $this;
}
/**
* Gets the password.
*
* @return string A password.
*/
public function getPassword()
{
return (string) $this->password;
}
/**
* Sets the Password instance.
*
* @param Password $password A Password instance.
*
* @return self The current User entity.
*/
public function setPassword(Password $password)
{
$this->password = $password;
return $this;
}
/**
* Gets the date and time of registering.
*
* @return string A date and time of registering.
*/
public function getRegisterDateTime()
{
return (string) $this->registerDateTime;
}
/**
* Sets the RegisterDateTime instance.
*
* @param RegisterDateTime $registerDateTime A RegisterDateTime instance.
*
* @return self The current User entity.
*/
public function setRegisterDateTime(RegisterDateTime $registerDateTime)
{
$this->registerDateTime = $registerDateTime;
return $this;
}
/**
* Assembles an Authentication child entity and adds it to the list.
*
* @param integer $seriesNumber A series number.
* @param string $token A token.
* @param string $registerDateTime A date and time of registering.
*
* @throws Exception if series number is not unique.
*/
public function addAuthentication($seriesNumber, $token, $registerDateTime)
{
if ($this->findAuthenticationBySeriesNumber($seriesNumber)) {
throw new Exception('Series number is not unique.');
}
$seriesNumber = new SeriesNumber($seriesNumber);
$token = new Token($token);
$registerDateTime = new RegisterDateTime($registerDateTime);
$authentication = new Authentication();
$authentication->setSeriesNumber($seriesNumber)
->setToken($token)
->setRegisterDateTime($registerDateTime);
$this->authentications[$seriesNumber] = $authentication;
}
/**
* Finds the Authentication child entity with the matching series number.
*
* @param string $seriesNumber A series number.
*
* @return Authentication|void An Authentication child entity instance if found.
*/
public function findAuthenticationBySeriesNumber($seriesNumber)
{
if (isset($this->authentications[$seriesNumber])) {
return $this->authentications[$seriesNumber];
}
}
/**
* Deletes all Authentication child entities.
*/
public function deleteAllAuthentications()
{
$this->authentications = [];
}
/**
* Deletes the Authentication child entity with the matching series number.
*
* @param string $seriesNumber A series number.
*/
public function deleteAuthenticationBySeriesNumber($seriesNumber)
{
if (isset($this->authentications[$seriesNumber])) {
unset($this->authentications[$seriesNumber]);
}
}
}
Entity class:
namespace Model\Domain\Entity;
use \Exception;
use Model\Domain\Entity\Value\Identifier;
/**
* Base class for all entities in the problem domain.
*
* @author John Doe <http://google.com>
*/
abstract class Entity
{
/**
* @var Identifier $identifier An identifier value object.
*/
protected $identifier;
/**
* Gets the identifier.
*
* @return string An identifier.
*/
public function getIdentifier()
{
return (string) $this->identifier;
}
/**
* Sets the Identifier instance.
*
* @param Identifier $identifier An Identifier instance.
*
* @throws Exception if the identifier is already set.
*
* @return self The current entity.
*/
public function setIdentifier(Identifier $identifier)
{
if ($this->identifier) {
throw new Exception('Identifier is already set and therefore immutable.');
}
$this->identifier = $identifier;
return $this;
}
}