Passed
Pull Request — master (#778)
by
unknown
08:24
created

Gateway::getSessionStart()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.9332
c 1
b 0
f 1
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7
 * SPDX-License-Identifier: AGPL-3.0-or-later
8
 */
9
10
namespace OCA\TwoFactorGateway\Provider\Channel\WhatsApp;
11
12
use OCA\TwoFactorGateway\Exception\ConfigurationException;
13
use OCA\TwoFactorGateway\Provider\Channel\WhatsApp\Config\DriverFactory;
14
use OCA\TwoFactorGateway\Provider\Channel\WhatsApp\Drivers\CloudApiDriver;
15
use OCA\TwoFactorGateway\Provider\Channel\WhatsApp\Drivers\IWhatsAppDriver;
16
use OCA\TwoFactorGateway\Provider\Gateway\AGateway;
17
use OCA\TwoFactorGateway\Provider\Settings;
18
use OCP\Http\Client\IClientService;
19
use OCP\IAppConfig;
20
use OCP\IConfig;
21
use Psr\Log\LoggerInterface;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
/**
26
 * Gateway refatorado que delega para drivers específicos via Factory pattern
27
 * Suporta múltiplos drivers: CloudApiDriver e WebSocketDriver
28
 */
29
class Gateway extends AGateway {
30
	private ?IWhatsAppDriver $driver = null;
31
	private DriverFactory $factory;
32
33
	public function __construct(
34
		public IAppConfig $appConfig,
35
		private IConfig $config,
36
		private IClientService $clientService,
37
		private LoggerInterface $logger,
38
	) {
39
		parent::__construct($appConfig);
40
		$this->factory = new DriverFactory(
41
			$appConfig,
42 1
			$config,
43
			$clientService,
44
			$logger,
45
		);
46
	}
47
48
	#[\Override]
49 1
	public function createSettings(): Settings {
50 1
		// Retorna as configurações do driver ativo ou configurações vazias se não houver driver
51 1
		$driver = $this->factory->create();
52
		if ($driver === null) {
53
			// Retorna Settings vazio se nenhum driver for configurado
54 1
			return new Settings(
55
				name: 'WhatsApp Cloud API',
56 1
				id: 'whatsapp',
57 1
				instructions: 'Send two-factor authentication codes via WhatsApp',
58 1
				fields: [],
59 1
			);
60 1
		}
61 1
		return $driver->getSettings();
62 1
	}
63 1
64 1
	#[\Override]
65 1
	public function send(string $identifier, string $message, array $extra = []): void {
66
		// Delega o envio para o driver
67
		$driver = $this->factory->create();
68
		if ($driver === null) {
69
			throw new ConfigurationException('WhatsApp is not configured');
70
		}
71
		$driver->send($identifier, $message, $extra);
72
	}
73
74
	#[\Override]
75
	public function cliConfigure(InputInterface $input, OutputInterface $output): int {
76
		// Se nenhum driver está configurado, use o CloudApiDriver como padrão
77
		$driver = $this->factory->create();
78
		if ($driver === null) {
79
			// Cria um CloudApiDriver para configuração inicial
80
			$driver = new CloudApiDriver($this->appConfig, $this->clientService, $this->logger);
81
		}
82
		return $driver->cliConfigure($input, $output);
83
	}
84
85
	/**
86
	 * Obtém a instância do driver, inicializando se necessário
87
	 *
88
	 * @throws ConfigurationException se nenhum driver conseguir ser criado
89
	 */
90
	private function getDriver(): IWhatsAppDriver {
0 ignored issues
show
Unused Code introduced by
The method getDriver() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
91
		if ($this->driver === null) {
92
			try {
93
				$created = $this->factory->create();
94
				if ($created === null) {
95
					throw new ConfigurationException('No WhatsApp driver configuration found');
96
				}
97
				$this->driver = $created;
98
			} catch (ConfigurationException $e) {
99
				$this->logger->error('Failed to create WhatsApp driver', ['exception' => $e]);
100
				throw $e;
101
			}
102
		}
103
104
		return $this->driver;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->driver could return the type null which is incompatible with the type-hinted return OCA\TwoFactorGateway\Pro...Drivers\IWhatsAppDriver. Consider adding an additional type-check to rule them out.
Loading history...
105
	}
106
}
107