Login   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
eloc 31
dl 0
loc 46
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A execute() 0 33 5
1
#!/usr/bin/env php
2
<?php
3
4
declare(strict_types=1);
5
6
/**
7
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
8
 * SPDX-License-Identifier: AGPL-3.0-or-later
9
 */
10
11
namespace OCA\TwoFactorGateway\Provider\Channel\Telegram\Provider\Drivers\ClientCli;
12
13
use danog\MadelineProto\API;
14
use danog\MadelineProto\Settings;
15
use danog\MadelineProto\Settings\AppInfo;
16
use danog\MadelineProto\Settings\Logger;
17
use Symfony\Component\Console\Attribute\AsCommand;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
#[AsCommand(name: 'telegram:login', description: 'Login into Telegram using the Client API')]
24
class Login extends Command {
25
26
	protected function configure(): void {
27
		$this
28
			->addOption(
29
				'session-directory',
30
				's',
31
				InputOption::VALUE_REQUIRED,
32
				'Directory to store the session files',
33
			);
34
	}
35
36
	protected function execute(InputInterface $input, OutputInterface $output): int {
37
		$sessionDirectory = $input->getOption('session-directory');
38
39
		$appInfo = new AppInfo();
40
		$appInfo->setDeviceModel('Nextcloud-TwoFactor-Gateway');
41
		if ($apiId = getenv('TELEGRAM_API_ID')) {
42
			$appInfo->setApiId((int)$apiId);
43
		}
44
		if ($apiHash = getenv('TELEGRAM_API_HASH')) {
45
			$appInfo->setApiHash($apiHash);
46
		}
47
48
		try {
49
			$settings = (new Settings())
50
				->setAppInfo($appInfo)
51
				->setLogger((new Logger())->setExtra($sessionDirectory . '/MadelineProto.log'));
52
53
			$api = new API($sessionDirectory, $settings);
54
55
			if (empty(getenv('TELEGRAM_API_ID'))) {
56
				$api->start();
57
				$output->writeln(<<<MESSAGE
58
59
					<info>Telegram login successful! Run again the twofactor setup command to finish configuration.</info>
60
61
					MESSAGE);
62
			}
63
64
65
			return Command::SUCCESS;
66
		} catch (\Throwable $e) {
67
			$output->writeln('<error>Error: ' . $e->getMessage() . '</error>');
68
			return Command::FAILURE;
69
		}
70
	}
71
}
72