Skip to content

Commit 5825a2e

Browse files
committed
Baseid Call Support:
- Can get a specific call. - Can create an outbound call. - No call manipulation, or listing.
1 parent 05dd3a3 commit 5825a2e

File tree

11 files changed

+772
-1
lines changed

11 files changed

+772
-1
lines changed

src/Calls/Call.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
6+
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
7+
*/
8+
9+
namespace Nexmo\Calls;
10+
11+
12+
use Nexmo\Conversations\Conversation;
13+
use Nexmo\Entity\EntityInterface;
14+
use Nexmo\Entity\JsonResponseTrait;
15+
use Nexmo\Entity\JsonSerializableTrait;
16+
use Nexmo\Entity\JsonUnserializableInterface;
17+
use Nexmo\Entity\NoRequestResponseTrait;
18+
use Nexmo\Entity\Psr7Trait;
19+
20+
class Call implements EntityInterface, \JsonSerializable, JsonUnserializableInterface
21+
{
22+
use NoRequestResponseTrait;
23+
use JsonSerializableTrait;
24+
use JsonResponseTrait;
25+
26+
const WEBHOOK_ANSWER = 'answer';
27+
const WEBHOOK_EVENT = 'event';
28+
29+
const TIMER_LENGTH = 'length';
30+
const TIMER_RINGING = 'ringing';
31+
32+
const TIMEOUT_MACHINE = 'machine';
33+
34+
protected $id;
35+
36+
protected $to;
37+
38+
protected $from;
39+
40+
/**
41+
* @var Webhook[]
42+
*/
43+
protected $webhooks = [];
44+
45+
protected $data = [];
46+
47+
public function __construct($id = null)
48+
{
49+
$this->id = $id;
50+
}
51+
52+
public function getId()
53+
{
54+
return $this->id;
55+
}
56+
57+
public function setTo($endpoint)
58+
{
59+
if(!($endpoint instanceof Endpoint)){
60+
$endpoint = new Endpoint($endpoint);
61+
}
62+
63+
$this->to = $endpoint;
64+
return $this;
65+
}
66+
67+
/**
68+
* @return Endpoint
69+
*/
70+
public function getTo()
71+
{
72+
if(!empty($this->data)){
73+
return new Endpoint($this->data['to']['number'], $this->data['to']['type']);
74+
}
75+
76+
return $this->to;
77+
}
78+
79+
public function setFrom($endpoint)
80+
{
81+
if(!($endpoint instanceof Endpoint)){
82+
$endpoint = new Endpoint($endpoint);
83+
}
84+
85+
$this->from = $endpoint;
86+
return $this;
87+
}
88+
89+
/**
90+
* @return Endpoint
91+
*/
92+
public function getFrom()
93+
{
94+
if(!empty($this->data)){
95+
return new Endpoint($this->data['from']['number'], $this->data['from']['type']);
96+
}
97+
98+
return $this->from;
99+
}
100+
101+
public function setWebhook($type, $url = null, $method = null)
102+
{
103+
if($type instanceof Webhook){
104+
$this->webhooks[$type->getType()] = $type;
105+
return $this;
106+
}
107+
108+
if(is_null($url)){
109+
throw new \InvalidArgumentException('must provide `Nexmo\Calls\Webhook` object, or a type and url: missing url' );
110+
}
111+
112+
$this->webhooks[$type] = new Webhook($type, $url, $method);
113+
return $this;
114+
}
115+
116+
public function setTimer($type, $length)
117+
{
118+
$this->data[$type . '_timer'] = $length;
119+
}
120+
121+
public function setTimeout($type, $length)
122+
{
123+
$this->data[$type . '_timeout'] = $length;
124+
}
125+
126+
public function getStatus()
127+
{
128+
if(!empty($this->data)){
129+
return $this->data['status'];
130+
}
131+
}
132+
133+
public function getDirection()
134+
{
135+
if(!empty($this->data)){
136+
return $this->data['direction'];
137+
}
138+
}
139+
140+
public function getConversation()
141+
{
142+
if(!empty($this->data)){
143+
return new Conversation($this->data['conversation_uuid']);
144+
}
145+
}
146+
147+
function jsonSerialize()
148+
{
149+
$data = $this->data;
150+
151+
if(isset($this->to)){
152+
$data['to'] = [$this->to->jsonSerialize()];
153+
}
154+
155+
if(isset($this->from)){
156+
$data['from'] = $this->from->jsonSerialize();
157+
}
158+
159+
foreach($this->webhooks as $webhook){
160+
$data = array_merge($data, $webhook->jsonSerialize());
161+
}
162+
163+
return $data;
164+
}
165+
166+
public function JsonUnserialize(array $json)
167+
{
168+
$this->data = $json;
169+
$this->id = $json['uuid'];
170+
}
171+
}

