Passed
Pull Request — master (#92)
by Christoph
03:17
created

Gateway::getProviderDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\IUser;
34
use Telegram\Bot\Api;
35
use Telegram\Bot\Objects\Update;
36
37
class Gateway implements IGateway {
38
39
	/** @var IClient */
40
	private $client;
41
42
	/** @var GatewayConfig */
43
	private $gatewayConfig;
44
45
	/** @var IConfig */
46
	private $config;
47
48
	public function __construct(IClientService $clientService,
49
								GatewayConfig $gatewayConfig,
50
								IConfig $config) {
51
		$this->client = $clientService->newClient();
52
		$this->gatewayConfig = $gatewayConfig;
53
		$this->config = $config;
54
	}
55
56
	/**
57
	 * @param IUser $user
58
	 * @param string $identifier
59
	 * @param string $message
60
	 *
61
	 * @throws SmsTransmissionException
62
	 */
63
	public function send(IUser $user, string $identifier, string $message) {
64
		$botToken = $this->gatewayConfig->getBotToken();
65
66
		$api = new Api($botToken);
67
		$chatId = $this->getChatId($user, $api, (int)$identifier);
68
69
		$api->sendMessage([
70
			'chat_id' => $chatId,
71
			'text' => $message,
72
		]);
73
	}
74
75
	private function getChatId(IUser $user, Api $api, int $userId): int {
76
		$chatId = $this->config->getUserValue($user->getUID(), 'twofactor_gateway', 'telegram_chat_id', null);
77
78
		if (!is_null($chatId)) {
0 ignored issues
show
introduced by
The condition is_null($chatId) is always false.
Loading history...
79
			return (int)$chatId;
80
		}
81
82
		$updates = $api->getUpdates();
83
		/** @var Update $update */
84
		$update = current(array_filter($updates, function (Update $data) use ($userId) {
85
			if ($data->message->text === "/start" && $data->message->from->id === $userId) {
86
				return true;
87
			}
88
			return false;
89
		}));
90
		// TODO: handle missing `/start` message and `$update` null values
91
92
		$chatId = $update->message->chat->id;
93
		$this->config->setUserValue($user->getUID(), 'twofactor_gateway', 'chat_id', $chatId);
94
95
		return (int)$chatId;
96
	}
97
98
	/**
99
	 * Get the gateway-specific configuration
100
	 *
101
	 * @return IGatewayConfig
102
	 */
103
	public function getConfig(): IGatewayConfig {
104
		return $this->gatewayConfig;
105
	}
106
107
}
108