Skip to content

Commit 6710210

Browse files
fix: address code review feedback (Vonage domain, from formatting, test coverage)
1 parent 2af52bc commit 6710210

3 files changed

Lines changed: 45 additions & 6 deletions

File tree

src/Utopia/Messaging/Adapter/SMS/VonageMessages.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ class VonageMessages extends SMSAdapter
2626
* @param string $apiSecret Vonage API Secret
2727
*/
2828
public function __construct(
29-
/** @phpstan-ignore property.onlyWritten */
3029
private string $apiKey,
31-
/** @phpstan-ignore property.onlyWritten */
3230
private string $apiSecret,
3331
private ?string $from = null
3432
) {
@@ -54,6 +52,13 @@ protected function process(SMSMessage $message): array
5452

5553
$response = new Response($this->getType());
5654

55+
if (empty($from)) {
56+
$response->addResult($message->getTo()[0], 'The "from" field is required for the Vonage Messages API.');
57+
return $response->toArray();
58+
}
59+
60+
$from = \ltrim($from, '+');
61+
5762
$result = $this->request(
5863
method: 'POST',
5964
url: $this->getApiEndpoint(),

src/Utopia/Messaging/Adapter/VonageMessagesBase.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ trait VonageMessagesBase
2121
*/
2222
protected function getApiEndpoint(): string
2323
{
24-
return 'https://api.nexmo.com/v1/messages';
24+
return 'https://api.vonage.com/v1/messages';
2525
}
2626

27+
/**
28+
* @todo Implement JWT authentication for non-SMS channels
29+
*/
2730
protected function getAuthorizationHeader(): string
2831
{
2932
return 'Basic ' . \base64_encode("{$this->apiKey}:{$this->apiSecret}");

tests/Messaging/Adapter/SMS/VonageMessagesTest.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ public function testSendSMS(): void
1515
{
1616
$apiKey = \getenv('VONAGE_API_KEY');
1717
$apiSecret = \getenv('VONAGE_API_SECRET');
18+
$to = \getenv('VONAGE_TO');
1819

19-
if (!$apiKey || !$apiSecret) {
20-
$this->markTestSkipped('Vonage Messages credentials are not available.');
20+
if (!$apiKey || !$apiSecret || !$to) {
21+
$this->markTestSkipped('Vonage Messages credentials or recipient are not available.');
2122
}
2223

2324
$sender = new VonageMessages(
@@ -27,7 +28,7 @@ public function testSendSMS(): void
2728
);
2829

2930
$message = new SMS(
30-
to: [\getenv('VONAGE_TO')],
31+
to: [$to],
3132
content: 'Test Content',
3233
from: \getenv('VONAGE_FROM')
3334
);
@@ -36,4 +37,34 @@ public function testSendSMS(): void
3637

3738
$this->assertResponse($response);
3839
}
40+
41+
/**
42+
* @throws \Exception
43+
*/
44+
public function testSendSMSWithFallbackFrom(): void
45+
{
46+
$apiKey = \getenv('VONAGE_API_KEY');
47+
$apiSecret = \getenv('VONAGE_API_SECRET');
48+
$to = \getenv('VONAGE_TO');
49+
$from = \getenv('VONAGE_FROM');
50+
51+
if (!$apiKey || !$apiSecret || !$to || !$from) {
52+
$this->markTestSkipped('Vonage Messages credentials or sender/recipient are not available.');
53+
}
54+
55+
$sender = new VonageMessages(
56+
apiKey: $apiKey,
57+
apiSecret: $apiSecret,
58+
);
59+
60+
$message = new SMS(
61+
to: [$to],
62+
content: 'Test Content',
63+
from: $from
64+
);
65+
66+
$response = $sender->send($message);
67+
68+
$this->assertResponse($response);
69+
}
3970
}

0 commit comments

Comments
 (0)