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\Provider\Channel\Telegram; |
11
|
|
|
|
12
|
|
|
use OCA\TwoFactorGateway\AppInfo\Application; |
13
|
|
|
use OCA\TwoFactorGateway\Exception\ConfigurationException; |
14
|
|
|
use OCA\TwoFactorGateway\Provider\Channel\Telegram\Provider\IProvider; |
15
|
|
|
use OCA\TwoFactorGateway\Provider\Gateway\AGateway; |
16
|
|
|
use OCP\IAppConfig; |
17
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
21
|
|
|
|
22
|
|
|
class Gateway extends AGateway { |
23
|
|
|
public const SCHEMA = [ |
24
|
|
|
'name' => 'Telegram', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
public IAppConfig $appConfig, |
29
|
|
|
private Factory $providerFactory, |
30
|
|
|
) { |
31
|
|
|
parent::__construct($appConfig); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
#[\Override] |
35
|
|
|
public function send(string $identifier, string $message, array $extra = []): void { |
36
|
|
|
$this->getProvider()->send($identifier, $message); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
#[\Override] |
40
|
|
|
final public function cliConfigure(InputInterface $input, OutputInterface $output): int { |
41
|
|
|
$namespaces = $this->providerFactory->getFqcnList(); |
42
|
|
|
$schemas = []; |
43
|
|
|
foreach ($namespaces as $ns) { |
44
|
|
|
$schemas[] = $ns::SCHEMA; |
45
|
|
|
} |
46
|
|
|
$names = array_column($schemas, 'name'); |
47
|
|
|
|
48
|
|
|
$helper = new QuestionHelper(); |
49
|
|
|
$choiceQuestion = new ChoiceQuestion('Please choose a Telegram provider:', $names); |
50
|
|
|
$name = $helper->ask($input, $output, $choiceQuestion); |
51
|
|
|
$selectedIndex = array_search($name, $names); |
52
|
|
|
$schema = $schemas[$selectedIndex]; |
|
|
|
|
53
|
|
|
|
54
|
|
|
$provider = $this->getProvider($namespaces[$selectedIndex]::getProviderId()); |
55
|
|
|
|
56
|
|
|
$provider->cliConfigure($input, $output); |
57
|
|
|
return 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
#[\Override] |
61
|
|
|
public function getSettings(): array { |
62
|
|
|
try { |
63
|
|
|
$provider = $this->getProvider(); |
64
|
|
|
} catch (ConfigurationException) { |
65
|
|
|
return static::SCHEMA; |
66
|
|
|
} |
67
|
|
|
return $provider::SCHEMA; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
#[\Override] |
71
|
|
|
public function isComplete(array $schema = []): bool { |
72
|
|
|
if (empty($schema)) { |
73
|
|
|
try { |
74
|
|
|
$provider = $this->getProvider(); |
75
|
|
|
} catch (ConfigurationException) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
$schema = $provider::SCHEMA; |
79
|
|
|
} |
80
|
|
|
return parent::isComplete($schema); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
#[\Override] |
84
|
|
|
public function remove(array $schema = []): void { |
85
|
|
|
if (empty($schema)) { |
86
|
|
|
$schema = static::SCHEMA; |
87
|
|
|
} |
88
|
|
|
parent::remove($schema); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getProvider(string $providerName = ''): IProvider { |
92
|
|
|
if ($providerName) { |
93
|
|
|
$this->setProvider($providerName); |
94
|
|
|
} |
95
|
|
|
$providerName = $this->appConfig->getValueString(Application::APP_ID, 'telegram_provider_name'); |
96
|
|
|
if ($providerName === '') { |
97
|
|
|
throw new ConfigurationException(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->providerFactory->get($providerName); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function setProvider(string $provider): void { |
104
|
|
|
$this->appConfig->setValueString(Application::APP_ID, 'telegram_provider_name', $provider); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|