Completed
Branch 3.0 (ea0c88)
by Brian
11:51
created

Channels::hold()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
3
/*
4
 * Copyright 2014 Brian Smith <[email protected]>.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *      http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace phparia\Api;
20
21
use GuzzleHttp\Exception\RequestException;
22
use phparia\Resources\Channel;
23
use phparia\Resources\Variable;
24
use phparia\Exception\ConflictException;
25
use phparia\Exception\InvalidParameterException;
26
use phparia\Exception\NotFoundException;
27
use phparia\Exception\ServerException;
28
29
/**
30
 * Channels API
31
 *
32
 * @author Brian Smith <[email protected]>
33
 */
34
class Channels extends MediaBase
35
{
36
    const AST_STATE_DOWN = 'Down'; // Channel is down and available
37
    const AST_STATE_RESERVED = 'Rsrvd'; // Channel is down, but reserved
38
    const AST_STATE_OFFHOOK = 'OffHook'; // Channel is off hook
39
    const AST_STATE_DIALING = 'Dialing'; // Digits (or equivalent) have been dialed
40
    const AST_STATE_RING = 'Ring'; // Line is ringing
41
    const AST_STATE_RINGING = 'Ringing'; // Remote end is ringing
42
    const AST_STATE_UP = 'Up'; // Line is up
43
    const AST_STATE_BUSY = 'Busy'; // Line is busy
44
    const AST_STATE_DIALING_OFFHOOK = 'Dialing Offhook'; // Digits (or equivalent) have been dialed while offhook
45
    const AST_STATE_PRERING = 'Pre-ring'; // Channel has detected an incoming call and is waiting for ring
46
    const AST_STATE_MUTE = 'Mute'; // Do not transmit voice data
47
    const AST_STATE_UNKNOWN = 'Unknown';
48
49
    /**
50
     * List all active channels in Asterisk.
51
     *
52
     * @return Channel[]
53
     */
54 View Code Duplication
    public function getChannels()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $uri = 'channels';
57
        $response = $this->client->getEndpoint()->get($uri);
58
59
        $channels = [];
60
        foreach (\GuzzleHttp\json_decode($response->getBody()) as $channel) {
61
            $channels[] = new Channel($this->client, $channel);
62
        }
63
64
        return $channels;
65
    }
66
67
    /**
68
     * Create a new channel (originate). The new channel is created immediately and a snapshot of it
69
     * returned. If a Stasis application is provided it will be automatically subscribed to the originated
70
     * channel for further events and updates.
71
     *
72
     * @param string $endpoint (required) Endpoint to call.
73
     * @param string $extension The extension to dial after the endpoint answers
74
     * @param string $context The context to dial after the endpoint answers. If omitted, uses 'default'
75
     * @param int $priority The priority to dial after the endpoint answers. If omitted, uses 1
76
     * @param string $label Asterisk 13+ The label to dial after the endpoint answers. Will supersede 'priority' if provided. Mutually exclusive with 'app'.
77
     * @param string $app The application that is subscribed to the originated channel. When the channel is answered, it will be passed to this Stasis application. Mutually exclusive with 'context', 'extension', 'priority', and 'label'.
78
     * @param string $appArgs The application arguments to pass to the Stasis application.
79
     * @param string $callerId CallerID to use when dialing the endpoint or extension.
80
     * @param int $timeout (default 30) Timeout (in seconds) before giving up dialing, or -1 for no timeout.
81
     * @param string $channelId The unique id to assign the channel on creation.
82
     * @param string $otherChannelId The unique id to assign the second channel when using local channels.
83
     * @param array $variables The "variables" key in the body object holds variable key/value pairs to set on the channel on creation. Other keys in the body object are interpreted as query parameters. Ex. { "endpoint": "SIP/Alice", "variables": { "CALLERID(name)": "Alice" } }
84
     * @return Channel
85
     * @throws InvalidParameterException
86
     * @throws ServerException
87
     */
