user = $forUser; $this->code = md5(random_bytes(100)); } public function getId(): int { return $this->id; } public function getUser(): User { return $this->user; } public function getCode(): string { return $this->code; } public function getUsedBy(): ?User { return $this->usedBy; } public function use(User $user): void { if ($this->usedBy) { throw new \RuntimeException(sprintf( 'Invite #%d is already used by User#%d and can\'t be used by User#%d', $this->id, $this->usedBy->getId(), $user->getId() )); } $this->usedBy = $user; } }