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

DriverFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 29
c 1
b 0
f 0
dl 0
loc 74
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 18 5
A instantiateDriver() 0 14 1
A getStoredConfig() 0 9 1
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\Config;
11
12
use OCA\TwoFactorGateway\Exception\ConfigurationException;
13
use OCA\TwoFactorGateway\Provider\Channel\WhatsApp\Drivers\CloudApiDriver;
14
use OCA\TwoFactorGateway\Provider\Channel\WhatsApp\Drivers\IWhatsAppDriver;
15
use OCA\TwoFactorGateway\Provider\Channel\WhatsApp\Drivers\WebSocketDriver;
16
use OCP\Http\Client\IClientService;
17
use OCP\IAppConfig;
18
use OCP\IConfig;
19
use Psr\Log\LoggerInterface;
20
21
/**
22
 * Factory para detectar e instanciar o driver de WhatsApp apropriado
23
 */
24
class DriverFactory {
25
	private const DRIVERS = [
26
		CloudApiDriver::class,
27
		WebSocketDriver::class,
28
	];
29
30
	public function __construct(
31
		private IAppConfig $appConfig,
32
		private IConfig $config,
33
		private IClientService $clientService,
34
		private LoggerInterface $logger,
35
	) {}
36
37
	/**
38
	 * Cria instância do driver apropriado baseado na configuração armazenada
39
	 * Retorna null se nenhuma configuração for encontrada
40
	 *
41
	 * @throws ConfigurationException se nenhum driver conseguir ser detectado/instanciado
42
	 */
43
	public function create(): ?IWhatsAppDriver {
44
		$storedConfig = $this->getStoredConfig();
45
46
		// Se nenhuma configuração foi fornecida, retorna null
47
		if (empty($storedConfig['api_key']) && empty($storedConfig['base_url'])) {
48
			return null;
49
		}
50
51
		// Tenta detectar qual driver deve ser usado
52
		foreach (self::DRIVERS as $driverClass) {
53
			if ($driverClass::detectDriver($storedConfig) !== null) {
54
				return $this->instantiateDriver($driverClass);
55
			}
56
		}
57
58
		// Se nenhum driver for detectado, lança exceção
59
		throw new ConfigurationException(
60
			'No WhatsApp driver configuration found. Please configure one.'
61
		);
62
	}
63
64
	/**
65
	 * Instancia um driver específico
66
	 */
67
	private function instantiateDriver(string $driverClass): IWhatsAppDriver {
68
		return match ($driverClass) {
69
			CloudApiDriver::class => new CloudApiDriver(
70
				$this->appConfig,
71
				$this->clientService,
72
				$this->logger,
73
			),
74
			WebSocketDriver::class => new WebSocketDriver(
75
				$this->appConfig,
76
				$this->config,
77
				$this->clientService,
78
				$this->logger,
79
			),
80
			default => throw new ConfigurationException("Unknown driver: $driverClass"),
81
		};
82
	}
83
84
	/**
85
	 * Obtém a configuração armazenada de ambos os drivers
86
	 *
87
	 * @return array<string, string|null>
88
	 */
89
	private function getStoredConfig(): array {
90
		return [
91
			// Cloud API
92
			'api_key' => $this->appConfig->getValueString('twofactor_gateway', 'whatsapp_cloud_api_key', ''),
93
			'phone_number_id' => $this->appConfig->getValueString('twofactor_gateway', 'whatsapp_cloud_phone_number_id', ''),
94
			'business_account_id' => $this->appConfig->getValueString('twofactor_gateway', 'whatsapp_cloud_business_account_id', ''),
95
			'api_endpoint' => $this->appConfig->getValueString('twofactor_gateway', 'whatsapp_cloud_api_endpoint', ''),
96
			// WebSocket
97
			'base_url' => $this->appConfig->getValueString('twofactor_gateway', 'whatsapp_base_url', ''),
98
		];
99
	}
100
}
101