88 35 View Code Duplication
    public function createChannel(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
        $endpoint,
90
        $extension = null,
91
        $context = null,
92
        $priority = null,
93
        $label = null,
94
        $app = null,
95
        $appArgs = null,
96
        $callerId = null,
97
        $timeout = null,
98
        $channelId = null,
99
        $otherChannelId = null,
100
        $variables = array()
101
    ) {
102 35
        $uri = 'channels';
103
        try {
104 35
            $response = $this->client->getEndpoint()->post($uri, [
105
                'form_params' => [
106 35
                    'endpoint' => $endpoint,
107 35
                    'extension' => $extension,
108 35
                    'context' => $context,
109 35
                    'priority' => $priority,
110 35
                    'label' => $label,
111 35
                    'app' => $app,
112 35
                    'appArgs' => $appArgs,
113 35
                    'callerId' => $callerId,
114 35
                    'timeout' => $timeout,
115 35
                    'channelId' => $channelId,
116 35
                    'otherChannelId' => $otherChannelId,
117 35
                    'variables' => array_map('strval', $variables),
118
                ]
119 35
            ]);
120 35
        } catch (RequestException $e) {
121
            $this->processRequestException($e);
122
        }
123
124 35
        return new Channel($this->client, \GuzzleHttp\json_decode($response->getBody()));
125
    }
126
127
    /**
128
     * Channel details.
129
     *
130
     * @param string $channelId
131
     * @return Channel
132
     * @throws NotFoundException
133
     */
134 View Code Duplication
    public function getChannel($channelId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136
        $uri = "channels/$channelId";
137
        try {
138
            $response = $this->client->getEndpoint()->get($uri);
139
        } catch (RequestException $e) {
140
            $this->processRequestException($e);
141
        }
142
143
        return new Channel($this->client, \GuzzleHttp\json_decode($response->getBody()));
144
    }
145
146
    /**
147
     * Create a new channel (originate). The new channel is created immediately and a snapshot of it
148
     * returned. If a Stasis application is provided it will be automatically subscribed to the originated
149
     * channel for further events and updates.
150
     *
151
     * @param string $endpoint (required) Endpoint to call.
152
     * @param string $extension The extension to dial after the endpoint answers
153
     * @param string $context The context to dial after the endpoint answers. If omitted, uses 'default'
154
     * @param int $priority The priority to dial after the endpoint answers. If omitted, uses 1
155
     * @param string $label Asterisk 13+ The label to dial after the endpoint answers. Will supersede 'priority' if provided. Mutually exclusive with 'app'.
156
     * @param string $app The application that is subscribed to the originated channel, and passed to the Stasis application.
157
     * @param string $appArgs The application arguments to pass to the Stasis application.
158
     * @param string $callerId CallerID to use when dialing the endpoint or extension.
159
     * @param int $timeout (default 30) Timeout (in seconds) before giving up dialing, or -1 for no timeout.
160
     * @param string $channelId The unique id to assign the channel on creation.
161
     * @param string $otherChannelId The unique id to assign the second channel when using local channels.
162
     * @param array $variables The "variables" key in the body object holds variable key/value pairs to set on the channel on creation. Other keys in the body object are interpreted as query parameters. Ex. { "endpoint": "SIP/Alice", "variables": { "CALLERID(name)": "Alice" } }
163
     * @return Channel
164
     * @throws InvalidParameterException
165
     */
166 View Code Duplication
    public function createChannelWithId(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
        $endpoint,
168
        $extension = null,
169
        $context = null,
170
        $priority = null,
171
        $label = null,
172
        $app = null,
173
        $appArgs = null,
174
        $callerId = null,
175
        $timeout = null,
176
        $channelId = null,
177
        $otherChannelId = null,
178
        $variables = array()
179
    ) {
180
        $uri = "channels/$channelId";
181
        try {
182
            $response = $this->client->getEndpoint()->post($uri, [
183
                'form_params' => [
184
                    'endpoint' => $endpoint,
185
                    'extension' => $extension,
186
                    'context' => $context,
187
                    'priority' => $priority,
188
                    'label' => $label,
189
                    'app' => $app,
190
                    'appArgs' => $appArgs,
191
                    'callerId' => $callerId,
192
                    'timeout' => $timeout,
193
                    'otherChannelId' => $otherChannelId,
194
                    'variables' => array_map('strval', $variables),
195
                ]
196
            ]);
197
        } catch (RequestException $e) {
198
            $this->processRequestException($e);
199
        }
200
201
        return new Channel($this->client, \GuzzleHttp\json_decode($response->getBody()));
202
    }
