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. public function updateType(string $type)
  70. {
  71. $this->type = $type;
  72. }
  73. /**
  74. * @return string
  75. */
  76. public function id(): string
  77. {
  78. return $this->uuid;
  79. }
  80. /**
  81. * @return string
  82. */
  83. public function type(): string
  84. {
  85. return $this->type;
  86. }
  87. /**
  88. * @return bool
  89. */
  90. public function isEnabled(): bool
  91. {
  92. return $this->isEnabled;
  93. }
  94. /**
  95. * @return \DateTime
  96. */
  97. public function createdAt(): \DateTime
  98. {
  99. return $this->createdAt;
  100. }
  101. /**
  102. * @return \DateTime|null
  103. */
  104. public function updatedAt()
  105. {
  106. return $this->updatedAt;
  107. }
  108. /**
  109. * @return User
  110. */
  111. public function user(): User
  112. {
  113. return $this->user;
  114. }
  115. /*
  116. * Enable a role
  117. */
  118. public function enableRole()
  119. {
  120. $this->isEnabled = TRUE;
  121. $this->updatedAt = new \DateTime();
  122. }
  123. /*
  124. * Disable a role
  125. */
  126. public function disableRole()
  127. {
  128. $this->isEnabled = FALSE;
  129. $this->updatedAt = new \DateTime();
  130. }
  131. /**
  132. * @return bool
  133. */
  134. public function equals(Role $role)
  135. {
  136. return $this->id() === $role->id();
  137. }
  138. /**
  139. * Get base role
  140. *
  141. * @return string
  142. */
  143. public static final function baseRole($role)
  144. {
  145. switch ($role) {
  146. case self::ROLE_ADMIN:
  147. return self::BASE_ROLE_ADMIN;
  148. case self::ROLE_USER:
  149. return self::BASE_ROLE_USER;
  150. }
  151. return 'UNKNOWN';
  152. }
  153. /**
  154. * Get slugify role name
  155. *
  156. * @return string
  157. */
  158. public static final function roleSlug($role)
  159. {
  160. switch ($role) {
  161. case self::BASE_ROLE_ADMIN:
  162. return 'admin';
  163. case self::BASE_ROLE_USER:
  164. return 'user';
  165. }
  166. return 'unknown';
  167. }
  168. public function description()
  169. {
  170. $str = $this->type;
  171. return $str;
  172. }
  173. public function __toString()
  174. {
  175. $str = $this->type;
  176. return $str;
  177. }
  178. }