Skip to content

Commit 1c60bee

Browse files
authored
Merge pull request #1155 from nextcloud/backport/1153/stable33
[stable33] feat: add direct gateway integration service
2 parents 0f8b987 + 65e8ba0 commit 1c60bee

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\TwoFactorGateway\Service;
11+
12+
use OCA\TwoFactorGateway\Exception\MessageTransmissionException;
13+
14+
/**
15+
* Stable direct-integration path for other apps that only know the gateway id
16+
* and destination identifier, and do not need user routing or instance
17+
* selection details.
18+
*/
19+
class GatewayDirectIntegrationService {
20+
public function __construct(
21+
private GatewayRuntimeAvailabilityService $gatewayRuntimeAvailabilityService,
22+
) {
23+
}
24+
25+
public function ensureAvailable(string $gatewayId): void {
26+
$this->gatewayRuntimeAvailabilityService->getGateway($gatewayId);
27+
}
28+
29+
public function isGatewayComplete(string $gatewayId): bool {
30+
return $this->gatewayRuntimeAvailabilityService->hasDirectGatewayFallback($gatewayId);
31+
}
32+
33+
/**
34+
* @param array<string, mixed> $extra
35+
* @throws MessageTransmissionException
36+
*/
37+
public function send(string $gatewayId, string $identifier, string $message, array $extra = []): void {
38+
$this->gatewayRuntimeAvailabilityService->getGateway($gatewayId)->send($identifier, $message, $extra);
39+
}
40+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\TwoFactorGateway\Tests\Unit\Service;
11+
12+
use OCA\TwoFactorGateway\Provider\Gateway\IGateway;
13+
use OCA\TwoFactorGateway\Service\GatewayDirectIntegrationService;
14+
use OCA\TwoFactorGateway\Service\GatewayRuntimeAvailabilityService;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class GatewayDirectIntegrationServiceTest extends TestCase {
19+
private GatewayRuntimeAvailabilityService&MockObject $gatewayRuntimeAvailabilityService;
20+
private GatewayDirectIntegrationService $service;
21+
22+
protected function setUp(): void {
23+
parent::setUp();
24+
$this->gatewayRuntimeAvailabilityService = $this->createMock(GatewayRuntimeAvailabilityService::class);
25+
$this->service = new GatewayDirectIntegrationService($this->gatewayRuntimeAvailabilityService);
26+
}
27+
28+
public function testEnsureAvailableDelegatesToRuntimeAvailabilityService(): void {
29+
$gateway = $this->createMock(IGateway::class);
30+
31+
$this->gatewayRuntimeAvailabilityService->expects($this->once())
32+
->method('getGateway')
33+
->with('sms')
34+
->willReturn($gateway);
35+
36+
$this->service->ensureAvailable('sms');
37+
}
38+
39+
public function testIsGatewayCompleteDelegatesToRuntimeAvailabilityService(): void {
40+
$this->gatewayRuntimeAvailabilityService->expects($this->once())
41+
->method('hasDirectGatewayFallback')
42+
->with('signal')
43+
->willReturn(true);
44+
45+
$this->assertTrue($this->service->isGatewayComplete('signal'));
46+
}
47+
48+
public function testSendDelegatesToResolvedGateway(): void {
49+
$gateway = $this->createMock(IGateway::class);
50+
$gateway->expects($this->once())
51+
->method('send')
52+
->with('+5511999999999', 'hello', []);
53+
54+
$this->gatewayRuntimeAvailabilityService->expects($this->once())
55+
->method('getGateway')
56+
->with('whatsapp')
57+
->willReturn($gateway);
58+
59+
$this->service->send('whatsapp', '+5511999999999', 'hello');
60+
}
61+
}

0 commit comments

Comments
 (0)