Passed
Pull Request — master (#671)
by Vitor
08:37
created

Client::send()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 20
rs 9.7998
cc 3
nc 4
nop 3
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\Provider\Drivers;
11
12
use OCA\TwoFactorGateway\Exception\MessageTransmissionException;
13
use OCA\TwoFactorGateway\Provider\Channel\Telegram\Provider\AProvider;
14
use OCA\TwoFactorGateway\Vendor\TelegramBot\Api\BotApi;
0 ignored issues
show
Bug introduced by
The type OCA\TwoFactorGateway\Vendor\TelegramBot\Api\BotApi was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use OCA\TwoFactorGateway\Vendor\TelegramBot\Api\Exception as TelegramSDKException;
0 ignored issues
show
Bug introduced by
The type OCA\TwoFactorGateway\Ven...legramBot\Api\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use OCP\IL10N;
17
use Psr\Log\LoggerInterface;
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\Question;
22
23
/**
24
 * @method string getBotToken()
25
 * @method static setBotToken(string $botToken)
26
 */
27
class Client extends AProvider {
28
	public const SCHEMA = [
29
		'name' => 'Telegram Client API',
30
		'instructions' => <<<HTML
31
			<p>Enter your Telegram number or username to receive your verification code below.</p>
32
			HTML,
33
		'fields' => [
34
			['field' => 'bot_token', 'prompt' => 'Please enter your Telegram bot token:'],
35
		],
36
	];
37
	public function __construct(
38
		private LoggerInterface $logger,
39
		private IL10N $l10n,
40
	) {
41
	}
42
43
	#[\Override]
44
	public function send(string $identifier, string $message, array $extra = []): void {
45
		if (empty($message)) {
46
			$message = $this->l10n->t('`%s` is your Nextcloud verification code.', [$extra['code']]);
47
		}
48
		$this->logger->debug("sending telegram message to $identifier, message: $message");
49
		$botToken = $this->getBotToken();
50
		$this->logger->debug("telegram bot token: $botToken");
51
52
		$api = new BotApi($botToken);
53
54
		$this->logger->debug("sending telegram message to $identifier");
55
		try {
56
			$api->sendMessage($identifier, $message, parseMode: 'markdown');
57
		} catch (TelegramSDKException $e) {
58
			$this->logger->error($e);
59
60
			throw new MessageTransmissionException($e->getMessage());
61
		}
62
		$this->logger->debug("telegram message to chat $identifier sent");
63
	}
64
65
	#[\Override]
66
	public function cliConfigure(InputInterface $input, OutputInterface $output): int {
67
		// if (PHP_VERSION_ID < 80200) {
68
		// 	$output->writeln('The Telegram Client API provider requires PHP 8.2 or higher.');
69
70
		// 	return 1;
71
		// }
72
73
		require __DIR__ . '/../../../../../../vendor-bin/telegram-client/vendor/autoload.php';
74
		// $helper = new QuestionHelper();
75
		// $tokenQuestion = new Question(self::SCHEMA['fields'][0]['prompt'] . ' ');
76
		// $token = $helper->ask($input, $output, $tokenQuestion);
77
		// $this->setBotToken($token);
78
		// $output->writeln("Using $token.");
79
80
		// $this->setBotToken($token);
81
		return 0;
82
	}
83
}
84