Skip to content

Commit 1941107

Browse files
committed
Merge branch 'develop-collection-refactor' into develop
2 parents 87dc35a + 75f1cc1 commit 1941107

Some content is hidden

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

50 files changed

+2607
-600
lines changed

README.md

+28-5
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ $client->calls()->create([
249249
]);
250250
```
251251

252-
Or you can create a `Nexmo\Calls\Call` object, and use that:
252+
Or you can create a `Nexmo\Call\Call` object, and use that:
253253

254254
```php
255-
use Nexmo\Calls\Call;
255+
use Nexmo\Call\Call;
256256
$call = new Call();
257257
$call->setTo('14843331234')
258258
->setFrom('14843335555')
@@ -264,17 +264,40 @@ $client->calls()->create($call);
264264

265265
### Fetching A Call
266266

267-
You can fetch a call using a `Nexmo\Calls\Call` object, or the call's UUID as a string:
267+
You can fetch a call using a `Nexmo\Call\Call` object, or the call's UUID as a string:
268268

269269
```php
270270
$call = $client->calls()->get('3fd4d839-493e-4485-b2a5-ace527aacff3');
271271

272-
$call = new Nexmo\Calls\Call('3fd4d839-493e-4485-b2a5-ace527aacff3');
272+
$call = new Nexmo\Call\Call('3fd4d839-493e-4485-b2a5-ace527aacff3');
273273
$client->calls()->get($call);
274274

275275
echo $call->getDirection();
276276
```
277277

278+
The call collection can also be treated as an array:
279+
280+
```php
281+
echo $client->calls['3fd4d839-493e-4485-b2a5-ace527aacff3']->getDirection();
282+
```
283+
284+
And iterated over:
285+
286+
```php
287+
foreach($client->calls as $call){
288+
echo $call->getDirection();
289+
}
290+
```
291+
292+
With an optional filter:
293+
294+
```php
295+
$filter = new \Nexmo\Call\Filter()->setStatus('completed');
296+
foreach($client->calls($filter) as $call){
297+
echo $call->getDirection();
298+
}
299+
```
300+
278301
### Creating An Application
279302

280303
Application are configuration containers, and you can create one using a simple array structure:
@@ -377,7 +400,7 @@ API Coverage
377400
* [ ] Sending Alerts
378401
* [ ] Campaign Subscription Management
379402
* Voice
380-
* [X] Outbound Calls
403+
* [X] Outbound Call
381404
* [ ] Inbound Call
382405
* [ ] Text-To-Speech Call
383406
* [ ] Text-To-Speech Prompt

src/Application/Client.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@ class Client implements ClientAwareInterface, CollectionInterface
2121
use ClientAwareTrait;
2222
use CollectionTrait;
2323

24-
public function getCollectionName()
24+
public static function getCollectionName()
2525
{
2626
return 'applications';
2727
}
2828

29-
public function getCollectionPath()
29+
public static function getCollectionPath()
3030
{
31-
return '/v1/' . $this->getCollectionName();
31+
return '/v1/' . self::getCollectionName();
32+
}
33+
34+
public function hydrateEntity($data, $id)
35+
{
36+
$application = new Application($id);
37+
$application->jsonUnserialize($data);
38+
return $application;
3239
}
3340

3441
public function get($application)

src/Call/Call.php