src/Calls/Client.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
6+
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
7+
*/
8+
9+
namespace Nexmo\Calls;
10+
11+
use Nexmo\Client\ClientAwareInterface;
12+
use Nexmo\Client\ClientAwareTrait;
13+
use Nexmo\Conversations\Conversation;
14+
use Nexmo\Entity\CollectionInterface;
15+
use Nexmo\Entity\CollectionTrait;
16+
use Psr\Http\Message\ResponseInterface;
17+
use Zend\Diactoros\Request;
18+
use Nexmo\Client\Exception;
19+
20+
class Client implements ClientAwareInterface, CollectionInterface
21+
{
22+
use ClientAwareTrait;
23+
use CollectionTrait;
24+
25+
public function getCollectionName()
26+
{
27+
return 'calls';
28+
}
29+
30+
public function getCollectionPath()
31+
{
32+
return '/v1/' . $this->getCollectionName();
33+
}
34+
35+
public function create($call)
36+
{
37+
return $this->post($call);
38+
}
39+
40+
public function post($call)
41+
{
42+
if($call instanceof Call){
43+
$body = $call->getRequestData();
44+
} else {
45+
$body = $call;
46+
}
47+
48+
$request = new Request(
49+
\Nexmo\Client::BASE_API . $this->getCollectionPath()
50+
,'POST',
51+
'php://temp',
52+
['content-type' => 'application/json']
53+
);
54+
55+
$request->getBody()->write(json_encode($body));
56+
$response = $this->client->send($request);
57+
58+
if($response->getStatusCode() != '201'){
59+
throw $this->getException($response);
60+
}
61+
62+
$body = json_decode($response->getBody()->getContents(), true);
63+
return new Conversation($body['conversation_uuid']);
64+
}
65+
66+
public function get($call)
67+
{
68+
if(!($call instanceof Call)){
69+
$call = new Call($call);
70+
}
71+
72+
$request = new Request(
73+
\Nexmo\Client::BASE_API . $this->getCollectionPath() . '/' . $call->getId()
74+
,'GET'
75+
);
76+
77+
$response = $this->client->send($request);
78+
79+
if($response->getStatusCode() != '200'){
80+
throw $this->getException($response, $call);
81+
}
82+
83+
$body = json_decode($response->getBody()->getContents(), true);
84+
$call->JsonUnserialize($body);
85+
86+
return $call;
87+
}
88+
89+
protected function getException(ResponseInterface $response)
90+
{
91+
$body = json_decode($response->getBody()->getContents(), true);
92+
$status = $response->getStatusCode();
93+
94+
if($status >= 400 AND $status < 500) {
95+
$e = new Exception\Request($body['error_title'], $status);
96+
} elseif($status >= 500 AND $status < 600) {
97+
$e = new Exception\Server($body['error_title'], $status);
98+
} else {
99+
$e = new Exception\Exception('Unexpected HTTP Status Code');
100+
throw $e;
101+
}
102+
103+
return $e;
104+
}
105+
106+
}

src/Calls/Endpoint.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Nexmo Client Library for PHP
4+
*
5+
* @copyright Copyright (c) 2016 Nexmo, Inc. (http://nexmo.com)
6+
* @license https://github.com/Nexmo/nexmo-php/blob/master/LICENSE.txt MIT License
7+
*/
8+
9+
namespace Nexmo\Calls;
10+
11+
class Endpoint implements \JsonSerializable
12+
{
13+
const PHONE = 'phone';
14+
15+
protected $id;
16+
17+
protected $type;
18+
19+
protected $additional;
20+
21+
public function __construct($id, $type = self::PHONE, $additional = [])
22+
{
23+
$this->id = $id;
24+
$this->type = $type;
25+
$this->additional = $additional;
26+
}
27+
28+
public function getType()
29+
{
30+
return $this->type;
31+
}
32+
33+
public function getId()
34+
{
35+
return $this->id;
36+
}
37+
38+
public function set($property, $value)
39+
{
40+
$this->additional[$property] = $value;
41+
return $this;
42+
}
43+
44+
public function get($property)
45+
{
46+
if(isset($this->additional[$property])){
47+
return $this->additional[$property];
48+
}
49+
}
50+
51+
public function getNumber()
52+
{
53+
if(!self::PHONE == $this->type){
54+
throw new \RuntimeException('number not defined for this type');
55+
}
56+
57+
return $this->getId();
58+
}
59+
60+
public function __toString()
61+
{
62+
return (string) $this->getId();
63+
}
64+
65+
function jsonSerialize()
66+
{
67+
switch($this->type){
68+
case 'phone':
69+
return array_merge(
70+
$this->additional,
71+
[
72+
'type' => $this->type,
73+
'number' => $this->id
74+
]
75+
76+
);
77+
default:
78+
throw new \RuntimeException('unknown type: ' . $this->type);
79+
}
80+
}
81+
82+
83+
}

0 commit comments

Comments
 (0)