Completed
Push — master ( ccad89...700c1b )
by Brian
9s
created

src/wormling/phparia/Api/Channels.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Exception\MissingParameterException;
23
use phparia\Exception\PreconditionFailedException;
24
use phparia\Exception\UnprocessableEntityException;
25
use phparia\Resources\Channel;
26
use phparia\Resources\Variable;
27
use phparia\Exception\ConflictException;
28
use phparia\Exception\InvalidParameterException;
29
use phparia\Exception\NotFoundException;
30
use phparia\Exception\ServerException;
31
32
/**
33
 * Channels API
34
 *
35
 * @author Brian Smith <[email protected]>
36
 */
37
class Channels extends MediaBase
38
{
39
    const AST_STATE_DOWN = 'Down'; // Channel is down and available
40
    const AST_STATE_RESERVED = 'Rsrvd'; // Channel is down, but reserved
41
    const AST_STATE_OFFHOOK = 'OffHook'; // Channel is off hook
42
    const AST_STATE_DIALING = 'Dialing'; // Digits (or equivalent) have been dialed
43
    const AST_STATE_RING = 'Ring'; // Line is ringing
44
    const AST_STATE_RINGING = 'Ringing'; // Remote end is ringing
45
    const AST_STATE_UP = 'Up'; // Line is up
46
    const AST_STATE_BUSY = 'Busy'; // Line is busy
47
    const AST_STATE_DIALING_OFFHOOK = 'Dialing Offhook'; // Digits (or equivalent) have been dialed while offhook
48
    const AST_STATE_PRERING = 'Pre-ring'; // Channel has detected an incoming call and is waiting for ring
49
    const AST_STATE_MUTE = 'Mute'; // Do not transmit voice data
50
    const AST_STATE_UNKNOWN = 'Unknown';
51
52
    /**
53
     * List all active channels in Asterisk.
54
     *
55
     * @return Channel[]
56
     */
57 98 View Code Duplication
    public function getChannels()
0 ignored issues
show
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...
58
    {
59 98
        $uri = '/channels';
60 98
        $response = $this->client->getEndpoint()->get($uri);
61
62 98
        $channels = [];
63 98
        foreach (\GuzzleHttp\json_decode($response->getBody()) as $channel) {
64 36
            $channels[] = new Channel($this->client, $channel);
65 98
        }
66
67 98
        return $channels;
68
    }
69
70
    /**
71
     * Create a new channel (originate). The new channel is created immediately and a snapshot of it
72
     * returned. If a Stasis application is provided it will be automatically subscribed to the originated
73
     * channel for further events and updates.
74
     *
75
     * @param string $endpoint (required) Endpoint to call.
76
     * @param string $extension The extension to dial after the endpoint answers
77
     * @param string $context The context to dial after the endpoint answers. If omitted, uses 'default'
78
     * @param int $priority The priority to dial after the endpoint answers. If omitted, uses 1
79
     * @param string $label Asterisk 13+ The label to dial after the endpoint answers. Will supersede 'priority' if provided. Mutually exclusive with 'app'.
80
     * @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'.
81
     * @param string $appArgs The application arguments to pass to the Stasis application.
82
     * @param string $callerId CallerID to use when dialing the endpoint or extension.
83
     * @param int $timeout (default 30) Timeout (in seconds) before giving up dialing, or -1 for no timeout.
84
     * @param string $channelId The unique id to assign the channel on creation.
85
     * @param string $otherChannelId The unique id to assign the second channel when using local channels.
86
     * @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" } }
87
     * @return Channel
88
     * @throws InvalidParameterException
89
     * @throws ServerException
90
     */
91 38 View Code Duplication
    public function createChannel(
92
        $endpoint,
93
        $extension = null,
94
        $context = null,
95
        $priority = null,
96
        $label = null,
97
        $app = null,
98
        $appArgs = null,
99
        $callerId = null,
100
        $timeout = null,
101
        $channelId = null,
102
        $otherChannelId = null,
103
        $variables = array()
104
    ) {
105 38
        $uri = 'channels';
106
        try {
107 38
            $response = $this->client->getEndpoint()->post($uri, [
108 38
                'form_params' => [
109 38
                    'endpoint' => $endpoint,
110 38
                    'extension' => $extension,
111 38
                    'context' => $context,
112 38
                    'priority' => $priority,
113 38
                    'label' => $label,
114 38
                    'app' => $app,
115 38
                    'appArgs' => $appArgs,
116 38
                    'callerId' => $callerId,
117 38
                    'timeout' => $timeout,
118 38
                    'channelId' => $channelId,
119 38
                    'otherChannelId' => $otherChannelId,
120 38
                    'variables' => array_map('strval', $variables),
121 38
                ]
122
            ]);
123
        } catch (RequestException $e) {
124
            $this->processRequestException($e);
125
        }
126
127 38
        return new Channel($this->client, \GuzzleHttp\json_decode($response->getBody()));
128
    }
129
130
    /**
131
     * Channel details.
132
     *
133
     * @param string $channelId
134
     * @return Channel
135
     * @throws NotFoundException
136
     */
137 View Code Duplication
    public function getChannel($channelId)
138
    {
139
        $uri = "channels/$channelId";
140
        try {
141
            $response = $this->client->getEndpoint()->get($uri);
142
        } catch (RequestException $e) {
143
            $this->processRequestException($e);
144
        }
145
146
        return new Channel($this->client, \GuzzleHttp\json_decode($response->getBody()));
147
    }
148
149
    /**
150
     * Create a new channel (originate). The new channel is created immediately and a snapshot of it
151
     * returned. If a Stasis application is provided it will be automatically subscribed to the originated
152
     * channel for further events and updates.
153
     *
154
     * @param string $endpoint (required) Endpoint to call.
155
     * @param string $extension The extension to dial after the endpoint answers
156
     * @param string $context The context to dial after the endpoint answers. If omitted, uses 'default'
157
     * @param int $priority The priority to dial after the endpoint answers. If omitted, uses 1
158
     * @param string $label Asterisk 13+ The label to dial after the endpoint answers. Will supersede 'priority' if provided. Mutually exclusive with 'app'.
159
     * @param string $app The application that is subscribed to the originated channel, and passed to the Stasis application.
160
     * @param string $appArgs The application arguments to pass to the Stasis application.
161
     * @param string $callerId CallerID to use when dialing the endpoint or extension.
162
     * @param int $timeout (default 30) Timeout (in seconds) before giving up dialing, or -1 for no timeout.
163
     * @param string $channelId The unique id to assign the channel on creation.
164
     * @param string $otherChannelId The unique id to assign the second channel when using local channels.
165
     * @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" } }
166
     * @return Channel
167
     * @throws InvalidParameterException
168
     * @throws ServerException
169
     */
170 View Code Duplication
    public function createChannelWithId(
171
        $endpoint,
172
        $extension = null,
173
        $context = null,
174
        $priority = null,
175
        $label = null,
176
        $app = null,
177
        $appArgs = null,
178
        $callerId = null,
179
        $timeout = null,
180
        $channelId = null,
181
        $otherChannelId = null,
182
        $variables = array()
183
    ) {
184
        $uri = "channels/$channelId";
185
        try {
186
            $response = $this->client->getEndpoint()->post($uri, [
187
                'form_params' => [
188
                    'endpoint' => $endpoint,
189
                    'extension' => $extension,
190
                    'context' => $context,
191
                    'priority' => $priority,
192
                    'label' => $label,
193
                    'app' => $app,
194
                    'appArgs' => $appArgs,
195
                    'callerId' => $callerId,
196
                    'timeout' => $timeout,
197
                    'otherChannelId' => $otherChannelId,
198
                    'variables' => array_map('strval', $variables),
199
                ]
200
            ]);
201
        } catch (RequestException $e) {
202
            $this->processRequestException($e);
203
        }
204
205
        return new Channel($this->client, \GuzzleHttp\json_decode($response->getBody()));
206
    }
207
208
    /**
209
     * Delete (i.e. hangup) a channel.
210
     *
211 37
     * @param string $channelId Channel's id
212
     * @throws NotFoundException
213 37
     */
214
    public function deleteChannel($channelId)
215 37
    {
216 37
        $uri = "channels/$channelId";
217 37
        try {
218
            $this->client->getEndpoint()->delete($uri);
219 37
        } catch (RequestException $e) {
220
            $this->processRequestException($e);
221
        }
222
    }
223
224
    /**
225
     * Hangup a channel if it still exists.
226 37
     *
227
     * @param string $channelId Channel's id
228
     */
229 37
    public function hangup($channelId)
230 37
    {
231
        try {
232
            $this->deleteChannel($channelId);
233 37
        } catch (\Exception $ignore) {
234
            // Don't throw exception if the channel doesn't exist
235
        }
236
    }
237
238
    /**
239
     * Exit application; continue execution in the dialplan.
240
     *
241
     * @param string $channelId Channel's id
242
     * @param string $context The context to continue to.
243
     * @param string $extension The extension to continue to.
244
     * @param int $priority The priority to continue to.
245
     * @throws NotFoundException
246
     * @throws ConflictException
247
     */
248
    public function continueDialplan($channelId, $context, $extension, $priority)
249
    {
250
        $uri = "channels/$channelId/continue";
251
        try {
252
            $this->client->getEndpoint()->post($uri, [
253
                'form_params' => [
254
                    'context' => $context,
255
                    'extension' => $extension,
256
                    'priority' => $priority,
257
                ]
258
            ]);
259
        } catch (RequestException $e) {
260
            $this->processRequestException($e);
261
        }
262
    }
263
264
    /**
265
     * Redirect the channel to a different location.
266
     *
267
     * @param string $channelId Channel's id
268 37
     * @param string $endpoint (required) The endpoint to redirect the channel to
269
     * @throws MissingParameterException
270 37
     * @throws NotFoundException
271
     * @throws ConflictException
272 37
     * @throws UnprocessableEntityException
273 37
     * @throws PreconditionFailedException
274
     */
275 View Code Duplication
    public function redirect($channelId, $endpoint)
276
    {
277
        $uri = "channels/$channelId/redirect";
278 37
        try {
279
            $this->client->getEndpoint()->post($uri, [
280
                'form_params' => [
281
                    'endpoint' => $endpoint
282
                ]
283
            ]);
284
        } catch (RequestException $e) {
285
            $this->processRequestException($e);
286
        }
287
    }
288
289
    /**
290
     * Answer a channel.
291
     *
292
     * @param string $channelId Channel's id
293
     * @throws NotFoundException
294
     * @throws ConflictException
295
     */
296
    public function answer($channelId)
297
    {
298
        $uri = "channels/$channelId/answer";
299
        try {
300
            $this->client->getEndpoint()->post($uri);
301
        } catch (RequestException $e) {
302
            $this->processRequestException($e);
303
        }
304
    }
305
306
    /**
307
     * Indicate ringing to a channel.
308
     *
309
     * @param string $channelId
310
     * @throws NotFoundException
311
     * @throws ConflictException
312
     */
313
    public function startRinging($channelId)
314
    {
315
        $uri = "channels/$channelId/ring";
316
        try {
317
            $this->client->getEndpoint()->post($uri);
318
        } catch (RequestException $e) {
319
            $this->processRequestException($e);
320
        }
321
    }
322
323
    /**
324
     * Stop ringing indication on a channel if locally generated.
325
     *
326
     * @param string $channelId
327
     * @throws NotFoundException
328
     * @throws ConflictException
329
     */
330
    public function stopRinging($channelId)
331
    {
332
        $uri = "channels/$channelId/ring";
333
        try {
334
            $this->client->getEndpoint()->delete($uri);
335
        } catch (RequestException $e) {
336
            $this->processRequestException($e);
337
        }
338
    }
339
340
    /**
341
     * Send provided DTMF to a given channel.
342
     *
343
     * @param string $channelId
344
     * @param string $dtmf DTMF To send.
345
     * @param int $before Amount of time to wait before DTMF digits (specified in milliseconds) start.
346
     * @param int $between Amount of time in between DTMF digits (specified in milliseconds).  Default: 100
347
     * @param int $duration Length of each DTMF digit (specified in milliseconds).  Default: 100
348
     * @param int $after Amount of time to wait after DTMF digits (specified in milliseconds) end.
349
     * @throws InvalidParameterException
350
     * @throws NotFoundException
351
     * @throws ConflictException
352
     */
353
    public function sendDtmf($channelId, $dtmf, $before = null, $between = null, $duration = null, $after = null)
354
    {
355
        $uri = "channels/$channelId/dtmf";
356
        try {
357
            $this->client->getEndpoint()->post($uri, [
358
                'form_params' => [
359
                    'dtmf' => $dtmf,
360
                    'before' => $before,
361
                    'between' => $between,
362
                    'duration' => $duration,
363
                    'after' => $after,
364
                ]
365
            ]);
366
        } catch (RequestException $e) {
367
            $this->processRequestException($e);
368
        }
369
    }
370
371
    /**
372
     * Mute a channel.
373
     *
374
     * @param string $channelId Channel's id
375
     * @param string $direction (default both) Direction in which to mute audio.  Allowed values: both, in, out
376
     * @throws NotFoundException
377
     * @throws ConflictException
378
     */
379 View Code Duplication
    public function mute($channelId, $direction)
380
    {
381
        $uri = "channels/$channelId/mute";
382
        try {
383
            $this->client->getEndpoint()->post($uri, [
384
                'form_params' => [
385
                    'direction' => $direction,
386
                ]
387
            ]);
388
        } catch (RequestException $e) {
389
            $this->processRequestException($e);
390
        }
391
    }
392
393
    /**
394
     * Unmute a channel.
395
     *
396
     * @param string $channelId Channel's id
397
     * @param string $direction (default both) Direction in which to unmute audio
398
     * @throws NotFoundException
399
     * @throws ConflictException
400
     */
401 View Code Duplication
    public function unmute($channelId, $direction)
402
    {
403
        $uri = "channels/$channelId/mute?direction=".\GuzzleHttp\json_encode($direction);
404
        try {
405
            $this->client->getEndpoint()->delete($uri);
406
        } catch (RequestException $e) {
407
            $this->processRequestException($e);
408
        }
409
    }
410
411
    /**
412
     * Hold a channel.
413
     *
414
     * @param string $channelId Channel's id
415
     * @throws NotFoundException
416
     * @throws ConflictException
417
     */
418
    public function hold($channelId)
419
    {
420
        $uri = "channels/$channelId/hold";
421
        try {
422
            $this->client->getEndpoint()->post($uri);
423
        } catch (RequestException $e) {
424
            $this->processRequestException($e);
425
        }
426
    }
427
428
    /**
429
     * Remove a channel from hold.
430
     *
431
     * @param string $channelId Channel's id
432
     * @throws NotFoundException
433
     * @throws ConflictException
434
     */
435
    public function unhold($channelId)
436
    {
437
        $uri = "channels/$channelId/hold";
438
        try {
439
            $this->client->getEndpoint()->delete($uri);
440
        } catch (RequestException $e) {
441
            $this->processRequestException($e);
442
        }
443
    }
444
445
    /**
446
     * 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.
447
     *
448
     * @param string $channelId Channel's id
449
     * @throws NotFoundException
450
     * @throws ConflictException
451
     */
452
    public function startSilence($channelId)
453
    {
454
        $uri = "channels/$channelId/silence";
455
        try {
456
            $this->client->getEndpoint()->post($uri);
457
        } catch (RequestException $e) {
458
            $this->processRequestException($e);
459
        }
460
    }
461
462
    /**
463
     * Stop playing silence to a channel.
464
     *
465
     * @param string $channelId Channel's id
466
     * @throws NotFoundException
467
     * @throws ConflictException
468
     */
469
    public function stopSilence($channelId)
470
    {
471
        $uri = "channels/$channelId/silence";
472
        try {
473
            $this->client->getEndpoint()->delete($uri);
474
        } catch (RequestException $e) {
475
            $this->processRequestException($e);
476
        }
477
    }
478
479
    /**
480
     * Get the value of a channel variable or function.
481
     *
482
     * @param string $channelId
483
     * @param string $variable
484
     * @param null|string $default The value to return if the variable does not exist
485
     * @return string|Variable
486
     * @throws ConflictException
487
     * @throws InvalidParameterException
488
     * @throws NotFoundException
489
     */
490
    public function getChannelVar($channelId, $variable, $default = null)
491
    {
492
        $uri = "channels/$channelId/variable";
493
        try {
494
            $response = $this->client->getEndpoint()->get($uri, [
495
                'form_params' => [
496
                    'variable' => $variable,
497
                ]
498
            ]);
499
        } catch (RequestException $e) {
500
            try {
501
                $this->processRequestException($e);
502
            } catch (NotFoundException $notFoundException) {
503
                if ($default === null) {
504
                    throw $notFoundException;
505
                }
506
507
                return $default;
508
            }
509
        }
510
511
        return new Variable(\GuzzleHttp\json_decode($response->getBody()));
512
    }
513
514
    /**
515
     * Get the value of a channel variable or function.
516
     *
517
     * @param string $channelId
518
     * @param string $variable
519
     * @param null|string $default The value to return if the variable does not exist
520
     * @return string|Variable
521
     * @throws ConflictException
522
     * @throws InvalidParameterException
523
     * @throws NotFoundException
524
     * @deprecated
525
     */
526
    public function getVariable($channelId, $variable, $default = null)
527
    {
528
        return $this->getChannelVar($channelId, $variable, $default);
529
    }
530
531
    /**
532
     * Set the value of a channel variable or function.
533
     *
534
     * @param string $channelId
535
     * @param string $variable
536
     * @param string $value
537
     * @return Variable
538
     * @throws InvalidParameterException
539
     * @throws NotFoundException
540
     * @throws ConflictException
541
     */
542
    public function setChannelVar($channelId, $variable, $value)
543
    {
544
        $uri = "channels/$channelId/variable";
545
        try {
546
            $response = $this->client->getEndpoint()->post($uri, [
547
                'form_params' => [
548
                    'variable' => $variable,
549
                    'value' => $value,
550
                ]
551
            ]);
552
        } catch (RequestException $e) {
553
            $this->processRequestException($e);
554
        }
555
556
        return new Variable(\GuzzleHttp\json_decode($response->getBody()));
557
    }
558
559
    /**
560
     * Set the value of a channel variable or function.
561
     *
562
     * @param string $channelId
563
     * @param string $variable
564
     * @param string $value
565
     * @return Variable
566
     * @throws InvalidParameterException
567
     * @throws NotFoundException
568
     * @throws ConflictException
569
     * @deprecated
570
     */
571
    public function setVariable($channelId, $variable, $value)
572
    {
573
        return $this->setChannelVar($channelId, $variable, $value);
574
    }
575
576
    /**
577
     * Start snooping. Snoop (spy/whisper) on a specific channel.
578
     *
579
     * @param string $channelId Channel's id
580
     * @param string $spy (default none) Direction of audio to spy on
581
     * @param string $whisper (default none) Direction of audio to whisper into
582
     * @param string $app (required) Application the snooping channel is placed into
583
     * @param string $appArgs The application arguments to pass to the Stasis application
584
     * @param string $snoopId Unique ID to assign to snooping channel
585
     * @return Channel
586
     * @throws InvalidParameterException
587
     * @throws NotFoundException
588
     */
589 View Code Duplication
    public function startSnoop($channelId, $spy, $whisper, $app, $appArgs, $snoopId)
590
    {
591
        $uri = "channels/$channelId/snoop";
592
        try {
593
            $response = $this->client->getEndpoint()->post($uri, [
594
                'form_params' => [
595
                    'spy' => $spy,
596
                    'whisper' => $whisper,
597
                    'app' => $app,
598
                    'appArgs' => $appArgs,
599 6
                    'snoopId' => $snoopId,
600
                ]
601 6
            ]);
602
        } catch (RequestException $e) {
603
            $this->processRequestException($e);
604
        }
605
606
        return new Channel($this->client, \GuzzleHttp\json_decode($response->getBody()));
607
    }
608
609
    /**
610
     * Start snooping. Snoop (spy/whisper) on a specific channel.
611
     *
612
     * @param string $channelId Channel's id
613
     * @param string $spy (default none) Direction of audio to spy on
614
     * @param string $whisper (default none) Direction of audio to whisper into
615
     * @param string $app (required) Application the snooping channel is placed into
616
     * @param string $appArgs The application arguments to pass to the Stasis application
617
     * @param string $snoopId Unique ID to assign to snooping channel
618
     * @return Channel
619
     * @throws InvalidParameterException
620
     * @throws NotFoundException
621
     */
622 View Code Duplication
    public function startSnoopWithId($channelId, $spy, $whisper, $app, $appArgs, $snoopId)
623
    {
624
        $uri = "channels/$channelId/snoop/$snoopId";
625
        try {
626
            $response = $this->client->getEndpoint()->post($uri, [
627
                'form_params' => [
628
                    'spy' => $spy,
629
                    'whisper' => $whisper,
630
                    'app' => $app,
631
                    'appArgs' => $appArgs,
632
                ]
633
            ]);
634
        } catch (RequestException $e) {
635
            $this->processRequestException($e);
636
        }
637
638
        return new Channel($this->client, \GuzzleHttp\json_decode($response->getBody()));
639
    }
640
641
    /**
642
     * @return string
643
     */
644
    public function getType()
645
    {
646
        return 'channels';
647
    }
648
}
649