findOneByInvite($invite); if ($invite === null) { throw new \Exception("Invalid invite code"); } if ($this->exists($username)) { throw new \Exception("User already exists"); } $user = new UserModel(); $user->setUsername($username); $user->setPassword(\password_hash($password, PASSWORD_DEFAULT)); $user->save(); $invite->delete(); return $user; } /** * @param string $username * @return bool */ public function exists($username) { $amount = UserQuery::create()->findByUsername($username)->count(); return $amount > 0; } public function login($username, $password) { $user = UserQuery::create()->findOneByUsername($username); if ($user === null || !password_verify($password, $user->getPassword())) { return null; } return $user; } /** * @return string */ public function createInvite() { $invite = new Invite(); $invite->setInvite(bin2hex(openssl_random_pseudo_bytes(20))); $invite->save(); return $invite->getInvite(); } }