Completed
Push — master ( 087b38...e9a1e8 )
by Brian
03:09
created

Channel::hangup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Resources;
20
21
use DateTime;
22
use phparia\Client\AriClient;
23
use phparia\Events\Event;
24
use phparia\Exception\ConflictException;
25
use phparia\Exception\InvalidParameterException;
26
use phparia\Exception\NotFoundException;
27
use phparia\Exception\UnprocessableEntityException;
28
29
/**
30
 * A specific communication connection between Asterisk and an Endpoint.
31
 * 
32
 * @author Brian Smith <[email protected]>
33
 */
34
class Channel extends Resource
35
{
36
    /**
37
     * @var string 
38
     */
39
    private $accountCode;
40
41
    /**
42
     * @var CallerId
43
     */
44
    private $caller;
45
46
    /**
47
     * @var CallerId 
48
     */
49
    private $connected;
50
51
    /**
52
     * @var DateTime 
53
     */
54
    private $creationTime;
55
56
    /**
57
     * @var DialplanCep 
58
     */
59
    private $dialplan;
60
61
    /**
62
     * @var string Unique identifier of the channel.  This is the same as the Uniqueid field in AMI.
63
     */
64
    private $id;
65
66
    /**
67
     * @var string Name of the channel (i.e. SIP/foo-0000a7e3) 
68
     */
69
    private $name;
70
71
    /**
72
     * @var string 
73
     */
74
    private $state;
75
76
    /**
77
     * @return string
78
     */
79
    public function getAccountCode()
80
    {
81
        return $this->accountCode;
82
    }
83
84
    /**
85
     * @return CallerId Caller identification
86
     */
87
    public function getCaller()
88
    {
89
        return $this->caller;
90
    }
91
92
    /**
93
     * @return CallerId Connected caller identification
94
     */
95
    public function getConnected()
96
    {
97
        return $this->connected;
98
    }
99
100
    /**
101
     * @return DateTime
102
     */
103
    public function getCreationTime()
104
    {
105
        return $this->creationTime;
106
    }
107
108
    /**
109
     * @return DialplanCep Dialplan location (context/extension/priority)
110
     */
111
    public function getDialplan()
112
    {
113
        return $this->dialplan;
114
    }
115
116
    /**
117
     * @return string Unique identifier of the channel.  This is the same as the Uniqueid field in AMI.
118
     */
119 20
    public function getId()
120
    {
121 20
        return $this->id;
122
    }
123
124
    /**
125
     * @return string Name of the channel (i.e. SIP/foo-0000a7e3) 
126
     */
127
    public function getName()
128
    {
129
        return $this->name;
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function getState()
136
    {
137
        return $this->state;
138
    }
139
140
    /**
141
     * @param callable $callback
142
     */
143
    public function onStasisEnd(callable $callback)
144
    {
145
        $this->on(Event::STASIS_END . '_' . $this->getId(), $callback);
146
    }
147
148
    /**
149
     * @param callable $callback
150
     */
151
    public function onceStasisEnd(callable $callback)
152
    {
153
        $this->once(Event::STASIS_END . '_' . $this->getId(), $callback);
154
    }
155
156
    /**
157
     * @param callable $callback
158
     */
159
    public function onStasisStart(callable $callback)
160
    {
161
        $this->on(Event::STASIS_START . '_' . $this->getId(), $callback);
162
    }
163
164
    /**
165
     * @param callable $callback
166
     */
167
    public function onceStasisStart(callable $callback)
168
    {
169
        $this->once(Event::STASIS_START . '_' . $this->getId(), $callback);
170
    }
171
172
    /**
173
     * @param callable $callback
174
     */
175
    public function onChannelCallerId(callable $callback)
176
    {
177
        $this->on(Event::CHANNEL_CALLER_ID . '_' . $this->getId(), $callback);
178
    }
179
180
    /**
181
     * @param callable $callback
182
     */
183
    public function onceChannelCallerId(callable $callback)
184
    {
185
        $this->once(Event::CHANNEL_CALLER_ID . '_' . $this->getId(), $callback);
186
    }
187
188
    /**
189
     * @param callable $callback
190
     */
191
    public function onChannelCreated(callable $callback)
192
    {
193
        $this->on(Event::CHANNEL_CREATED . '_' . $this->getId(), $callback);
194
    }
195
196
    /**
197
     * @param callable $callback
198
     */
199
    public function onceChannelCreated(callable $callback)
200
    {
201
        $this->once(Event::CHANNEL_CREATED . '_' . $this->getId(), $callback);
202
    }
203
204
    /**
205
     * @param callable $callback
206
     */
207
    public function onChannelDestroyed(callable $callback)
208
    {
209
        $this->on(Event::CHANNEL_DESTROYED . '_' . $this->getId(), $callback);
210
    }
211
212
    /**
213
     * @param callable $callback
214
     */
215
    public function onceChannelDestroyed(callable $callback)
216
    {
217
        $this->once(Event::CHANNEL_DESTROYED . '_' . $this->getId(), $callback);
218
    }
219
    
220
    /**
221
     * @param callable $callback
222
     */
223
    public function removeChannelDestroyedListener(callable $callback)
224
    {
225
        $this->removeListener(Event::CHANNEL_DESTROYED . '_' . $this->getId(), $callback);
226
    }
227
228
    /**
229
     * @param callable $callback
230
     */
231
    public function onChannelDtmfReceived(callable $callback)
232
    {
233
        $this->on(Event::CHANNEL_DTMF_RECEIVED . '_' . $this->getId(), $callback);
234
    }
235
236
    /**
237
     * @param callable $callback
238
     */
239
    public function onceChannelDtmfReceived(callable $callback)
240
    {
241
        $this->once(Event::CHANNEL_DTMF_RECEIVED . '_' . $this->getId(), $callback);
242
    }
243
244
    /**
245
     * @param callable $callback
246
     */
247
    public function onChannelEnteredBridge(callable $callback)
248
    {
249
        $this->on(Event::CHANNEL_ENTERED_BRIDGE . '_' . $this->getId(), $callback);
250
    }
251
252
    /**
253
     * @param callable $callback
254
     */
255
    public function onceChannelEnteredBridge(callable $callback)
256
    {
257
        $this->once(Event::CHANNEL_ENTERED_BRIDGE . '_' . $this->getId(), $callback);
258
    }
259
260
    /**
261
     * @param callable $callback
262
     */
263
    public function onChannelHangupRequest(callable $callback)
264
    {
265
        $this->on(Event::CHANNEL_HANGUP_REQUEST . '_' . $this->getId(), $callback);
266
    }
267
268
    /**
269
     * @param callable $callback
270
     */
271
    public function onceChannelHangupRequest(callable $callback)
272
    {
273
        $this->once(Event::CHANNEL_HANGUP_REQUEST . '_' . $this->getId(), $callback);
274
    }
275
276
    /**
277
     * @param callable $callback
278
     */
279
    public function onChannelLeftBridge(callable $callback)
280
    {
281
        $this->on(Event::CHANNEL_LEFT_BRIDGE . '_' . $this->getId(), $callback);
282
    }
283
284
    /**
285
     * @param callable $callback
286
     */
287
    public function onceChannelLeftBridge(callable $callback)
288
    {
289
        $this->once(Event::CHANNEL_LEFT_BRIDGE . '_' . $this->getId(), $callback);
290
    }
291
292
    /**
293
     * @param callable $callback
294
     */
295
    public function onChannelStateChange(callable $callback)
296
    {
297
        $this->on(Event::CHANNEL_STATE_CHANGED . '_' . $this->getId(), $callback);
298
    }
299
300
    /**
301
     * @param callable $callback
302
     */
303
    public function onceChannelStateChange(callable $callback)
304
    {
305
        $this->once(Event::CHANNEL_STATE_CHANGED . '_' . $this->getId(), $callback);
306
    }
307
308
    /**
309
     * @param callable $callback
310
     */
311
    public function onChannelTalkingFinished(callable $callback)
312
    {
313
        $this->on(Event::CHANNEL_TALKING_FINISHED . '_' . $this->getId(), $callback);
314
    }
315
316
    /**
317
     * @param callable $callback
318
     */
319
    public function onceChannelTalkingFinished(callable $callback)
320
    {
321
        $this->once(Event::CHANNEL_TALKING_FINISHED . '_' . $this->getId(), $callback);
322
    }
323
324
    /**
325
     * @param callable $callback
326
     */
327
    public function onChannelTalkingStarted(callable $callback)
328
    {
329
        $this->on(Event::CHANNEL_TALKING_STARTED . '_' . $this->getId(), $callback);
330
    }
331
332
    /**
333
     * @param callable $callback
334
     */
335
    public function onceChannelTalkingStarted(callable $callback)
336
    {
337
        $this->once(Event::CHANNEL_TALKING_STARTED . '_' . $this->getId(), $callback);
338
    }
339
340
    /**
341
     * @param callable $callback
342
     */
343
    public function onChannelUserevent(callable $callback)
344
    {
345
        $this->on(Event::CHANNEL_USEREVENT . '_' . $this->getId(), $callback);
346
    }
347
348
    /**
349
     * @param callable $callback
350
     */
351
    public function onceChannelUserevent(callable $callback)
352
    {
353
        $this->once(Event::CHANNEL_USEREVENT . '_' . $this->getId(), $callback);
354
    }
355
356
    /**
357
     * @param callable $callback
358
     */
359
    public function onChannelVarset(callable $callback)
360
    {
361
        $this->on(Event::CHANNEL_VARSET . '_' . $this->getId(), $callback);
362
    }
363
364
    /**
365
     * @param callable $callback
366
     */
367
    public function onceChannelVarset(callable $callback)
368
    {
369
        $this->once(Event::CHANNEL_VARSET . '_' . $this->getId(), $callback);
370
    }
371
372
    /**
373
     * Delete (i.e. hangup) a channel.
374
     *
375
     * @throws NotFoundException
376
     */
377
    public function deleteChannel()
378
    {
379
        $this->client->channels()->deleteChannel($this->id);
380
    }
381
    
382
    /**
383
     * Hangup the channel if it still exists.
384
     */
385 1
    public function hangup()
386
    {
387 1
        $this->client->channels()->hangup($this->id);
388 1
    }
389
390
    /**
391
     * Exit application; continue execution in the dialplan.
392
     * 
393
     * @param string $context The context to continue to.
394
     * @param string $extension The extension to continue to.
395
     * @param int $priority The priority to continue to.
396
     * @throws NotFoundException
397
     * @throws ConflictException
398
     */
399
    public function continueDialplan($context, $extension, $priority)
400
    {
401
        $this->client->channels()->continueDialplan($this->id, $context, $extension, $priority);
402
    }
403
404
    /**
405
     * Answer a channel.
406
     * 
407
     * @throws NotFoundException
408
     * @throws ConflictException
409
     */
410 19
    public function answer()
411
    {
412 19
        $this->client->channels()->answer($this->id);
413 19
    }
414
415
    /**
416
     * Indicate ringing to a channel.
417
     * 
418
     * @throws NotFoundException
419
     * @throws ConflictException
420
     */
421
    public function startRinging()
422
    {
423
        $this->client->channels()->startRinging($this->id);
424
    }
425
426
    /**
427
     * Stop ringing indication on a channel if locally generated.
428
     * 
429
     * @throws NotFoundException
430
     * @throws ConflictException
431
     */
432
    public function stopRinging()
433
    {
434
        $this->client->channels()->stopRinging($this->id);
435
    }
436
437
    /**
438
     * Send provided DTMF to a given channel.
439
     * 
440
     * @param string $dtmf DTMF To send.
441
     * @param int $before Amount of time to wait before DTMF digits (specified in milliseconds) start.
442
     * @param int $between Amount of time in between DTMF digits (specified in milliseconds).  Default: 100
443
     * @param int $duration Length of each DTMF digit (specified in milliseconds).  Default: 100
444
     * @param int $after Amount of time to wait after DTMF digits (specified in milliseconds) end.
445
     * @throws InvalidParameterException
446
     * @throws NotFoundException
447
     * @throws ConflictException
448
     */
449
    public function sendDtmf($dtmf, $before = null, $between = null, $duration = null, $after = null)
450
    {
451
        $this->client->channels()->sendDtmf($this->id, $dtmf, $before, $between, $duration, $after);
452
    }
453
454
    /**
455
     * Mute a channel.
456
     * 
457
     * @param string $direction (default both) Direction in which to mute audio
458
     * @throws NotFoundException
459
     * @throws ConflictException
460
     */
461
    public function mute($direction)
462
    {
463
        $this->client->channels()->mute($this->id, $direction);
464
    }
465
466
    /**
467
     * Unmute a channel.
468
     * 
469
     * @param string $direction (default both) Direction in which to unmute audio
470
     * @throws NotFoundException
471
     * @throws ConflictException
472
     */
473
    public function unmute($direction)
474
    {
475
        $this->client->channels()->unmute($this->id, $direction);
476
    }
477
478
    /**
479
     * Hold a channel.
480
     * 
481
     * @throws NotFoundException
482
     * @throws ConflictException
483
     */
484
    public function hold()
485
    {
486
        $this->client->channels()->hold($this->id);
487
    }
488
489
    /**
490
     * Remove a channel from hold.
491
     * 
492
     * @throws NotFoundException
493
     * @throws ConflictException
494
     */
495
    public function unhold()
496
    {
497
        $this->client->channels()->unhold($this->id);
498
    }
499
500
    /**
501
     * Play music on hold to a channel. Using media operations such as /play on a channel playing MOH in 
502
     * this manner will suspend MOH without resuming automatically. If continuing music on hold is 
503
     * desired, the stasis application must reinitiate music on hold.
504
     * 
505
     * @param string $mohClass Music on hold class to use
506
     * @throws NotFoundException
507
     * @throws ConflictException
508
     */
509
    public function startMusicOnHold($mohClass)
510
    {
511
        $this->client->channels()->startMusicOnHold($this->id, $mohClass);
512
    }
513
514
    /**
515
     * Stop playing music on hold to a channel.
516
     * 
517
     * @throws NotFoundException
518
     * @throws ConflictException
519
     */
520
    public function stopMusicOnHold()
521
    {
522
        $this->client->channels()->stopMusicOnHold($this->id);
523
    }
524
525
    /**
526
     * 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.
527
     * 
528
     * @throws NotFoundException
529
     * @throws ConflictException
530
     */
531
    public function startSilence()
532
    {
533
        $this->client->channels()->startSilence($this->id);
534
    }
535
536
    /**
537
     * Stop playing silence to a channel.
538
     * 
539
     * @throws NotFoundException
540
     * @throws ConflictException
541
     */
542
    public function stopSilence()
543
    {
544
        $this->client->channels()->stopSilence($this->id);
545
    }
546
547
    /**
548
     * Start playback of media. The media URI may be any of a number of URI's. Currently sound:, 
549
     * recording:, number:, digits:, characters:, and tone: URI's are supported. This operation creates a 
550
     * playback resource that can be used to control the playback of media (pause, rewind, fast forward, 
551
     * etc.)
552
     * 
553
     * @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback
554
     * 
555
     * @param string $media (required) Media's URI to play.
556
     * @param string $lang For sounds, selects language for sound.
557
     * @param int $offsetms Number of media to skip before playing.
558
     * @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations.
559
     * @param string $playbackId Playback Id.
560
     * @return Playback
561
     * @throws NotFoundException
562
     * @throws ConflictException
563
     */
564
    public function playMedia($media, $lang = null, $offsetms = null, $skipms = null, $playbackId = null)
565
    {
566
        return $this->client->channels()->playMedia($this->id, $media, $lang, $offsetms, $skipms, $playbackId);
567
    }
568
569
    /**
570
     * Start playback of media and specify the playbackId. The media URI may be any of a number of URI's. 
571
     * Currently sound: and recording: URI's are supported. This operation creates a playback resource 
572
     * that can be used to control the playback of media (pause, rewind, fast forward, etc.)
573
     * 
574
     * @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback
575
     * 
576
     * @param string $media (required) Media's URI to play.
577
     * @param string $lang For sounds, selects language for sound.
578
     * @param int $offsetms Number of media to skip before playing.
579
     * @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations.
580
     * @param string $playbackId Playback Id.
581
     * @return Playback
582
     * @throws NotFoundException
583
     * @throws ConflictException
584
     */
585 4
    public function playMediaWithId($media, $lang = null, $offsetms = null, $skipms = null, $playbackId = null)
586
    {
587 4
        return $this->client->channels()->playMediaWithId($this->getId(), $media, $lang, $offsetms, $skipms, $playbackId);
588
    }
589
590
    /**
591
     * Start a recording. Record audio from a channel. Note that this will not capture audio sent to the
592
     * channel. The bridge itself has a record feature if that's what you want.
593
     *
594
     * @param string $name (required) Recording's filename
595
     * @param string $format (required) Format to encode audio in
596
     * @param int $maxDurationSeconds Maximum duration of the recording, in seconds. 0 for no limit.  Allowed range: Min: 0; Max: None
597
     * @param int $maxSilenceSeconds Maximum duration of silence, in seconds. 0 for no limit.  Allowed range: Min: 0; Max: None
598
     * @param string $ifExists = Action to take if a recording with the same name already exists. default: fail, Allowed values: fail, overwrite, append
599
     * @param boolean $beep Play beep when recording begins
600
     * @param string $terminateOn DTMF input to terminate recording.  Default: none, Allowed values: none, any, *, #
601
     * @return LiveRecording
602
     * @throws InvalidParameterException
603
     * @throws NotFoundException
604
     * @throws ConflictException
605
     * @throws UnprocessableEntityException
606
     */
607
    public function record($name, $format, $maxDurationSeconds = null, $maxSilenceSeconds = null, $ifExists = null, $beep = null, $terminateOn = null)
608
    {
609
        return $this->client->channels()->record($this->id, $name, $format, $maxDurationSeconds, $maxSilenceSeconds, $ifExists, $beep, $terminateOn);
610
    }
611
612
    /**
613
     * Get the value of a channel variable or function.
614
     * 
615
     * @param string $variable
616
     * @return string|Variable
617
     * @throws InvalidParameterException
618
     * @throws NotFoundException
619
     * @throws ConflictException
620
     */
621
    public function getVariable($variable)
622
    {
623
        return $this->client->channels()->getVariable($this->id, $variable);
624
    }
625
626
    /**
627
     * Set the value of a channel variable or function.
628
     * 
629
     * @param string $variable
630
     * @param string $value
631
     * @return Variable
632
     * @throws InvalidParameterException
633
     * @throws NotFoundException
634
     * @throws ConflictException
635
     */
636
    public function setVariable($variable, $value)
637
    {
638
        return $this->client->channels()->setVariable($this->id, $variable, $value);
639
    }
640
641
    /**
642
     * Start snooping. Snoop (spy/whisper) on a specific channel.
643
     * 
644
     * @param string $spy (default none) Direction of audio to spy on
645
     * @param string $whisper (default none) Direction of audio to whisper into
646
     * @param string $app (required) Application the snooping channel is placed into
647
     * @param string $appArgs The application arguments to pass to the Stasis application
648
     * @param string $snoopId Unique ID to assign to snooping channel
649
     * @return Channel
650
     * @throws InvalidParameterException
651
     * @throws NotFoundException
652
     */
653
    public function startSnoop($spy, $whisper, $app, $appArgs, $snoopId)
654
    {
655
        return $this->client->channels()->startSnoop($this->id, $spy, $whisper, $app, $appArgs, $snoopId);
656
    }
657
658
    /**
659
     * Start snooping. Snoop (spy/whisper) on a specific channel.
660
     * 
661
     * @param string $spy (default none) Direction of audio to spy on
662
     * @param string $whisper (default none) Direction of audio to whisper into
663
     * @param string $app (required) Application the snooping channel is placed into
664
     * @param string $appArgs The application arguments to pass to the Stasis application
665
     * @param string $snoopId Unique ID to assign to snooping channel
666
     * @return Channel
667
     * @throws InvalidParameterException
668
     * @throws NotFoundException
669
     */
670
    public function startSnoopWithId($spy, $whisper, $app, $appArgs, $snoopId)
671
    {
672
        return $this->client->channels()->startSnoopWithId($this->id, $spy, $whisper, $app, $appArgs, $snoopId);
673
    }
674
675
    /**
676
     * @param AriClient $client
677
     * @param string $response
678
     */
679 20
    public function __construct(AriClient $client, $response)
680
    {
681 20
        parent::__construct($client, $response);
682
683 20
        $this->accountCode = property_exists($this->response, 'account_code') ? $this->response->account_code : null;
684 20
        $this->caller = new CallerId($this->response->caller);
685 20
        $this->connected = new CallerId($this->response->connected);
686 20
        $this->creationTime = new \DateTime($this->response->creationtime);
687 20
        $this->dialplan = new DialplanCep($this->response->dialplan);
688 20
        $this->id = $this->response->id;
689 20
        $this->name = $this->response->name;
690 20
        $this->state = $this->response->state;
691 20
    }
692
693
}
694