Completed
Push — master ( 947279...3626a2 )
by Christoph
05:44
created

Gateway::getChatId()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 26
rs 9.7333
c 0
b 0
f 0
cc 4
nc 2
nop 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A Gateway::getConfig() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Christoph Wurst <[email protected]>
7
 * @author André Fondse <[email protected]>
8
 *
9
 * Nextcloud - Two-factor Gateway for Telegram
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\TwoFactorGateway\Service\Gateway\Telegram;
26
27
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
28
use OCA\TwoFactorGateway\Service\Gateway\IGateway;
29
use OCA\TwoFactorGateway\Service\Gateway\IGatewayConfig;
30
use OCP\Http\Client\IClient;
31
use OCP\Http\Client\IClientService;
32
use OCP\IConfig;
33
use OCP\ILogger;
34
use OCP\IUser;
35
use Telegram\Bot\Api;
36
use Telegram\Bot\Exceptions\TelegramSDKException;
37
use Telegram\Bot\Objects\Update;
38
39
class Gateway implements IGateway {
40
41
	/** @var IClient */
42
	private $client;
43
44
	/** @var GatewayConfig */
45
	private $gatewayConfig;
46
47
	/** @var IConfig */
48
	private $config;
49
50
	/** @var ILogger */
51
	private $logger;
52
53
	public function __construct(IClientService $clientService,
54
								GatewayConfig $gatewayConfig,
55
								IConfig $config,
56
								ILogger $logger) {
57
		$this->client = $clientService->newClient();
58
		$this->gatewayConfig = $gatewayConfig;
59
		$this->config = $config;
60
		$this->logger = $logger;
61
	}
62
63
	/**
64
	 * @param IUser $user
65
	 * @param string $identifier
66
	 * @param string $message
67
	 *
68
	 * @throws SmsTransmissionException
69
	 */
70
	public function send(IUser $user, string $identifier, string $message) {
71
		$this->logger->debug("sending telegram message to $identifier, message: $message");
72
		$botToken = $this->gatewayConfig->getBotToken();
73
		$this->logger->debug("telegram bot token: $botToken");
74
75
		$api = new Api($botToken);
76
77
		$this->logger->debug("sending telegram message to $identifier");
78
		try {
79
			$api->sendMessage([
80
				'chat_id' => $identifier,
81
				'text' => $message,
82
			]);
83
		} catch (TelegramSDKException $e) {
84
			$this->logger->logException($e);
85
86
			throw new SmsTransmissionException($e);
87
		}
88
		$this->logger->debug("telegram message to chat $identifier sent");
89
	}
90
91
	/**
92
	 * Get the gateway-specific configuration
93
	 *
94
	 * @return IGatewayConfig
95
	 */
96
	public function getConfig(): IGatewayConfig {
97
		return $this->gatewayConfig;
98
	}
99
100
}
101