user = $user; $this->status = $status; if ($params) { $this->message = $params->message ?? null; $this->token = $params->token ?? null; $this->view = $params->view ?? null; $this->loggedUser = $params->loggedUser ?? null; $this->failReason = $params->failReason ?? null; } } /** * Create an instance for a successful login. */ public static function success(User $user) { return new Result(self::STATUS_SUCCESS, $user); } /** * Create an instance for a failed login. */ public static function fail(?string $reason = null) { return new Result(self::STATUS_FAIL, null, (object) [ 'failReason' => $reason, ]); } /** * Create an instance for a login requiring a second step. E.g. for 2FA. */ public static function secondStepRequired(User $user, StdClass $params) { return new Result(self::STATUS_SECOND_STEP_REQUIRED, $user, $params); } /** * Login is successful. */ public function isSuccess() : bool { return $this->status === self::STATUS_SUCCESS; } /** * Second step is required. E.g. for 2FA. */ public function isSecondStepRequired() : bool { return $this->status === self::STATUS_SECOND_STEP_REQUIRED; } /** * Login is failed. */ public function isFail() : bool { return $this->status === self::STATUS_FAIL; } /** * Get a user. */ public function getUser() : ?User { return $this->user; } /** * Get a logged user. Considered that an admin user can log in as another user. The logged user will be an admin user. */ public function getLoggedUser() : ?User { return $this->loggedUser ?? $this->user; } /** * A status. */ public function getStatus() : string { return $this->status; } /** * A client view to redirect to for a second step. */ public function getView() : ?string { return $this->view; } /** * A message to show to end user for a second step. */ public function getMessage() : ?string { return $this->message; } /** * A token can be retured to a client to be used instead of password in a request for a second step. */ public function getToken() : ?string { return $this->token; } /** * A fail reason. */ public function getFailReason() : ?string { return $this->failReason; } }