+300
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
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\Call;
10+
11+
use Nexmo\Client\ClientAwareInterface;
12+
use Nexmo\Client\ClientAwareTrait;
13+
use Nexmo\Conversations\Conversation;
14+
use Nexmo\Entity\EntityInterface;
15+
use Nexmo\Entity\JsonResponseTrait;
16+
use Nexmo\Entity\JsonSerializableTrait;
17+
use Nexmo\Entity\JsonUnserializableInterface;
18+
use Nexmo\Entity\NoRequestResponseTrait;
19+
use Psr\Http\Message\ResponseInterface;
20+
use Nexmo\Client\Exception;
21+
use Zend\Diactoros\Request;
22+
23+
/**
24+
* Class Call
25+
*
26+
* @property \Nexmo\Call\Stream $stream
27+
* @property \Nexmo\Call\Talk $talk
28+
* @property \Nexmo\Call\Dtmf $dtmf
29+
*
30+
* @method \Nexmo\Call\Stream stream()
31+
* @method \Nexmo\Call\Talk talk()
32+
* @method \Nexmo\Call\Dtmf dtmf()
33+
*/
34+
class Call implements EntityInterface, \JsonSerializable, JsonUnserializableInterface, ClientAwareInterface
35+
{
36+
use NoRequestResponseTrait;
37+
use JsonSerializableTrait;
38+
use JsonResponseTrait;
39+
use ClientAwareTrait;
40+
41+
const WEBHOOK_ANSWER = 'answer';
42+
const WEBHOOK_EVENT = 'event';
43+
44+
const TIMER_LENGTH = 'length';
45+
const TIMER_RINGING = 'ringing';
46+
47+
const TIMEOUT_MACHINE = 'machine';
48+
49+
protected $id;
50+
51+
protected $to;
52+
53+
protected $from;
54+
55+
/**
56+
* @var Webhook[]
57+
*/
58+
protected $webhooks = [];
59+
60+
protected $data = [];
61+
62+
protected $subresources = [];
63+
64+
public function __construct($id = null)
65+
{
66+
$this->id = $id;
67+
}
68+
69+
public function get()
70+
{
71+
$request = new Request(
72+
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId()
73+
,'GET'
74+
);
75+
76+
$response = $this->getClient()->send($request);
77+
78+
if($response->getStatusCode() != '200'){
79+
throw $this->getException($response);
80+
}
81+
82+
$data = json_decode($response->getBody()->getContents(), true);
83+
$this->jsonUnserialize($data);
84+
85+
return $this;
86+
}
87+
88+
protected function getException(ResponseInterface $response)
89+
{
90+
$body = json_decode($response->getBody()->getContents(), true);
91+
$status = $response->getStatusCode();
92+
93+
if($status >= 400 AND $status < 500) {
94+
$e = new Exception\Request($body['error_title'], $status);
95+
} elseif($status >= 500 AND $status < 600) {
96+
$e = new Exception\Server($body['error_title'], $status);
97+
} else {
98+
$e = new Exception\Exception('Unexpected HTTP Status Code');
99+
}
100+
101+
return $e;
102+
}
103+
104+
public function put($payload)
105+
{
106+
$request = new Request(
107+
\Nexmo\Client::BASE_API . Collection::getCollectionPath() . '/' . $this->getId()
108+
,'PUT',
109+
'php://temp',
110+
['content-type' => 'application/json']
111+
);
112+
113+
$request->getBody()->write(json_encode($payload));
114+
$response = $this->client->send($request);
115+
116+
if($response->getStatusCode() != '200'){
117+
throw $this->getException($response);
118+
}
119+
120+
return $this;
121+
}
122+
123+
public function getId()
124+
{
125+
return $this->id;
126+
}
127+
128+
public function setTo($endpoint)
129+
{
130+
if(!($endpoint instanceof Endpoint)){
131+
$endpoint = new Endpoint($endpoint);
132+
}
133+
134+
$this->to = $endpoint;
135+
return $this;
136+
}
137+
138+
/**
139+
* @return Endpoint
140+
*/
141+
public function getTo()
142+
{
143+
if($this->lazyLoad()){
144+
return new Endpoint($this->data['to']['number'], $this->data['to']['type']);
145+
}
146+
147+
return $this->to;
148+
}
149+
150+
public function setFrom($endpoint)
151+
{
152+
if(!($endpoint instanceof Endpoint)){
153+
$endpoint = new Endpoint($endpoint);
154+
}
155+
156+
$this->from = $endpoint;
157+
return $this;
158+
}
159+
160+
/**
161+
* @return Endpoint
162+
*/
163+
public function getFrom()
164+
{
165+
if($this->lazyLoad()){
166+
return new Endpoint($this->data['from']['number'], $this->data['from']['type']);
167+
}
168+
169+
return $this->from;
170+
}
171+
172+
public function setWebhook($type, $url = null, $method = null)
173+
{
174+
if($type instanceof Webhook){
175+
$this->webhooks[$type->getType()] = $type;
176+
return $this;
177+
}
178+
179+
if(is_null($url)){
180+
throw new \InvalidArgumentException('must provide `Nexmo\Call\Webhook` object, or a type and url: missing url' );
181+
}
182+
183+
$this->webhooks[$type] = new Webhook($type, $url, $method);
184+
return $this;
185+
}
186+
187+
public function setTimer($type, $length)
188+
{
189+
$this->data[$type . '_timer'] = $length;
190+
}
191+
192+
public function setTimeout($type, $length)
193+
{
194+
$this->data[$type . '_timeout'] = $length;
195+
}
196+
197+
public function getStatus()
198+
{
199+
if($this->lazyLoad()){
200+
return $this->data['status'];
201+
}
202+
}
203+
204+
public function getDirection()
205+
{
206+
if($this->lazyLoad()){
207+
return $this->data['direction'];
208+
}
209+
}
210+
211+
public function getConversation()
212+
{
213+
if($this->lazyLoad()){
214+
return new Conversation($this->data['conversation_uuid']);
215+
}
216+
}
217+
218+
/**
219+
* Returns true if the resource data is loaded.
220+
*
221+
* Will attempt to load the data if it's not already.
222+
*
223+
* @return bool
224+
*/
225+
protected function lazyLoad()
226+
{
227+
if(!empty($this->data)){
228+
return true;
229+
}
230+
231+
if(isset($this->id)){
232+
$this->get($this);
233+
return true;
234+
}
235+
236+
return false;
237+
}
238+
239+
public function __get($name)
240+
{
241+
switch($name){
242+
case 'stream':
243+
case 'talk':
244+
case 'dtmf':
245+
return $this->lazySubresource(ucfirst($name));
246+
default:
247+
throw new \RuntimeException('property does not exist: ' . $name);
248+
}
249+
}
250+
251+
public function __call($name, $arguments)
252+
{
253+
switch($name){
254+
case 'stream':
255+
case 'talk':
256+
case 'dtmf':
257+
$entity = $this->lazySubresource(ucfirst($name));
258+
return call_user_func_array($entity, $arguments);
259+
default:
260+
throw new \RuntimeException('method does not exist: ' . $name);
261+
}
262+
}
263+
264+
protected function lazySubresource($type)
265+
{
266+
if(!isset($this->subresources[$type])){
267+
$class = 'Nexmo\Call\\' . $type;
268+
$instance = new $class($this->getId());
269+
$instance->setClient($this->getClient());
270+
$this->subresources[$type] = $instance;
271+
}
272+
273+
return $this->subresources[$type];
274+
}
275+
276+
public function jsonSerialize()
277+
{
278+
$data = $this->data;
279+
280+
if(isset($this->to)){
281+
$data['to'] = [$this->to->jsonSerialize()];
282+
}
283+
284+
if(isset($this->from)){
285+
$data['from'] = $this->from->jsonSerialize();
286+
}
287+
288+
foreach($this->webhooks as $webhook){
289+
$data = array_merge($data, $webhook->jsonSerialize());
290+
}
291+
292+
return $data;
293+
}
294+
295+
public function jsonUnserialize(array $json)
296+
{
297+
$this->data = $json;
298+
$this->id = $json['uuid'];
299+
}
300+
}

0 commit comments

Comments
 (0)