Passed
Pull Request — master (#640)
by Vitor
03:39
created

AGateway::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 0
dl 0
loc 3
rs 10
c 1
b 1
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
7
 * SPDX-License-Identifier: AGPL-3.0-or-later
8
 */
9
10
namespace OCA\TwoFactorGateway\Provider\Gateway;
11
12
use OCA\TwoFactorGateway\AppInfo\Application;
13
use OCA\TwoFactorGateway\Exception\MessageTransmissionException;
14
use OCP\IAppConfig;
15
use OCP\IUser;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
abstract class AGateway implements IGateway {
20
	use TConfigurable;
21
	public const SCHEMA = [];
22
23
	public function __construct(
24
		public IAppConfig $appConfig,
25
	) {
26
	}
27
28
	/**
29
	 * @throws MessageTransmissionException
30
	 */
31
	#[\Override]
32
	abstract public function send(IUser $user, string $identifier, string $message, array $extra = []): void;
33
34
	#[\Override]
35
	public function isComplete(array $schema = []): bool {
36
		if (empty($schema)) {
37
			$schema = static::SCHEMA;
38
		}
39
		$set = $this->appConfig->getKeys(Application::APP_ID);
40
		$fields = array_column($schema['fields'], 'field');
41
		$fields = array_map(fn ($f) => $this->getProviderId() . '_' . $f, $fields);
42
		$intersect = array_intersect($fields, $set);
43
		return count($intersect) === count($fields);
44
	}
45
46
	#[\Override]
47
	public function getSettings(): array {
48
		$settings = [];
49
		if (isset(static::SCHEMA['instructions'])) {
50
			$settings['instructions'] = static::SCHEMA['instructions'];
51
		}
52
		$settings['name'] = static::SCHEMA['name'];
53
		return $settings;
54
	}
55
56
	#[\Override]
57
	abstract public function cliConfigure(InputInterface $input, OutputInterface $output): int;
58
59
	#[\Override]
60
	public function remove(array $schema = []): void {
61
		if (empty($schema)) {
62
			$schema = static::SCHEMA;
63
		}
64
		foreach ($schema['fields'] as $field) {
65
			$method = 'delete' . $this->toCamel($field['field']);
66
			$this->{$method}();
67
		}
68
	}
69
70
	protected function toCamel(string $field): string {
71
		return str_replace(' ', '', ucwords(str_replace('_', ' ', $field)));
72
	}
73
74
	#[\Override]
75
	public static function getProviderId(): string {
76
		$id = self::deriveIdFromFqcn(static::class);
77
		if ($id === null) {
78
			throw new \LogicException('Cannot derive gateway id from FQCN: ' . static::class);
79
		}
80
		return $id;
81
	}
82
83
	private static function deriveIdFromFqcn(string $fqcn): ?string {
84
		$prefix = 'OCA\\TwoFactorGateway\\Provider\\Channel\\';
85
		if (strncmp($fqcn, $prefix, strlen($prefix)) !== 0) {
86
			return null;
87
		}
88
		$rest = substr($fqcn, strlen($prefix));
89
		$sep = strpos($rest, '\\');
90
		if ($sep === false || substr($rest, $sep + 1) !== 'Gateway') {
91
			return null;
92
		}
93
		$type = substr($rest, 0, $sep);
94
		return $type !== '' ? strtolower($type) : null;
95
	}
96
}
97