Skip to content

Commit

Permalink
add new capabilities to application (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK authored Aug 10, 2023
1 parent 4db1d3c commit 13b0b15
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 61 deletions.
22 changes: 22 additions & 0 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ public function toArray(): array
$configAccessorMethod = 'get' . ucfirst($type) . 'Config';

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

if ($webhook) {
Expand All @@ -275,10 +276,31 @@ public function toArray(): array
'address' => $webhook->getUrl(),
'http_method' => $webhook->getMethod(),
];

if (!is_null($webhook->getConnectionTimeout())) {
$capabilities[$type]['webhooks'][$constant]['connection_timeout'] = $webhook->getConnectionTimeout();
}

if (!is_null($webhook->getSocketTimeout())) {
$capabilities[$type]['webhooks'][$constant]['socket_timeout'] = $webhook->getSocketTimeout();
}
}
}
}

// Handle other Voice capabilities outside of that needlessly complicated webhook loop
if (!is_null($this->getVoiceConfig()->getRegion())) {
$capabilities['voice']['region'] = $this->getVoiceConfig()->getRegion();
}

if (!is_null($this->getVoiceConfig()->getConversationsTtl())) {
$capabilities['voice']['conversations_ttl'] = $this->getVoiceConfig()->getConversationsTtl();
}

if (!is_null($this->getVoiceConfig()->getSignedCallbacks())) {
$capabilities['voice']['signed_callbacks'] = $this->getVoiceConfig()->getSignedCallbacks();
}

