*/ #[ORM\ManyToMany(targetEntity: WorkspaceEntity::class, mappedBy: 'users')] private Collection $workspaces; /** @var Collection */ #[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 * @throws Exception */ public function getWorkspaces(): Traversable { return $this->workspaces->getIterator(); } /** * @return Traversable * @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); } }