Skip to content

Commit 1beee09

Browse files
authored
feat: added message failover (#540)
1 parent 5a4a6b8 commit 1beee09

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
trim_trailing_whitespace = true
7+
max_line_length = 80
8+
indent_style = space
9+
indent_size = 4
10+
charset = utf-8

.phpactor.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "/phpactor.schema.json",
3+
"language_server_phpstan.enabled": false,
4+
"php_code_sniffer.enabled": false,
5+
"prophecy.enabled": false
6+
}

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"$schema": "https://getcomposer.org/schema.json",
23
"name": "vonage/client-core",
34
"type": "library",
45
"description": "PHP Client for using Vonage's API.",
@@ -9,6 +10,11 @@
910
"name": "James Seconde",
1011
"email": "jim.seconde@vonage.com",
1112
"role": "PHP Developer Advocate"
13+
},
14+
{
15+
"name": "Chuck \"MANCHUCK\" Reeves",
16+
"homepage": "https://github.com/manchuck",
17+
"role": "Sr Developer Advocate"
1218
}
1319
],
1420
"require": {
@@ -22,7 +28,7 @@
2228
"psr/log": "^1.1|^2.0|^3.0",
2329
"vonage/jwt": "^0.5.0"
2430
},
25-
"require-dev": {
31+
"require-dev": {
2632
"guzzlehttp/guzzle": ">=6",
2733
"helmich/phpunit-json-assert": "^3.3",
2834
"php-http/mock-client": "^1.4",

src/Messages/Channel/BaseMessage.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ abstract class BaseMessage implements Message
1111
protected ?string $clientRef = null;
1212
protected ?string $webhookUrl = null;
1313
protected ?string $webhookVersion = null;
14+
protected ?array $failover = null;
1415

1516
protected array $permittedVersions = [
1617
'v0.1',
@@ -91,6 +92,17 @@ public function getWebhookVersion(): ?string
9192
return $this->webhookVersion;
9293
}
9394

95+
public function addFailover(Message $message): void
96+
{
97+
if (!isset($this->failover)) {
98+
$this->failover = [];
99+
}
100+
array_push(
101+
$this->failover,
102+
$message
103+
);
104+
}
105+
94106
public function getBaseMessageUniversalOutputArray(): array
95107
{
96108
$returnArray = [
@@ -112,6 +124,11 @@ public function getBaseMessageUniversalOutputArray(): array
112124
$returnArray['webhook_version'] = $this->getWebhookVersion();
113125
}
114126

127+
if (isset($this->failover)) {
128+
$callback = fn(Message $message): array => $message->toArray();
129+
$returnArray['failover'] = array_map($callback, $this->failover);
130+
}
131+
115132
return $returnArray;
116133
}
117134
}

src/Messages/Channel/Message.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public function setWebhookUrl(string $url): void;
2020
public function getWebhookVersion(): ?string;
2121
public function setWebhookVersion(string $version): void;
2222
public function validatesE164(): bool;
23+
public function addFailover(Message $message): void;
24+
2325

2426
/**
2527
* All message types have shared outputs required by the endpoint.

test/Messages/ClientTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,38 @@ public function testCanSendSMSWithOptionalFields(): void
166166
$this->assertArrayHasKey('message_uuid', $result);
167167
}
168168

169+
public function testCanSendSMSWtihFailover(): void
170+
{
171+
$payload = [
172+
'to' => '447700900000',
173+
'from' => '16105551212',
174+
'text' => 'Reticulating Splines',
175+
'message_type' => 'text',
176+
'channel' => 'sms',
177+
'failover' => [
178+
[
179+
'message_type' => 'text',
180+
'to' => '447700900000',
181+
'from' => '16105551212',
182+
'channel' => 'sms',
183+
'text' => 'Reticulating Splines',
184+
]
185+
]
186+
];
187+
188+
$message = new SMSText($payload['to'], $payload['from'], $payload['text']);
189+
$failMessage = new SMSText($payload['to'], $payload['from'], $payload['text']);
190+
$message->addFailover($failMessage);
191+
192+
$this->vonageClient->send(Argument::that(function (Request $request) use ($payload) {
193+
$this->assertRequestJsonBodyContains('failover', $payload['failover'], $request);
194+
return true;
195+
}))->willReturn($this->getResponse('sms-success', 202));
196+
$result = $this->messageClient->send($message);
197+
$this->assertIsArray($result);
198+
$this->assertArrayHasKey('message_uuid', $result);
199+
}
200+
169201
public function testCanSendMMSImage(): void
170202
{
171203
$imageUrl = 'https://picsum.photos/200/300';

0 commit comments

Comments
 (0)