StateStorage::get()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 19
ccs 15
cts 15
cp 1
rs 9.7666
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2024 Christoph Wurst <[email protected]>
7
 * SPDX-License-Identifier: AGPL-3.0-or-later
8
 */
9
10
namespace OCA\TwoFactorGateway\Service;
11
12
use Exception;
13
use OCA\TwoFactorGateway\AppInfo\Application;
14
use OCA\TwoFactorGateway\Provider\State;
15
use OCP\IConfig;
16
use OCP\IUser;
17
18
class StateStorage {
19
	public const STATE_DISABLED = 0;
20
	public const STATE_START_VERIFICATION = 1;
21
	public const STATE_VERIFYING = 2;
22
	public const STATE_ENABLED = 3;
23
24 6
	public function __construct(
25
		private IConfig $config,
26
	) {
27 6
	}
28
29 6
	private function buildConfigKey(string $gatewayName, string $key): string {
30 6
		return $gatewayName . "_$key";
31
	}
32
33 4
	private function getUserValue(IUser $user, string $gatewayName, string $key, string $default = ''): string {
34 4
		$gatewayKey = $this->buildConfigKey($gatewayName, $key);
35 4
		return $this->config->getUserValue($user->getUID(), Application::APP_ID, $gatewayKey, $default);
0 ignored issues
show
Deprecated Code introduced by
The function OCP\IConfig::getUserValue() has been deprecated: 33.0.0 - use {@see IUserConfig} directly ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

35
		return /** @scrutinizer ignore-deprecated */ $this->config->getUserValue($user->getUID(), Application::APP_ID, $gatewayKey, $default);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
36
	}
37
38 2
	private function setUserValue(IUser $user, string $gatewayName, string $key, ?string $value): void {
39 2
		$gatewayKey = $this->buildConfigKey($gatewayName, $key);
40 2
		$this->config->setUserValue($user->getUID(), Application::APP_ID, $gatewayKey, $value);
0 ignored issues
show
Deprecated Code introduced by
The function OCP\IConfig::setUserValue() has been deprecated: 33.0.0 - use {@see IUserConfig} directly ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

40
		/** @scrutinizer ignore-deprecated */ $this->config->setUserValue($user->getUID(), Application::APP_ID, $gatewayKey, $value);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
41
	}
42
43
	private function deleteUserValue(IUser $user, string $gatewayName, string $key): void {
44
		$gatewayKey = $this->buildConfigKey($gatewayName, $key);
45
		$this->config->deleteUserValue($user->getUID(), Application::APP_ID, $gatewayKey);
0 ignored issues
show
Deprecated Code introduced by
The function OCP\IConfig::deleteUserValue() has been deprecated: 33.0.0 - use {@see IUserConfig::deleteUserConfig} directly ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

45
		/** @scrutinizer ignore-deprecated */ $this->config->deleteUserValue($user->getUID(), Application::APP_ID, $gatewayKey);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
46
	}
47
48 4
	public function get(IUser $user, string $gatewayName): State {
49 4
		$isVerified = $this->getUserValue($user, $gatewayName, 'verified', 'false') === 'true';
50 4
		$identifier = $this->getUserValue($user, $gatewayName, 'identifier');
51 4
		$verificationCode = $this->getUserValue($user, $gatewayName, 'verification_code');
52
53 4
		if ($isVerified) {
54 1
			$state = StateStorage::STATE_ENABLED;
55 3
		} elseif ($identifier !== '' && $verificationCode !== '') {
56 1
			$state = StateStorage::STATE_VERIFYING;
57
		} else {
58 2
			$state = StateStorage::STATE_DISABLED;
59
		}
60
61 4
		return new State(
62 4
			$user,
63 4
			$state,
64 4
			$gatewayName,
65 4
			$identifier,
66 4
			$verificationCode
67 4
		);
68
	}
69
70 2
	public function persist(State $state): State {
71 2
		switch ($state->getState()) {
72
			case StateStorage::STATE_DISABLED:
73
				$this->deleteUserValue(
74
					$state->getUser(),
75
					$state->getGatewayName(),
76
					'verified'
77
				);
78
				$this->deleteUserValue(
79
					$state->getUser(),
80
					$state->getGatewayName(),
81
					'verification_code'
82
				);
83
84
				break;
85
			case StateStorage::STATE_VERIFYING:
86 1
				$this->setUserValue(
87 1
					$state->getUser(),
88 1
					$state->getGatewayName(),
89 1
					'identifier',
90 1
					$state->getIdentifier()
91 1
				);
92 1
				$this->setUserValue(
93 1
					$state->getUser(),
94 1
					$state->getGatewayName(),
95 1
					'verification_code',
96 1
					$state->getVerificationCode()
97 1
				);
98 1
				$this->setUserValue(
99 1
					$state->getUser(),
100 1
					$state->getGatewayName(),
101 1
					'verified',
102 1
					'false'
103 1
				);
104
105 1
				break;
106
			case StateStorage::STATE_ENABLED:
107 1
				$this->setUserValue(
108 1
					$state->getUser(),
109 1
					$state->getGatewayName(),
110 1
					'identifier',
111 1
					$state->getIdentifier()
112 1
				);
113 1
				$this->setUserValue(
114 1
					$state->getUser(),
115 1
					$state->getGatewayName(),
116 1
					'verification_code',
117 1
					$state->getVerificationCode()
118 1
				);
119 1
				$this->setUserValue(
120 1
					$state->getUser(),
121 1
					$state->getGatewayName(),
122 1
					'verified',
123 1
					'true'
124 1
				);
125
126 1
				break;
127
			default:
128
				throw new Exception('invalid provider state');
129
		}
130
131 2
		return $state;
132
	}
133
}
134