203
204
    /**
205
     * Delete (i.e. hangup) a channel.
206
     *
207
     * @param string $channelId Channel's id
208
     * @throws NotFoundException
209
     */
210
    public function deleteChannel($channelId)
211
    {
212
        $uri = "channels/$channelId";
213
        try {
214
            $this->client->getEndpoint()->delete($uri);
215
        } catch (RequestException $e) {
216
            $this->processRequestException($e);
217
        }
218
    }
219
220
    /**
221
     * Hangup a channel if it still exists.
222
     *
223
     * @param string $channelId Channel's id
224
     */
225
    public function hangup($channelId)
226
    {
227
        try {
228
            $this->deleteChannel($channelId);
229
        } catch (\Exception $ignore) {
230
            // Don't throw exception if the channel doesn't exist
231
        }
232
    }
233
234
    /**
235
     * Exit application; continue execution in the dialplan.
236
     *
237
     * @param string $channelId Channel's id
238
     * @param string $context The context to continue to.
239
     * @param string $extension The extension to continue to.
240
     * @param int $priority The priority to continue to.
241
     * @throws NotFoundException
242
     * @throws ConflictException
243
     */
244
    public function continueDialplan($channelId, $context, $extension, $priority)
245
    {
246
        $uri = "channels/$channelId/continue";
247
        try {
248
            $this->client->getEndpoint()->post($uri, [
249
                'form_params' => [
250
                    'context' => $context,
251
                    'extension' => $extension,
252
                    'priority' => $priority,
253
                ]
254
            ]);
255
        } catch (RequestException $e) {
256
            $this->processRequestException($e);
257
        }
258
    }
259
260
    /**
261
     * Answer a channel.
262
     *
263
     * @param string $channelId Channel's id
264
     * @throws NotFoundException
265
     * @throws ConflictException
266
     */
267 34
    public function answer($channelId)
268
    {
269 34
        $uri = "channels/$channelId/answer";
270
        try {
271 34
            $this->client->getEndpoint()->post($uri);
272 34
        } catch (RequestException $e) {
273
            $this->processRequestException($e);
274
        }
275 34
    }
276
277
    /**
278
     * Indicate ringing to a channel.
279
     *
280
     * @param string $channelId
281
     * @throws NotFoundException
282
     * @throws ConflictException
283
     */
284
    public function startRinging($channelId)
285
    {
286
        $uri = "channels/$channelId/ring";
287
        try {
288
            $this->client->getEndpoint()->post($uri);
289
        } catch (RequestException $e) {
290
            $this->processRequestException($e);
291
        }
292
    }
293
294
    /**
295
     * Stop ringing indication on a channel if locally generated.
296
     *
297
     * @param string $channelId
298
     * @throws NotFoundException
299
     * @throws ConflictException
300
     */
301
    public function stopRinging($channelId)
302
    {
303
        $uri = "channels/$channelId/ring";
304
        try {
305
            $this->client->getEndpoint()->delete($uri);
306
        } catch (RequestException $e) {
307
            $this->processRequestException($e);
308
        }
309
    }
310
311
    /**
312
     * Send provided DTMF to a given channel.
313
     *
314
     * @param string $channelId
315
     * @param string $dtmf DTMF To send.
316
     * @param int $before Amount of time to wait before DTMF digits (specified in milliseconds) start.
317
     * @param int $between Amount of time in between DTMF digits (specified in milliseconds).  Default: 100
318
     * @param int $duration Length of each DTMF digit (specified in milliseconds).  Default: 100
319
     * @param int $after Amount of time to wait after DTMF digits (specified in milliseconds) end.
320
     * @throws InvalidParameterException
321
     * @throws NotFoundException
322
     * @throws ConflictException
323
     */
324
    public function sendDtmf($channelId, $dtmf, $before = null, $between = null, $duration = null, $after = null)
