Skip to content

Commit 83aa473

Browse files
authored
Merge pull request #15 from Nexmo/account-api
New API Support
2 parents 4b30bea + e867cc3 commit 83aa473

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+3585
-18
lines changed

README.md

+124-4
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,126 @@ $verification = new \Nexmo\Verify\Verification('00e6c3377e5348cdaf567e1417c707a5
219219
$client->verify()->search($verification);
220220
echo "Verification cost was: " . $verification['price'] . PHP_EOL;
221221
```
222+
223+
### Making A Call
224+
225+
All `$client->calls()` methods require the client to be constructed with a `Nexmo\Client\Credentials\Keypair`, or a
226+
`Nexmo\Client\Credentials\Container` that includes the `Keypair` credentials:
227+
228+
```php
229+
$basic = new \Nexmo\Client\Credentials\Basic('key', 'secret');
230+
$keypair = new \Nexmo\Client\Credentials\Keypair(file_get_contents(__DIR__ . '/application.key'), 'application_id');
231+
232+
$client = new \Nexmo\Client(new \Nexmo\Client\Credentials\Container($basic, $keypair));
233+
```
234+
235+
You can start a call using an array as the structure:
236+
237+
```php
238+
$client->calls()->create([
239+
'to' => [[
240+
'type' => 'phone',
241+
'number' => '14843331234'
242+
]],
243+
'from' => [
244+
'type' => 'phone',
245+
'number' => '14843335555'
246+
],
247+
'answer_url' => ['https://example.com/answer'],
248+
'event_url' => ['https://example.com/event'],
249+
]);
250+
```
251+
252+
Or you can create a `Nexmo\Calls\Call` object, and use that:
253+
254+
```php
255+
use Nexmo\Calls\Call;
256+
$call = new Call();
257+
$call->setTo('14843331234')
258+
->setFrom('14843335555')
259+
->setWebhook(Call::WEBHOOK_ANSWER, 'https://example.com/answer')
260+
->setWebhook(Call::WEBHOOK_EVENT, 'https://example.com/event');
261+
262+
$client->calls()->create($call);
263+
```
264+
265+
### Fetching A Call
266+
267+
You can fetch a call using a `Nexmo\Calls\Call` object, or the call's UUID as a string:
268+
269+
```php
270+
$call = $client->calls()->get('3fd4d839-493e-4485-b2a5-ace527aacff3');
271+
272+
$call = new Nexmo\Calls\Call('3fd4d839-493e-4485-b2a5-ace527aacff3');
273+
$client->calls()->get($call);
274+
275+
echo $call->getDirection();
276+
```
277+
278+
### Creating An Application
279+
280+
Application are configuration containers, and you can create one using a simple array structure:
281+
282+
```php
283+
$application = $client->applications()->create([
284+
'name' => 'My Application',
285+
'answer_url' => 'https://example.com/answer',
286+
'event_url' => 'https://example.com/event'
287+
])
288+
```
289+
290+
You can also pass the client an application object:
291+
292+
```php
293+
$application = new Nexmo\Application\Application();
294+
$application->setName('My Application');
295+
$application->getVoiceConfig()->setWebhook(VoiceConfig::ANSWER, 'https://example.com/answer');
296+
$application->getVoiceConfig()->setWebhook(VoiceConfig::EVENT, 'https://example.com/event');
297+
298+
$client->appliations()->create($application);
299+
```
300+
301+
### Fetching Applications
302+
303+
You can iterate over all your applications:
304+
305+
```php
306+
foreach($client->applications() as $application){
307+
echo $application->getName() . PHP_EOL;
308+
}
309+
```
310+
311+
Or you can fetch an application using a string UUID, or an application object.
312+
313+
```php
314+
$application = $client->applications()->get('1a20a124-1775-412b-b623-e6985f4aace0');
315+
316+
$application = new Application('1a20a124-1775-412b-b623-e6985f4aace0');
317+
$client->applications()->get($application);
318+
```
319+
320+
### Updating an Application
321+
322+
Once you have an application object, you can modify and save it.
323+
324+
```php
325+
$application = $client->applications()->get('1a20a124-1775-412b-b623-e6985f4aace0');
326+
327+
$application->setName('Updated Application');
328+
$client->applications()->update($application);
329+
```
330+
331+
You can also pass an array and the application UUID to the client:
332+
333+
```php
334+
$application = $client->applications()->update([
335+
'name' => 'Updated Application',
336+
'answer_url' => 'https://example.com/v2/answer',
337+
'event_url' => 'https://example.com/v2/event'
338+
], '1a20a124-1775-412b-b623-e6985f4aace0');
339+
```
340+
341+
222342

223343
API Coverage
224344
------------
@@ -228,11 +348,11 @@ API Coverage
228348
* [ ] Pricing
229349
* [ ] Settings
230350
* [ ] Top Up
231-
* [ ] Numbers
232-
* [ ] Search
351+
* [X] Numbers
352+
* [X] Search
233353
* [ ] Buy
234354
* [ ] Cancel
235-
* [ ] Update
355+
* [X] Update
236356
* Number Insight
237357
* [ ] Basic
238358
* [ ] Standard
@@ -257,7 +377,7 @@ API Coverage
257377
* [ ] Sending Alerts
258378
* [ ] Campaign Subscription Management
259379
* Voice
260-
* [ ] Outbound Calls
380+
* [X] Outbound Calls
261381
* [ ] Inbound Call
262382
* [ ] Text-To-Speech Call
263383
* [ ] Text-To-Speech Prompt

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"php": ">=5.4",
1919
"php-http/client-implementation": "^1.0",
2020
"zendframework/zend-diactoros": "^1.3",
21-
"php-http/guzzle6-adapter": "^1.0"
21+
"php-http/guzzle6-adapter": "^1.0",
22+
"lcobucci/jwt": "^3.2"
2223
},
2324
"require-dev": {
2425
"phpunit/phpunit": "^5.3",
@@ -29,4 +30,4 @@
2930
"Nexmo\\": "src/"
3031
}
3132
}
32-
}
33+
}

phpunit.xml.dist

+5
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@
44
<directory>./test/</directory>
55
</testsuite>
66
</testsuites>
7+
<filter>
8+
<whitelist>
9+
<directory suffix=".php">./src/</directory>
10+
</whitelist>
11+
</filter>
712
</phpunit>

src/Application/Application.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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\Application;
10+
11+
12+
use Nexmo\Entity\JsonUnserializableInterface;
13+
use Nexmo\Entity\EntityInterface;
14+
use Nexmo\Entity\JsonResponseTrait;
15+
use Nexmo\Entity\JsonSerializableTrait;
16+
use Nexmo\Entity\Psr7Trait;
17+
18+
class Application implements EntityInterface, \JsonSerializable, JsonUnserializableInterface
19+
{
20+
use JsonSerializableTrait;
21+
use Psr7Trait;
22+
use JsonResponseTrait;
23+
24+
protected $voiceConfig;
25+
26+
protected $name;
27+
28+
protected $keys = [];
29+
30+
protected $id;
31+
32+
public function __construct($id = null)
33+
{
34+
$this->id = $id;
35+
}
36+
37+
public function getId()
38+
{
39+
return $this->id;
40+
}
41+
42+
public function setVoiceConfig(VoiceConfig $config)
43+
{
44+
$this->voiceConfig = $config;
45+
return $this;
46+
}
47+
48+
/**
49+
* @return VoiceConfig
50+
*/
51+
public function getVoiceConfig()
52+
{
53+
if(!isset($this->voiceConfig)){
54+
$this->setVoiceConfig(new VoiceConfig());
55+
$data = $this->getResponseData();
56+
if(isset($data['voice']) AND isset($data['voice']['webhooks'])){
57+
foreach($data['voice']['webhooks'] as $webhook){
58+
$this->voiceConfig->setWebhook($webhook['endpoint_type'], $webhook['endpoint'], $webhook['http_method']);
59+
}
60+
}
61+
}
62+
63+
return $this->voiceConfig;
64+
}
65+
66+
public function getPublicKey()
67+
{
68+
if(isset($this->keys['public_key'])){
69+
return $this->keys['public_key'];
70+
}
71+
}
72+
73+
public function getPrivateKey()
74+
{
75+
if(isset($this->keys['private_key'])){
76+
return $this->keys['private_key'];
77+
}
78+
}
79+
80+
public function setName($name)
81+
{
82+
$this->name = $name;
83+
return $this;
84+
}
85+
86+
public function getName()
87+
{
88+
return $this->name;
89+
}
90+
91+
public function jsonUnserialize(array $json)
92+
{
93+
$this->name = $json['name'];
94+
$this->id = $json['id'];
95+
$this->keys = $json['keys'];
96+
97+
//todo: make voice hydrate-able
98+
$this->voiceConfig = new VoiceConfig();
99+
if(isset($json['voice']) AND isset($json['voice']['webhooks'])){
100+
foreach($json['voice']['webhooks'] as $webhook){
101+
$this->voiceConfig->setWebhook($webhook['endpoint_type'], new Webhook($webhook['endpoint'], $webhook['http_method']));
102+
}
103+
}
104+
}
105+
106+
public function jsonSerialize()
107+
{
108+
return [
109+
'name' => $this->getName(),
110+
//currently, the request data does not match the response data
111+
'event_url' => (string) $this->getVoiceConfig()->getWebhook(VoiceConfig::EVENT),
112+
'answer_url' => (string) $this->getVoiceConfig()->getWebhook(VoiceConfig::ANSWER),
113+
'type' => 'voice' //currently the only type
114+
];
115+
}
116+
117+
public function __toString()
118+
{
119+
return (string) $this->getId();
120+
}
121+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Application;
10+
11+
use Nexmo\Entity\EntityInterface;
12+
13+
interface ApplicationInterface extends EntityInterface
14+
{
15+
public function getId();
16+
}

0 commit comments

Comments
 (0)