File size: 14,027 Bytes
cf14c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 | <?php
declare(strict_types=1);
namespace User\Domain\Entities;
use Affiliate\Domain\Entities\AffiliateEntity;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use Ramsey\Uuid\Uuid;
use Shared\Domain\ValueObjects\CityName;
use Shared\Domain\ValueObjects\CountryCode;
use Shared\Domain\ValueObjects\Id;
use Shared\Domain\ValueObjects\Ip;
use Traversable;
use User\Domain\Exceptions\InvalidPasswordException;
use User\Domain\Exceptions\InvalidTokenException;
use User\Domain\ValueObjects\ApiKey;
use User\Domain\ValueObjects\Email;
use User\Domain\ValueObjects\EmailVerificationToken;
use User\Domain\ValueObjects\FirstName;
use User\Domain\ValueObjects\IsEmailVerified;
use User\Domain\ValueObjects\Language;
use User\Domain\ValueObjects\LastName;
use User\Domain\ValueObjects\Password;
use User\Domain\ValueObjects\PasswordHash;
use User\Domain\ValueObjects\RecoveryToken;
use User\Domain\ValueObjects\Role;
use User\Domain\ValueObjects\Status;
use Workspace\Domain\Entities\WorkspaceEntity;
use Workspace\Domain\Exceptions\WorkspaceNotFoundException;
use Workspace\Domain\ValueObjects\Name;
#[ORM\Entity]
#[ORM\Table(name: 'user')]
#[ORM\Index(columns: ['first_name'])]
#[ORM\Index(columns: ['last_name'])]
#[ORM\HasLifecycleCallbacks]
class UserEntity
{
/** A unique numeric identifier of the entity. */
#[ORM\Embedded(class: Id::class, columnPrefix: false)]
private Id $id;
#[ORM\Column(type: Types::SMALLINT, enumType: Role::class, name: 'role')]
private Role $role;
/** The email of the user entity */
#[ORM\Embedded(class: Email::class, columnPrefix: false)]
private Email $email;
/** Password hash of the user */
#[ORM\Embedded(class: PasswordHash::class, columnPrefix: false)]
private PasswordHash $passwordHash;
/** First name of the user entity */
#[ORM\Embedded(class: FirstName::class, columnPrefix: false)]
private FirstName $firstName;
/** Last name of the user entity */
#[ORM\Embedded(class: LastName::class, columnPrefix: false)]
private LastName $lastName;
/** Language of the user */
#[ORM\Embedded(class: Language::class, columnPrefix: false)]
private Language $language;
#[ORM\Embedded(class: ApiKey::class, columnPrefix: false)]
private ApiKey $apiKey;
#[ORM\Embedded(class: Ip::class, columnPrefix: false)]
private Ip $ip;
#[ORM\Column(type: Types::STRING, enumType: CountryCode::class, name: 'country_code', nullable: true)]
private ?CountryCode $countryCode = null;
#[ORM\Embedded(class: CityName::class, columnPrefix: false)]
private CityName $cityName;
/** Creation date and time of the entity */
#[ORM\Column(type: 'datetime', name: 'created_at')]
private DateTimeInterface $createdAt;
/** The date and time when the entity was last modified. */
#[ORM\Column(type: 'datetime', name: 'updated_at', nullable: true)]
private ?DateTimeInterface $updatedAt = null;
#[ORM\Column(type: Types::SMALLINT, enumType: Status::class, name: 'status')]
private Status $status;
#[ORM\Embedded(class: RecoveryToken::class, columnPrefix: false)]
private RecoveryToken $recoveryToken;
#[ORM\Embedded(class: IsEmailVerified::class, columnPrefix: false)]
private IsEmailVerified $isEmailVerified;
#[ORM\Embedded(class: EmailVerificationToken::class, columnPrefix: false)]
private EmailVerificationToken $emailVerificationToken;
/** @var Collection<int,WorkspaceEntity> */
#[ORM\ManyToMany(targetEntity: WorkspaceEntity::class, mappedBy: 'users')]
private Collection $workspaces;
/** @var Collection<int,WorkspaceEntity> */
#[ORM\OneToMany(targetEntity: WorkspaceEntity::class, mappedBy: 'owner', cascade: ['persist', 'remove'])]
private Collection $ownedWorkspaces;
#[ORM\ManyToOne(targetEntity: WorkspaceEntity::class)]
#[ORM\JoinColumn(name: 'current_workspace_id', nullable: true, onDelete: 'SET NULL')]
private ?WorkspaceEntity $currentWorkspace = null;
#[ORM\OneToOne(targetEntity: AffiliateEntity::class, cascade: ['persist', 'remove'], mappedBy: 'user')]
private ?AffiliateEntity $affiliate = null;
#[ORM\ManyToOne(targetEntity: UserEntity::class)]
#[ORM\JoinColumn(name: 'referred_by', nullable: true, onDelete: 'SET NULL')]
private ?UserEntity $referredBy = null;
public function __construct(
Email $email,
FirstName $firstName,
LastName $lastName
) {
$this->id = new Id();
$this->role = Role::USER;
$this->email = $email;
$this->passwordHash = new PasswordHash();
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->language = new Language();
$this->createdAt = new DateTime();
$this->status = Status::ACTIVE;
$this->recoveryToken = new RecoveryToken();
$this->isEmailVerified = new IsEmailVerified();
$this->emailVerificationToken = new EmailVerificationToken(Uuid::uuid4()->toString());
$this->workspaces = new ArrayCollection();
$this->ownedWorkspaces = new ArrayCollection();
$this->apiKey = new ApiKey();
$this->createDefaultWorkspace();
$this->createAffiliate();
}
public function getId(): Id
{
return $this->id;
}
public function getRole(): Role
{
return $this->role;
}
public function setRole(Role $role): self
{
$this->role = $role;
return $this;
}
public function getEmail(): Email
{
return $this->email;
}
public function getFirstName(): FirstName
{
return $this->firstName;
}
public function setFirstName(FirstName $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): LastName
{
return $this->lastName;
}
public function setLastName(LastName $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getLanguage(): Language
{
return $this->language;
}
public function setLanguage(Language $language): self
{
$this->language = $language;
return $this;
}
public function getApiKey(): ApiKey
{
return $this->apiKey;
}
public function generateApiKey(): void
{
$this->apiKey = new ApiKey();
}
public function getIp(): Ip
{
return $this->ip;
}
public function setIp(Ip $ip): self
{
$this->ip = $ip;
return $this;
}
public function getCountryCode(): ?CountryCode
{
return $this->countryCode;
}
public function setCountryCode(?CountryCode $countryCode): self
{
$this->countryCode = $countryCode;
return $this;
}
public function getCityName(): CityName
{
return $this->cityName;
}
public function setCityName(CityName $cityName): self
{
$this->cityName = $cityName;
return $this;
}
public function getCreatedAt(): DateTimeInterface
{
return $this->createdAt;
}
public function getUpdatedAt(): ?DateTimeInterface
{
return $this->updatedAt;
}
public function getStatus(): Status
{
return $this->status;
}
public function setStatus(Status $status): self
{
$this->status = $status;
return $this;
}
public function getRecoveryToken(): RecoveryToken
{
return $this->recoveryToken;
}
public function generateRecoveryToken(): void
{
$this->recoveryToken = new RecoveryToken(Uuid::uuid4()->toString());
}
public function validateRecoveryToken(RecoveryToken $token): true
{
if ($this->recoveryToken->value !== $token->value) {
throw new InvalidTokenException($this, $token);
}
return true;
}
public function isEmailVerified(): IsEmailVerified
{
return $this->isEmailVerified;
}
public function verifyEmail(EmailVerificationToken $token): void
{
if ($this->emailVerificationToken->value !== $token->value) {
throw new InvalidTokenException($this, $token);
}
$this->isEmailVerified = new IsEmailVerified(true);
$this->emailVerificationToken = new EmailVerificationToken();
}
public function getEmailVerificationToken(): EmailVerificationToken
{
return $this->emailVerificationToken;
}
public function unverifyEmail(): void
{
$this->isEmailVerified = new IsEmailVerified(false);
$this->emailVerificationToken = new EmailVerificationToken(
Uuid::uuid4()->toString()
);
}
/**
* Password is required to change the email
*
* @param Email $email
* @param Password $password
* @return UserEntity
* @throws InvalidPasswordException
*/
public function updateEmail(Email $email, Password $password): self
{
$this->verifyPassword($password);
$this->email = $email;
$this->isEmailVerified = new IsEmailVerified(false);
$this->emailVerificationToken = new EmailVerificationToken(
Uuid::uuid4()->toString()
);
return $this;
}
/**
* Current password is required to change the password
*
* @param Password $currentPassword
* @param Password $password
* @return UserEntity
* @throws InvalidPasswordException
*/
public function updatePassword(
Password $currentPassword,
Password $password
): self {
$this->verifyPassword($currentPassword);
if ($currentPassword->value === $password->value) {
throw new InvalidPasswordException(
$this,
$password,
InvalidPasswordException::TYPE_SAME_AS_OLD
);
}
$this->setPassword($password);
return $this;
}
public function resetPassword(
RecoveryToken $token,
Password $password
): self {
// Validate the recovery token
$this->validateRecoveryToken($token);
// Set the new password
$this->setPassword($password);
// Reset the recovery token
$this->recoveryToken = new RecoveryToken();
return $this;
}
/**
* @param Password $password
* @return bool
* @throws InvalidPasswordException
*/
public function verifyPassword(Password $password): bool
{
if (
!$this->passwordHash->value
|| !password_verify(
$password->value,
$this->passwordHash->value
)
) {
throw new InvalidPasswordException(
$this,
$password,
InvalidPasswordException::TYPE_INCORRECT
);
}
return true;
}
public function hasPassword(): bool
{
return !is_null($this->passwordHash->value);
}
public function setPassword(Password $password): void
{
$this->passwordHash = new PasswordHash(
$password->value ?
password_hash($password->value, PASSWORD_DEFAULT)
: null
);
}
#[ORM\PreUpdate]
public function preUpdate(): void
{
$this->updatedAt = new DateTime();
}
public function createWorkspace(Name $name): WorkspaceEntity
{
$workspace = new WorkspaceEntity($this, $name);
$this->ownedWorkspaces->add($workspace);
// Switch to the new workspace
$this->currentWorkspace = $workspace;
return $workspace;
}
/**
* @return Traversable<WorkspaceEntity>
* @throws Exception
*/
public function getWorkspaces(): Traversable
{
return $this->workspaces->getIterator();
}
/**
* @return Traversable<WorkspaceEntity>
* @throws Exception
*/
public function getOwnedWorkspaces(): Traversable
{
return $this->ownedWorkspaces->getIterator();
}
#[ORM\PostLoad]
public function postLoad(): void
{
if ($this->ownedWorkspaces->count() == 0) {
$this->createDefaultWorkspace();
}
if (!$this->currentWorkspace) {
$this->currentWorkspace = $this->ownedWorkspaces->first();
}
if (!$this->affiliate) {
$this->createAffiliate();
}
}
private function createDefaultWorkspace(): void
{
$this->createWorkspace(new Name('Personal'));
}
private function createAffiliate(): void
{
$this->affiliate = new AffiliateEntity($this);
}
public function getAffiliate(): AffiliateEntity
{
return $this->affiliate;
}
public function getReferredBy(): ?UserEntity
{
return $this->referredBy;
}
public function setReferredBy(UserEntity $referredBy): void
{
$this->referredBy = $referredBy;
}
public function getCurrentWorkspace(): WorkspaceEntity
{
return $this->currentWorkspace;
}
public function setCurrentWorkspace(WorkspaceEntity $workspace): void
{
$this->currentWorkspace = $workspace;
}
public function getWorkspaceById(Id $id): WorkspaceEntity
{
/** @var WorkspaceEntity */
foreach ($this->workspaces as $workspace) {
if ($workspace->getId()->getValue() == $id->getValue()) {
return $workspace;
}
}
/** @var WorkspaceEntity */
foreach ($this->ownedWorkspaces as $workspace) {
if ($workspace->getId()->getValue() == $id->getValue()) {
return $workspace;
}
}
throw new WorkspaceNotFoundException($id);
}
}
|