Skip to content

Commit e5c4160

Browse files
committed
Basic Outbound Call Support
1 parent 4283114 commit e5c4160

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed

src/Nexmo/Voice/Call/Call.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* @author Tim Lytle <tim@timlytle.net>
4+
*/
5+
6+
namespace Nexmo\Voice\Call;
7+
use Nexmo\Client\Request\AbstractRequest;
8+
use Nexmo\Client\Request\RequestInterface;
9+
10+
class Call extends AbstractRequest implements RequestInterface
11+
{
12+
public function __construct($url, $to, $from = null)
13+
{
14+
$this->params['answer_url'] = $url;
15+
$this->params['to'] = $to;
16+
17+
if(!is_null($from)){
18+
$this->params['from'] = $from;
19+
}
20+
}
21+
22+
public function setAnswer($url, $method = null)
23+
{
24+
$this->params['answer_url'] = $url;
25+
if(!is_null($method)){
26+
$this->params['answer_method'] = $method;
27+
} else {
28+
unset($this->params['answer_method']);
29+
}
30+
31+
return $this;
32+
}
33+
34+
public function setError($url, $method = null)
35+
{
36+
$this->params['error_url'] = $url;
37+
if(!is_null($method)){
38+
$this->params['error_method'] = $method;
39+
} else {
40+
unset($this->params['error_method']);
41+
}
42+
43+
return $this;
44+
}
45+
46+
public function setStatus($url, $method = null)
47+
{
48+
$this->params['status_url'] = $url;
49+
if(!is_null($method)){
50+
$this->params['status_method'] = $method;
51+
} else {
52+
unset($this->params['status_method']);
53+
}
54+
55+
return $this;
56+
}
57+
58+
59+
public function setMachineDetection($hangup = true, $timeout = null)
60+
{
61+
$this->params['machine_detection'] = ($hangup ? 'hangup' : 'true');
62+
if(!is_null($timeout)){
63+
$this->params['machine_timeout'] = (int) $timeout;
64+
} else {
65+
unset($this->params['machine_timeout']);
66+
}
67+
68+
return $this;
69+
}
70+
71+
/**
72+
* @return string
73+
*/
74+
public function getURI()
75+
{
76+
return '/call/json';
77+
}
78+
79+
}

src/Nexmo/Voice/Call/Inbound.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* @author Tim Lytle <tim@timlytle.net>
4+
*/
5+
6+
namespace Nexmo\Voice\Call;
7+
8+
9+
class Inbound
10+
{
11+
12+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* @author Tim Lytle <tim@timlytle.net>
4+
*/
5+
namespace Nexmo\Voice\Call;
6+
class CallTest extends \PHPUnit_Framework_TestCase
7+
{
8+
/**
9+
* @var Call
10+
*/
11+
protected $call;
12+
13+
protected $to = '15554443232';
14+
protected $from = '15551112323';
15+
protected $url = 'http://example.com';
16+
17+
public function setUp()
18+
{
19+
$this->call = new Call($this->url, $this->to, $this->from);
20+
}
21+
22+
public function testConstructParams()
23+
{
24+
$params = $this->call->getParams();
25+
26+
$this->assertArrayHasKey('to', $params);
27+
$this->assertArrayHasKey('from', $params);
28+
$this->assertArrayHasKey('answer_url', $params);
29+
30+
$this->assertEquals($this->to, $params['to']);
31+
$this->assertEquals($this->from, $params['from']);
32+
$this->assertEquals($this->url, $params['answer_url']);
33+
}
34+
35+
public function testFromOptional()
36+
{
37+
$call = new Call($this->url, $this->to);
38+
39+
$params = $call->getParams();
40+
41+
$this->assertArrayNotHasKey('from', $params);
42+
}
43+
44+
public function testMachine()
45+
{
46+
$this->call->setMachineDetection();
47+
$params = $this->call->getParams();
48+
$this->assertArrayHasKey('machine_detection', $params);
49+
$this->assertArrayNotHasKey('machine_timeout', $params);
50+
$this->assertEquals('hangup', $params['machine_detection']);
51+
52+
$this->call->setMachineDetection(true, 100);
53+
$params = $this->call->getParams();
54+
$this->assertArrayHasKey('machine_detection', $params);
55+
$this->assertArrayHasKey('machine_timeout', $params);
56+
$this->assertEquals('hangup', $params['machine_detection']);
57+
$this->assertEquals(100, $params['machine_timeout']);
58+
59+
$this->call->setMachineDetection(false);
60+
$params = $this->call->getParams();
61+
$this->assertArrayHasKey('machine_detection', $params);
62+
$this->assertArrayNotHasKey('machine_timeout', $params);
63+
$this->assertEquals('true', $params['machine_detection']);
64+
}
65+
66+
/**
67+
* @dataProvider getCallbacks
68+
*/
69+
public function testCallback($method, $param, $param_method)
70+
{
71+
$this->call->$method('http://example.com');
72+
$params = $this->call->getParams();
73+
$this->assertArrayHasKey($param, $params);
74+
$this->assertEquals('http://example.com', $params[$param]);
75+
$this->assertArrayNotHasKey($param_method, $params);
76+
77+
$this->call->$method('http://example.com', 'POST');
78+
$params = $this->call->getParams();
79+
$this->assertArrayHasKey($param, $params);
80+
$this->assertEquals('http://example.com', $params[$param]);
81+
$this->assertArrayHasKey($param_method, $params);
82+
$this->assertEquals('POST', $params[$param_method]);
83+
84+
$this->call->$method('http://example.com');
85+
$params = $this->call->getParams();
86+
$this->assertArrayHasKey($param, $params);
87+
$this->assertEquals('http://example.com', $params[$param]);
88+
$this->assertArrayNotHasKey($param_method, $params);
89+
}
90+
91+
public function getCallbacks()
92+
{
93+
return array(
94+
array('setAnswer', 'answer_url', 'answer_method'),
95+
array('setError', 'error_url', 'error_method'),
96+
array('setStatus', 'status_url', 'status_method')
97+
);
98+
}
99+
}
100+

0 commit comments

Comments
 (0)