aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Authentication/Login/LoginResult.php
blob: 95e87b520e33086cd5cc0934f9e94e7b9b918bf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OC\Authentication\Login;

use OC\Core\Controller\LoginController;

class LoginResult {
	/** @var bool */
	private $success;

	/** @var LoginData */
	private $loginData;

	/** @var string|null */
	private $redirectUrl;

	/** @var string|null */
	private $errorMessage;

	private function __construct(bool $success, LoginData $loginData) {
		$this->success = $success;
		$this->loginData = $loginData;
	}

	private function setRedirectUrl(string $url) {
		$this->redirectUrl = $url;
	}

	private function setErrorMessage(string $msg) {
		$this->errorMessage = $msg;
	}

	public static function success(LoginData $data, ?string $redirectUrl = null) {
		$result = new static(true, $data);
		if ($redirectUrl !== null) {
			$result->setRedirectUrl($redirectUrl);
		}
		return $result;
	}

	/**
	 * @param LoginController::LOGIN_MSG_*|null $msg
	 */
	public static function failure(LoginData $data, ?string $msg = null): LoginResult {
		$result = new static(false, $data);
		if ($msg !== null) {
			$result->setErrorMessage($msg);
		}
		return $result;
	}

	public function isSuccess(): bool {
		return $this->success;
	}

	public function getRedirectUrl(): ?string {
		return $this->redirectUrl;
	}

	public function getErrorMessage(): ?string {
		return $this->errorMessage;
	}
}