src/Domain/User/Model/Role.php line 18

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\User\Model;
  3. use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
  4. use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
  5. use Assert\Assertion;
  6. use Whater\Domain\Common\Exception\InvalidUUIDException;
  7. use Whater\Domain\User\Event\RoleWasCreated;
  8. use Whater\Domain\User\Exception\InvalidRoleException;
  9. use Ramsey\Uuid\Uuid;
  10. /**
  11. * Class Role
  12. *
  13. * @package Whater\Domain\User\Model
  14. */
  15. class Role implements ContainsRecordedEvents
  16. {
  17. use EventRecorderCapabilities;
  18. const BASE_ROLE_USER = 'BASE_ROLE_USER';
  19. const BASE_ROLE_ADMIN = 'BASE_ROLE_ADMIN';
  20. const ROLE_ADMIN = 'ROLE_ADMIN';
  21. const ROLE_USER = 'ROLE_USER';
  22. /**
  23. * @var string
  24. */
  25. private $uuid;
  26. /**
  27. * @var string
  28. */
  29. private $type;
  30. /**
  31. * @var boolean
  32. */
  33. private $isEnabled;
  34. /**
  35. * @var \DateTime
  36. */
  37. private $createdAt;
  38. /**
  39. * @var null|\DateTime
  40. */
  41. private $updatedAt;
  42. /**
  43. * @var \Whater\Domain\User\Model\User
  44. */
  45. private $user;
  46. /**
  47. * User constructor.
  48. *
  49. * @param string $roleId
  50. * @param string $type
  51. * @param boolean $isEnabled
  52. * @param User $user
  53. */
  54. public function __construct(string $roleId=null, string $type, bool $isEnabled, User $user)
  55. {
  56. try {
  57. $this->uuid = Uuid::fromString($roleId ?: Uuid::uuid4())->toString();
  58. } catch (\InvalidArgumentException $e) {
  59. throw new InvalidUUIDException();
  60. }
  61. $this->type = $type;
  62. $this->isEnabled = $isEnabled;
  63. $this->user = $user;
  64. $this->createdAt = new \DateTime();
  65. $this->updatedAt = new \DateTime();
  66. // RoleWasCreated Event
  67. $this->record(new RoleWasCreated($this, $user->plainPassword()));
  68. }
  69. /**
  70. * @return string
  71. */
  72. public function id(): string
  73. {
  74. return $this->uuid;
  75. }
  76. /**
  77. * @return string
  78. */
  79. public function type(): string
  80. {
  81. return $this->type;
  82. }
  83. /**
  84. * @return bool
  85. */
  86. public function isEnabled(): bool
  87. {
  88. return $this->isEnabled;
  89. }
  90. /**
  91. * @return \DateTime
  92. */
  93. public function createdAt(): \DateTime
  94. {
  95. return $this->createdAt;
  96. }
  97. /**
  98. * @return \DateTime|null
  99. */
  100. public function updatedAt()
  101. {
  102. return $this->updatedAt;
  103. }
  104. /**
  105. * @return User
  106. */
  107. public function user(): User
  108. {
  109. return $this->user;
  110. }
  111. /*
  112. * Enable a role
  113. */
  114. public function enableRole()
  115. {
  116. $this->isEnabled = TRUE;
  117. $this->updatedAt = new \DateTime();
  118. }
  119. /*
  120. * Disable a role
  121. */
  122. public function disableRole()
  123. {
  124. $this->isEnabled = FALSE;
  125. $this->updatedAt = new \DateTime();
  126. }
  127. /**
  128. * @return bool
  129. */
  130. public function equals(Role $role){
  131. return $this->id() === $role->id();
  132. }
  133. /**
  134. * Get base role
  135. *
  136. * @return string
  137. */
  138. public static final function baseRole($role)
  139. {
  140. switch($role) {
  141. case self::ROLE_ADMIN:
  142. return self::BASE_ROLE_ADMIN;
  143. case self::ROLE_USER:
  144. return self::BASE_ROLE_USER;
  145. }
  146. return 'UNKNOWN';
  147. }
  148. /**
  149. * Get slugify role name
  150. *
  151. * @return string
  152. */
  153. public static final function roleSlug($role)
  154. {
  155. switch($role) {
  156. case self::BASE_ROLE_ADMIN:
  157. return 'admin';
  158. case self::BASE_ROLE_USER:
  159. return 'user';
  160. }
  161. return 'unknown';
  162. }
  163. public function description(){
  164. $str = $this->type;
  165. return $str;
  166. }
  167. public function __toString()
  168. {
  169. $str = $this->type;
  170. return $str;
  171. }
  172. }