Skip to content

Commit 63ef8f7

Browse files
committed
Added Notification webhook to voice webhook factory
1 parent e4509db commit 63ef8f7

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

src/Voice/Webhook/Factory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public static function createFromArray(array $data)
3333
return new Error($data);
3434
}
3535

36+
if (array_key_exists('payload', $data)) {
37+
return new Notification($data);
38+
}
39+
3640
throw new \InvalidArgumentException('Unable to detect incoming webhook type');
3741
}
3842
}

src/Voice/Webhook/Notification.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Nexmo\Voice\Webhook;
5+
6+
class Notification
7+
{
8+
/**
9+
* @var array<string, mixed>
10+
*/
11+
protected $payload;
12+
13+
/**
14+
* @var string
15+
*/
16+
protected $conversationUuid;
17+
18+
/**
19+
* @var \DateTimeImmutable
20+
*/
21+
protected $timestamp;
22+
23+
public function __construct(array $data)
24+
{
25+
if (is_string($data['payload'])) {
26+
$data['payload'] = json_decode($data['payload'], true);
27+
}
28+
29+
$this->payload = $data['payload'];
30+
$this->conversationUuid = $data['conversation_uuid'];
31+
$this->timestamp = new \DateTimeImmutable($data['timestamp']);
32+
}
33+
34+
public function getPayload() : array
35+
{
36+
return $this->payload;
37+
}
38+
39+
public function getConversationUuid() : string
40+
{
41+
return $this->conversationUuid;
42+
}
43+
44+
public function getTimestamp() : \DateTimeImmutable
45+
{
46+
return $this->timestamp;
47+
}
48+
}

test/Voice/NCCO/Action/NotifyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testGeneratesCorrectNCCOArray()
4646

4747
$this->assertSame('notify', $ncco['action']);
4848
$this->assertSame(['foo' => 'bar'], $ncco['payload']);
49-
$this->assertSame('https://test.domain/events', $ncco['eventUrl']);
49+
$this->assertSame(['https://test.domain/events'], $ncco['eventUrl']);
5050
$this->assertSame('POST', $ncco['eventMethod']);
5151
}
5252

@@ -61,7 +61,7 @@ public function testJSONSerializesToCorrectStructure()
6161

6262
$this->assertSame('notify', $ncco['action']);
6363
$this->assertSame(['foo' => 'bar'], $ncco['payload']);
64-
$this->assertSame('https://test.domain/events', $ncco['eventUrl']);
64+
$this->assertSame(['https://test.domain/events'], $ncco['eventUrl']);
6565
$this->assertSame('POST', $ncco['eventMethod']);
6666
}
6767

test/Voice/Webhook/FactoryTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Nexmo\Voice\Webhook\Factory;
1313
use Nexmo\Voice\Webhook\Transfer;
1414
use Zend\Diactoros\ServerRequest;
15+
use Nexmo\Voice\Webhook\Notification;
1516
use Zend\Diactoros\Request\Serializer;
1617

1718
class FactoryTest extends TestCase
@@ -155,6 +156,34 @@ public function testCanGenerateAnErrorWebhook()
155156
$this->assertEquals(new \DateTimeImmutable($expected['timestamp']), $error->getTimestamp());
156157
}
157158

159+
public function testCanGenerateANotificationGetWebhook()
160+
{
161+
$request = $this->getRequest('event-get-notify');
162+
$expected = $this->getRequest('event-get-notify')->getQueryParams();
163+
164+
/** @var Notification $notification */
165+
$notification = Factory::createFromRequest($request);
166+
167+
$this->assertTrue($notification instanceof Notification);
168+
$this->assertSame($expected['conversation_uuid'], $notification->getConversationUuid());
169+
$this->assertSame(json_decode($expected['payload'], true), $notification->getPayload());
170+
$this->assertEquals(new \DateTimeImmutable($expected['timestamp']), $notification->getTimestamp());
171+
}
172+
173+
public function testCanGenerateANotificationPostWebhook()
174+
{
175+
$request = $this->getRequest('event-post-notify');
176+
$expected = json_decode($this->getRequest('event-post-notify')->getBody()->getContents(), true);
177+
178+
/** @var Notification $notification */
179+
$notification = Factory::createFromRequest($request);
180+
181+
$this->assertTrue($notification instanceof Notification);
182+
$this->assertSame($expected['conversation_uuid'], $notification->getConversationUuid());
183+
$this->assertSame($expected['payload'], $notification->getPayload());
184+
$this->assertEquals(new \DateTimeImmutable($expected['timestamp']), $notification->getTimestamp());
185+
}
186+
158187
public function testThrowsExceptionOnUnknownWebhookData()
159188
{
160189
$this->expectException(\InvalidArgumentException::class);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
GET /webhooks/notification?payload=%7B%22foo%22:%22bar%22%7D&conversation_uuid=CON-2541d01c-253e-48be-a8e0-da4bbe4c3722&timestamp=2020-07-29T17:33:00.043Z HTTP/1.1
2+
User-Agent: Apache-HttpAsyncClient/4.1 (Java/1.8.0_66)
3+
Host: demo.ngrok.io
4+
Cache-Control: max-age=0
5+
X-Forwarded-For: 169.63.86.173
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
POST /webhooks/notification HTTP/1.1
2+
Content-Type: application/json
3+
Content-Length: 127
4+
User-Agent: Apache-HttpAsyncClient/4.1 (Java/1.8.0_66)
5+
Host: demo.ngrok.io
6+
Cache-Control: max-age=259200
7+
X-Forwarded-For: 169.63.86.173
8+
9+
{"payload":{"foo":"bar"},"conversation_uuid":"CON-2541d01c-253e-48be-a8e0-da4bbe4c3722","timestamp":"2020-07-29T17:32:26.628Z"}

0 commit comments

Comments
 (0)