// Handle VBC specifically
if ($this->getVbcConfig()->isEnabled()) {
$capabilities['vbc'] = new StdClass();
Expand Down
52 changes: 52 additions & 0 deletions src/Application/VoiceConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ class VoiceConfig
{
public const EVENT = 'event_url';
public const ANSWER = 'answer_url';
protected ?bool $signedCallbacks = null;
protected ?int $conversationsTtl = null;
protected ?string $region = null;

protected const ALLOWED_REGIONS = [
'na-east',
'na-west',
'eu-west',
'eu-east',
'apac-sng',
'apac-australia'
];

/**
* @var array
Expand All @@ -43,4 +55,44 @@ public function getWebhook($type)
{
return $this->webhooks[$type] ?? null;
}

public function getSignedCallbacks(): ?bool
{
return $this->signedCallbacks;
}

public function setSignedCallbacks(?bool $signedCallbacks): static
{
$this->signedCallbacks = $signedCallbacks;

return $this;
}

public function getConversationsTtl(): ?int
{
return $this->conversationsTtl;
}

public function setConversationsTtl(?int $conversationsTtl): static
{
$this->conversationsTtl = $conversationsTtl;

return $this;
}

public function getRegion(): ?string
{
return $this->region;
}

public function setRegion(?string $region): static
{
if (!in_array($region, self::ALLOWED_REGIONS, true)) {
throw new \InvalidArgumentException('Unrecognised Region: ' . $region);
}

$this->region = $region;

return $this;
}
}
28 changes: 28 additions & 0 deletions src/Application/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class Webhook implements \Stringable
public const METHOD_POST = 'POST';
public const METHOD_GET = 'GET';

public ?string $socketTimeout = null;

public ?string $connectionTimeout = null;

public function __construct(protected ?string $url, protected ?string $method = self::METHOD_POST)
{
}
Expand All @@ -34,4 +38,28 @@ public function __toString(): string
{
return $this->getUrl();
}

public function getSocketTimeout(): ?string
{
return $this->socketTimeout;
}

public function setSocketTimeout(?string $socketTimeout): static
{
$this->socketTimeout = $socketTimeout;

return $this;
}

public function getConnectionTimeout(): ?string
{
return $this->connectionTimeout;
}

public function setConnectionTimeout(?string $connectionTimeout): static
{
$this->connectionTimeout = $connectionTimeout;

return $this;
}
}
98 changes: 37 additions & 61 deletions test/Application/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ public function getApplication(): array
{
return [
['78d335fa323d01149c3dd6f0d48968cf', '78d335fa323d01149c3dd6f0d48968cf', false],
// [new Application('78d335fa323d01149c3dd6f0d48968cf'), '78d335fa323d01149c3dd6f0d48968cf', true]
];
}

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

$rawV1 = [
'name' => 'My Application',
'answer_url' => 'https://example.com/webhooks/answer',
'answer_method' => 'GET',
'event_url' => 'https://example.com/webhooks/event',
'event_method' => 'POST',
'status_url' => 'https://example.com/webhooks/status',
'status_method' => 'POST',
'inbound_url' => 'https://example.com/webhooks/inbound',
'inbound_method' => 'POST',
'vbc' => true,
'public_key' => "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCA\nKOxjsU4pf/sMFi9N0jqcSLcjxu33G" .
"\nd/vynKnlw9SENi+UZR44GdjGdmfm1\ntL1eA7IBh2HNnkYXnAwYzKJoa4eO3\n0kYWekeIZawIwe/g9faFgkev+1xsO\nOUNhP" .
"x2LhuLmgwWSRS4L5W851Xe3f\nUQIDAQAB\n-----END PUBLIC KEY-----\n"
return [
'createApplication' => [clone $application, 'create'],
];
}

$rawV2 = [
'name' => 'My Application',
'keys' => [
'public_key' => "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCA\nKOxjsU4pf/sMFi9N0jqcSLcjx" .
"u33G\nd/vynKnlw9SENi+UZR44GdjGdmfm1\ntL1eA7IBh2HNnkYXnAwYzKJoa4eO3\n0kYWekeIZawIwe/g9faFgkev+1xs" .
"O\nOUNhPx2LhuLmgwWSRS4L5W851Xe3f\nUQIDAQAB\n-----END PUBLIC KEY-----\n"
],
'capabilities' => [
'voice' => [
'webhooks' => [
'answer_url' => [
'address' => 'https://example.com/webhooks/answer',
'http_method' => 'GET',
],
'event_url' => [
'address' => 'https://example.com/webhooks/event',
'http_method' => 'POST',
],
]
],
'messages' => [
'webhooks' => [
'inbound_url' => [
'address' => 'https://example.com/webhooks/inbound',
'http_method' => 'POST'
public function testCreateApplicationWithRegion(): void
{
$this->vonageClient->send(Argument::that(function (Request $request) {
$this->assertEquals('/v2/applications', $request->getUri()->getPath());
$this->assertEquals('api.nexmo.com', $request->getUri()->getHost());
$this->assertEquals('POST', $request->getMethod());

],
'status_url' => [
'address' => 'https://example.com/webhooks/status',
'http_method' => 'POST'
]
]
],
'rtc' => [
'webhooks' => [
'event_url' => [
'address' => 'https://example.com/webhooks/event',
'http_method' => 'POST',
],
]
],
'vbc' => []
]
];
$this->assertRequestJsonBodyContains('name', 'my application', $request);
$this->assertRequestJsonBodyContains('region', 'eu-west', $request, true);
$this->assertRequestJsonBodyContains('signed_callbacks', true, $request, true);
$this->assertRequestJsonBodyContains('conversations_ttl', 50, $request, true);
return true;
}))->willReturn($this->getResponse('success', 201));

return [
'createApplication' => [clone $application, 'create'],
];
$application = new Application();
$application->setName('my application');
$application->getVoiceConfig()->setRegion('eu-west');
$application->getVoiceConfig()->setConversationsTtl(50);
$application->getVoiceConfig()->setSignedCallbacks(true);


$application->setPublicKey("-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCA\nKOxjsU4pf/sMFi9N0jqcSL" .
"cjxu33G\nd/vynKnlw9SENi+UZR44GdjGdmfm1\ntL1eA7IBh2HNnkYXnAwYzKJoa4eO3\n0kYWekeIZawIwe/g9faFgkev+1xsO\nOU" .
"NhPx2LhuLmgwWSRS4L5W851Xe3f\nUQIDAQAB\n-----END PUBLIC KEY-----\n");


$response = $this->applicationClient->create($application);
}

public function testCannotSetUnknownRegion(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unrecognised Region: eu-west-1');
$application = new Application();
$application->setName('my application');
$application->getVoiceConfig()->setRegion('eu-west-1');
}

/**
Expand Down

0 comments on commit 13b0b15

Please sign in to comment.