State::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @copyright 2018 Christoph Wurst <[email protected]>
5
 *
6
 * @author 2018 Christoph Wurst <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\TwoFactorGateway\Provider;
26
27
use JsonSerializable;
28
use OCP\IUser;
29
30
class State implements JsonSerializable {
31
32
	/** @var IUser */
33
	private $user;
34
35
	/** @var int */
36
	private $state;
37
38
	/** @var string */
39
	private $gatewayName;
40
41
	/** @var string|null */
42
	private $identifier;
43
44
	/** @var string|null */
45
	private $verificationCode;
46
47
	public function __construct(IUser $user,
48
		int $state,
49
		string $gatewayName,
50
		string $identifier = null,
51
		string $verificationCode = null) {
52
		$this->user = $user;
53
		$this->gatewayName = $gatewayName;
54
		$this->state = $state;
55
		$this->identifier = $identifier;
56
		$this->verificationCode = $verificationCode;
57
	}
58
59
	public static function verifying(IUser $user,
60
		string $gatewayName,
61
		string $identifier,
62
		string $verificationCode): State {
63
		return new State(
64
			$user,
65
			SmsProvider::STATE_VERIFYING,
66
			$gatewayName,
67
			$identifier,
68
			$verificationCode
69
		);
70
	}
71
72
	public static function disabled(IUser $user, string $gatewayName): State {
73
		return new State(
74
			$user,
75
			SmsProvider::STATE_DISABLED,
76
			$gatewayName
77
		);
78
	}
79
80
	public function verify(): State {
81
		return new State(
82
			$this->user,
83
			SmsProvider::STATE_ENABLED,
84
			$this->gatewayName,
85
			$this->identifier,
86
			$this->verificationCode
87
		);
88
	}
89
90
	/**
91
	 * @return IUser
92
	 */
93
	public function getUser(): IUser {
94
		return $this->user;
95
	}
96
97
	/**
98
	 * @return int
99
	 */
100
	public function getState(): int {
101
		return $this->state;
102
	}
103
104
	/**
105
	 * @return string
106
	 */
107
	public function getGatewayName(): string {
108
		return $this->gatewayName;
109
	}
110
111
	/**
112
	 * @return string|null
113
	 */
114
	public function getIdentifier() {
115
		return $this->identifier;
116
	}
117
118
	/**
119
	 * @return null|string
120
	 */
121
	public function getVerificationCode() {
122
		return $this->verificationCode;
123
	}
124
125
	public function jsonSerialize() {
126
		return [
127
			'gatewayName' => $this->gatewayName,
128
			'state' => $this->state,
129
			'phoneNumber' => $this->identifier,
130
		];
131
	}
132
}
133