325
    {
326
        $uri = "channels/$channelId/dtmf";
327
        try {
328
            $this->client->getEndpoint()->post($uri, [
329
                'form_params' => [
330
                    'dtmf' => $dtmf,
331
                    'before' => $before,
332
                    'between' => $between,
333
                    'duration' => $duration,
334
                    'after' => $after,
335
                ]
336
            ]);
337
        } catch (RequestException $e) {
338
            $this->processRequestException($e);
339
        }
340
    }
341
342
    /**
343
     * Mute a channel.
344
     *
345
     * @param string $channelId Channel's id
346
     * @param string $direction (default both) Direction in which to mute audio.  Allowed values: both, in, out
347
     * @throws NotFoundException
348
     * @throws ConflictException
349
     */
350
    public function mute($channelId, $direction)
351
    {
352
        $uri = "channels/$channelId/mute";
353
        try {
354
            $this->client->getEndpoint()->post($uri, [
355
                'form_params' => [
356
                    'direction' => $direction,
357
                ]
358
            ]);
359
        } catch (RequestException $e) {
360
            $this->processRequestException($e);
361
        }
362
    }
363
364
    /**
365
     * Unmute a channel.
366
     *
367
     * @param string $channelId Channel's id
368
     * @param string $direction (default both) Direction in which to unmute audio
369
     * @throws NotFoundException
370
     * @throws ConflictException
371
     */
372 View Code Duplication
    public function unmute($channelId, $direction)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
373
    {
374
        $uri = "channels/$channelId/mute?direction=".\GuzzleHttp\json_encode($direction);
375
        try {
376
            $this->client->getEndpoint()->delete($uri);
377
        } catch (RequestException $e) {
378
            $this->processRequestException($e);
379
        }
380
    }
381
382
    /**
383
     * Hold a channel.
384
     *
385
     * @param string $channelId Channel's id
386
     * @throws NotFoundException
387
     * @throws ConflictException
388
     */
389
    public function hold($channelId)
390
    {
391
        $uri = "channels/$channelId/hold";
392
        try {
393
            $this->client->getEndpoint()->post($uri);
394
        } catch (RequestException $e) {
395
            $this->processRequestException($e);
396
        }
397
    }
398
399
    /**
400
     * Remove a channel from hold.
401
     *
402
     * @param string $channelId Channel's id
403
     * @throws NotFoundException
404
     * @throws ConflictException
405
     */
406
    public function unhold($channelId)
407
    {
408
        $uri = "channels/$channelId/hold";
409
        try {
410
            $this->client->getEndpoint()->delete($uri);
411
        } catch (RequestException $e) {
412
            $this->processRequestException($e);
413
        }
414
    }
415
416
    /**
417
     * Play silence to a channel. Using media operations such as /play on a channel playing silence in this manner will suspend silence without resuming automatically.
418
     *
419
     * @param string $channelId Channel's id
420
     * @throws NotFoundException
421
     * @throws ConflictException
422
     */
423
    public function startSilence($channelId)
424
    {
425
        $uri = "channels/$channelId/silence";
426
        try {
427
            $this->client->getEndpoint()->post($uri);
428
        } catch (RequestException $e) {
429
            $this->processRequestException($e);
430
        }
431
    }
432
433
    /**
434
     * Stop playing silence to a channel.
435
     *
436
     * @param string $channelId Channel's id
437
     * @throws NotFoundException
438
     * @throws ConflictException
439
     */
440
    public function stopSilence($channelId)
441
    {
442
        $uri = "channels/$channelId/silence";
443
        try {
444
            $this->client->getEndpoint()->delete($uri);
445
        } catch (RequestException $e) {
446
            $this->processRequestException($e);
447
        }
448
    }
449
450
    /**
451
     * Get the value of a channel variable or function.
452
     *
453
     * @param string $channelId
454
     * @param string $variable
455
     * @param null|string $default The value to return if the variable does not exist
456
     * @return string|Variable
457
     * @throws ConflictException
458
     * @throws InvalidParameterException
459
     * @throws NotFoundException
460
     */
461
    public function getVariable($channelId, $variable, $default = null)
