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