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