Skip to content

Commit 13b0b15

Browse files
authored
add new capabilities to application (#427)
1 parent 4db1d3c commit 13b0b15

File tree

4 files changed

+139
-61
lines changed

4 files changed

+139
-61
lines changed

src/Application/Application.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ public function toArray(): array
264264
$configAccessorMethod = 'get' . ucfirst($type) . 'Config';
265265

266266
foreach ($values as $constant) {
267+
/** @var Webhook|\Vonage\Voice\Webhook $webhook */
267268
$webhook = $this->$configAccessorMethod()->getWebhook($constant);
268269

269270
if ($webhook) {
@@ -275,10 +276,31 @@ public function toArray(): array
275276
'address' => $webhook->getUrl(),
276277
'http_method' => $webhook->getMethod(),
277278
];
279+
280+
if (!is_null($webhook->getConnectionTimeout())) {
281+
$capabilities[$type]['webhooks'][$constant]['connection_timeout'] = $webhook->getConnectionTimeout();
282+
}
283+
284+
if (!is_null($webhook->getSocketTimeout())) {
285+
$capabilities[$type]['webhooks'][$constant]['socket_timeout'] = $webhook->getSocketTimeout();
286+
}
278287
}
279288
}
280289
}
281290

291+
// Handle other Voice capabilities outside of that needlessly complicated webhook loop
292+
if (!is_null($this->getVoiceConfig()->getRegion())) {
293+
$capabilities['voice']['region'] = $this->getVoiceConfig()->getRegion();
294+
}
295+
296+
if (!is_null($this->getVoiceConfig()->getConversationsTtl())) {
297+
$capabilities['voice']['conversations_ttl'] = $this->getVoiceConfig()->getConversationsTtl();
298+
}
299+
300+
if (!is_null($this->getVoiceConfig()->getSignedCallbacks())) {
301+
$capabilities['voice']['signed_callbacks'] = $this->getVoiceConfig()->getSignedCallbacks();
302+
}
303+
282304
// Handle VBC specifically
283305
if ($this->getVbcConfig()->isEnabled()) {
284306
$capabilities['vbc'] = new StdClass();

src/Application/VoiceConfig.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ class VoiceConfig
1717
{
1818
public const EVENT = 'event_url';
1919
public const ANSWER = 'answer_url';
20+
protected ?bool $signedCallbacks = null;
21+
protected ?int $conversationsTtl = null;
22+
protected ?string $region = null;
23+
24+
protected const ALLOWED_REGIONS = [
25+
'na-east',
26+
'na-west',
27+
'eu-west',
28+
'eu-east',
29+
'apac-sng',
30+
'apac-australia'
31+
];
2032

2133
/**
2234
* @var array
@@ -43,4 +55,44 @@ public function getWebhook($type)
4355
{
4456
return $this->webhooks[$type] ?? null;
4557
}
58+
59+
public function getSignedCallbacks(): ?bool
60+
{
61+
return $this->signedCallbacks;
62+
}
63+
64+
public function setSignedCallbacks(?bool $signedCallbacks): static
65+
{
66+
$this->signedCallbacks = $signedCallbacks;
67+
68+
return $this;
69+
}
70+
71+
public function getConversationsTtl(): ?int
72+
{
73+
return $this->conversationsTtl;
74+
}
75+
76+
public function setConversationsTtl(?int $conversationsTtl): static
77+
{
78+
$this->conversationsTtl = $conversationsTtl;
79+
80+
return $this;
81+
}
82+
83+
public function getRegion(): ?string
84+
{
85+
return $this->region;
86+
}
87+
88+
public function setRegion(?string $region): static
89+
{
90+
if (!in_array($region, self::ALLOWED_REGIONS, true)) {
91+
throw new \InvalidArgumentException('Unrecognised Region: ' . $region);
92+
}
93+
94+
$this->region = $region;
95+
96+
return $this;
97+
}
4698
}

src/Application/Webhook.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class Webhook implements \Stringable
1616
public const METHOD_POST = 'POST';
1717
public const METHOD_GET = 'GET';
1818

19+
public ?string $socketTimeout = null;
20+
21+
public ?string $connectionTimeout = null;
22+
1923
public function __construct(protected ?string $url, protected ?string $method = self::METHOD_POST)
2024
{
2125
}
@@ -34,4 +38,28 @@ public function __toString(): string
3438
{
3539
return $this->getUrl();
3640
}
41+
42+
public function getSocketTimeout(): ?string
43+
{
44+
return $this->socketTimeout;
45+
}
46+
47+
public function setSocketTimeout(?string $socketTimeout): static
48+
{
49+
$this->socketTimeout = $socketTimeout;
50+
51+
return $this;
52+
}
53+
54+
public function getConnectionTimeout(): ?string
55+
{
56+
return $this->connectionTimeout;
57+
}
58+
59+
public function setConnectionTimeout(?string $connectionTimeout): static
60+
{
61+
$this->connectionTimeout = $connectionTimeout;
62+
63+
return $this;
64+
}
3765
}

test/Application/ClientTest.php

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ public function getApplication(): array
151151
{
152152
return [
153153
['78d335fa323d01149c3dd6f0d48968cf', '78d335fa323d01149c3dd6f0d48968cf', false],
154-
// [new Application('78d335fa323d01149c3dd6f0d48968cf'), '78d335fa323d01149c3dd6f0d48968cf', true]
155154
];
156155
}
157156

@@ -519,70 +518,47 @@ public function createApplication(): array
519518
"NhPx2LhuLmgwWSRS4L5W851Xe3f\nUQIDAQAB\n-----END PUBLIC KEY-----\n");
520519
$application->getVbcConfig()->enable();
521520

522-
$rawV1 = [
523-
'name' => 'My Application',
524-
'answer_url' => 'https://example.com/webhooks/answer',
525-
'answer_method' => 'GET',
526-
'event_url' => 'https://example.com/webhooks/event',
527-
'event_method' => 'POST',
528-
'status_url' => 'https://example.com/webhooks/status',
529-
'status_method' => 'POST',
530-
'inbound_url' => 'https://example.com/webhooks/inbound',
531-
'inbound_method' => 'POST',
532-
'vbc' => true,
533-
'public_key' => "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCA\nKOxjsU4pf/sMFi9N0jqcSLcjxu33G" .
534-
"\nd/vynKnlw9SENi+UZR44GdjGdmfm1\ntL1eA7IBh2HNnkYXnAwYzKJoa4eO3\n0kYWekeIZawIwe/g9faFgkev+1xsO\nOUNhP" .
535-
"x2LhuLmgwWSRS4L5W851Xe3f\nUQIDAQAB\n-----END PUBLIC KEY-----\n"
521+
return [
522+
'createApplication' => [clone $application, 'create'],
536523
];
524+
}
537525

538-
$rawV2 = [
539-
'name' => 'My Application',
540-
'keys' => [
541-
'public_key' => "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCA\nKOxjsU4pf/sMFi9N0jqcSLcjx" .
542-
"u33G\nd/vynKnlw9SENi+UZR44GdjGdmfm1\ntL1eA7IBh2HNnkYXnAwYzKJoa4eO3\n0kYWekeIZawIwe/g9faFgkev+1xs" .
543-
"O\nOUNhPx2LhuLmgwWSRS4L5W851Xe3f\nUQIDAQAB\n-----END PUBLIC KEY-----\n"
544-
],
545-
'capabilities' => [
546-
'voice' => [
547-
'webhooks' => [
548-
'answer_url' => [
549-
'address' => 'https://example.com/webhooks/answer',
550-
'http_method' => 'GET',
551-
],
552-
'event_url' => [
553-
'address' => 'https://example.com/webhooks/event',
554-
'http_method' => 'POST',
555-
],
556-
]
557-
],
558-
'messages' => [
559-
'webhooks' => [
560-
'inbound_url' => [
561-
'address' => 'https://example.com/webhooks/inbound',
562-
'http_method' => 'POST'
526+
public function testCreateApplicationWithRegion(): void
527+
{
528+
$this->vonageClient->send(Argument::that(function (Request $request) {
529+
$this->assertEquals('/v2/applications', $request->getUri()->getPath());
530+
$this->assertEquals('api.nexmo.com', $request->getUri()->getHost());
531+
$this->assertEquals('POST', $request->getMethod());
563532

564-
],
565-
'status_url' => [
566-
'address' => 'https://example.com/webhooks/status',
567-
'http_method' => 'POST'
568-
]
569-
]
570-
],
571-
'rtc' => [
572-
'webhooks' => [
573-
'event_url' => [
574-
'address' => 'https://example.com/webhooks/event',
575-
'http_method' => 'POST',
576-
],
577-
]
578-
],
579-
'vbc' => []
580-
]
581-
];
533+
$this->assertRequestJsonBodyContains('name', 'my application', $request);
534+
$this->assertRequestJsonBodyContains('region', 'eu-west', $request, true);
535+
$this->assertRequestJsonBodyContains('signed_callbacks', true, $request, true);
536+
$this->assertRequestJsonBodyContains('conversations_ttl', 50, $request, true);
537+
return true;
538+
}))->willReturn($this->getResponse('success', 201));
582539

583-
return [
584-
'createApplication' => [clone $application, 'create'],
585-
];
540+
$application = new Application();
541+
$application->setName('my application');
542+
$application->getVoiceConfig()->setRegion('eu-west');
543+
$application->getVoiceConfig()->setConversationsTtl(50);
544+
$application->getVoiceConfig()->setSignedCallbacks(true);
545+
546+
547+
$application->setPublicKey("-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCA\nKOxjsU4pf/sMFi9N0jqcSL" .
548+
"cjxu33G\nd/vynKnlw9SENi+UZR44GdjGdmfm1\ntL1eA7IBh2HNnkYXnAwYzKJoa4eO3\n0kYWekeIZawIwe/g9faFgkev+1xsO\nOU" .
549+
"NhPx2LhuLmgwWSRS4L5W851Xe3f\nUQIDAQAB\n-----END PUBLIC KEY-----\n");
550+
551+
552+
$response = $this->applicationClient->create($application);
553+
}
554+
555+
public function testCannotSetUnknownRegion(): void
556+
{
557+
$this->expectException(\InvalidArgumentException::class);
558+
$this->expectExceptionMessage('Unrecognised Region: eu-west-1');
559+
$application = new Application();
560+
$application->setName('my application');
561+
$application->getVoiceConfig()->setRegion('eu-west-1');
586562
}
587563

588564
/**

0 commit comments

Comments
 (0)