0 ignored issues
show
Unused Code introduced by
The parameter $default is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
462
    {
463
        $uri = "channels/$channelId/variable";
464
        try {
465
            $response = $this->client->getEndpoint()->get($uri, [
466
                'form_params' => [
467
                    'variable' => $variable,
468
                ]
469
            ]);
470
        } catch (RequestException $e) {
471
            $this->processRequestException($e);
472
        }
473
474
        return new Variable(\GuzzleHttp\json_decode($response->getBody()));
475
    }
476
477
    /**
478
     * Set the value of a channel variable or function.
479
     *
480
     * @param string $channelId
481
     * @param string $variable
482
     * @param string $value
483
     * @return Variable
484
     * @throws InvalidParameterException
485
     * @throws NotFoundException
486
     * @throws ConflictException
487
     */
488
    public function setVariable($channelId, $variable, $value)
489
    {
490
        $uri = "channels/$channelId/variable";
491
        try {
492
            $response = $this->client->getEndpoint()->post($uri, [
493
                'form_params' => [
494
                    'variable' => $variable,
495
                    'value' => $value,
496
                ]
497
            ]);
498
        } catch (RequestException $e) {
499
            $this->processRequestException($e);
500
        }
501
502
        return new Variable(\GuzzleHttp\json_decode($response->getBody()));
503
    }
504
505
    /**
506
     * Start snooping. Snoop (spy/whisper) on a specific channel.
507
     *
508
     * @param string $channelId Channel's id
509
     * @param string $spy (default none) Direction of audio to spy on
510
     * @param string $whisper (default none) Direction of audio to whisper into
511
     * @param string $app (required) Application the snooping channel is placed into
512
     * @param string $appArgs The application arguments to pass to the Stasis application
513
     * @param string $snoopId Unique ID to assign to snooping channel
514
     * @return Channel
515
     * @throws InvalidParameterException
516
     * @throws NotFoundException
517
     */
518 View Code Duplication
    public function startSnoop($channelId, $spy, $whisper, $app, $appArgs, $snoopId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
519
    {
520
        $uri = "channels/$channelId/snoop";
521
        try {
522
            $response = $this->client->getEndpoint()->post($uri, [
523
                'form_params' => [
524
                    'spy' => $spy,
525
                    'whisper' => $whisper,
526
                    'app' => $app,
527
                    'appArgs' => $appArgs,
528
                    'snoopId' => $snoopId,
529
                ]
530
            ]);
531
        } catch (RequestException $e) {
532
            $this->processRequestException($e);
533
        }
534
535
        return new Channel($this->client, \GuzzleHttp\json_decode($response->getBody()));
536
    }
537
538
    /**
539
     * Start snooping. Snoop (spy/whisper) on a specific channel.
540
     *
541
     * @param string $channelId Channel's id
542
     * @param string $spy (default none) Direction of audio to spy on
543
     * @param string $whisper (default none) Direction of audio to whisper into
544
     * @param string $app (required) Application the snooping channel is placed into
545
     * @param string $appArgs The application arguments to pass to the Stasis application
546
     * @param string $snoopId Unique ID to assign to snooping channel
547
     * @return Channel
548
     * @throws InvalidParameterException
549
     * @throws NotFoundException
550
     */
551 View Code Duplication
    public function startSnoopWithId($channelId, $spy, $whisper, $app, $appArgs, $snoopId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
552
    {
553
        $uri = "channels/$channelId/snoop/$snoopId";
554
        try {
555
            $response = $this->client->getEndpoint()->post($uri, [
556
                'form_params' => [
557
                    'spy' => $spy,
558
                    'whisper' => $whisper,
559
                    'app' => $app,
560
                    'appArgs' => $appArgs,
561
                ]
562
            ]);
563
        } catch (RequestException $e) {
564
            $this->processRequestException($e);
565
        }
566
567
        return new Channel($this->client, \GuzzleHttp\json_decode($response->getBody()));
568
    }
569
570
    /**
571
     * @return string
572
     */
573 6
    public function getType()
574
    {
575 6
        return 'channels';
576
    }
577
}
578