user = $user; $this->status = $status; if ($data) { $this->message = $data->getMessage(); $this->token = $data->getToken(); $this->view = $data->getView(); $this->loggedUser = $data->getLoggedUser(); $this->failReason = $data->getFailReason(); } } /** * 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) { $data = $reason ? ResultData::fromFailReason($reason) : ResultData::fromNothing(); return new Result(self::STATUS_FAIL, null, $data); } /** * Create an instance for a login requiring a second step. E.g. for 2FA. */ public static function secondStepRequired(User $user, ResultData $data) { return new Result(self::STATUS_SECOND_STEP_REQUIRED, $user, $data); } /** * 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 returned 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; } }