Completed
Push — master ( 0e6f1b...69c814 )
by Brian
02:51
created

Channel::onceChanneUnhold()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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 1
    public function getDialplan()
112
    {
113 1
        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 38
    public function getId()
120
    {
121 38
        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 onChannelHold(callable $callback)
312
    {
313
        $this->on(Event::CHANNEL_HOLD.'_'.$this->getId(), $callback);
314
    }
315
316
    /**
317
     * @param callable $callback
318
     */
319
    public function onceChannelHold(callable $callback)
320
    {
321
        $this->once(Event::CHANNEL_HOLD.'_'.$this->getId(), $callback);
322
    }
323
324
    /**
325
     * @param callable $callback
326
     */
327
    public function onChannelUnhold(callable $callback)
328
    {
329
        $this->on(Event::CHANNEL_UNHOLD.'_'.$this->getId(), $callback);
330
    }
331
332
    /**
333
     * @param callable $callback
334
     */
335
    public function onceChanneUnhold(callable $callback)
336
    {
337
        $this->once(Event::CHANNEL_UNHOLD.'_'.$this->getId(), $callback);
338
    }
339
340
    /**
341
     * @param callable $callback
342
     */
343
    public function onChannelTalkingFinished(callable $callback)
344
    {
345
        $this->on(Event::CHANNEL_TALKING_FINISHED.'_'.$this->getId(), $callback);
346
    }
347
348
    /**
349
     * @param callable $callback
350
     */
351
    public function onceChannelTalkingFinished(callable $callback)
352
    {
353
        $this->once(Event::CHANNEL_TALKING_FINISHED.'_'.$this->getId(), $callback);
354
    }
355
356
    /**
357
     * @param callable $callback
358
     */
359
    public function onChannelTalkingStarted(callable $callback)
360
    {
361
        $this->on(Event::CHANNEL_TALKING_STARTED.'_'.$this->getId(), $callback);
362
    }
363
364
    /**
365
     * @param callable $callback
366
     */
367
    public function onceChannelTalkingStarted(callable $callback)
368
    {
369
        $this->once(Event::CHANNEL_TALKING_STARTED.'_'.$this->getId(), $callback);
370
    }
371
372
    /**
373
     * @param callable $callback
374
     */
375
    public function onChannelUserevent(callable $callback)
376
    {
377
        $this->on(Event::CHANNEL_USEREVENT.'_'.$this->getId(), $callback);
378
    }
379
380
    /**
381
     * @param callable $callback
382
     */
383
    public function onceChannelUserevent(callable $callback)
384
    {
385 37
        $this->once(Event::CHANNEL_USEREVENT.'_'.$this->getId(), $callback);
386
    }
387 37
388 37
    /**
389
     * @param callable $callback
390
     */
391
    public function onChannelVarset(callable $callback)
392
    {
393
        $this->on(Event::CHANNEL_VARSET.'_'.$this->getId(), $callback);
394
    }
395
396
    /**
397
     * @param callable $callback
398
     */
399
    public function onceChannelVarset(callable $callback)
400
    {
401
        $this->once(Event::CHANNEL_VARSET.'_'.$this->getId(), $callback);
402
    }
403
404
    /**
405
     * Delete (i.e. hangup) a channel.
406
     *
407
     * @throws NotFoundException
408
     */
409
    public function deleteChannel()
410 37
    {
411
        $this->client->channels()->deleteChannel($this->id);
412 37
    }
413 37
414
    /**
415
     * Hangup the channel if it still exists.
416
     */
417
    public function hangup()
418
    {
419
        $this->client->channels()->hangup($this->id);
420
    }
421
422
    /**
423
     * Exit application; continue execution in the dialplan.
424
     *
425
     * @param string $context The context to continue to.
426
     * @param string $extension The extension to continue to.
427
     * @param int $priority The priority to continue to.
428
     * @throws NotFoundException
429
     * @throws ConflictException
430
     */
431
    public function continueDialplan($context, $extension, $priority)
432
    {
433
        $this->client->channels()->continueDialplan($this->id, $context, $extension, $priority);
434
    }
435
436
    /**
437
     * Answer a channel.
438
     *
439
     * @throws NotFoundException
440
     * @throws ConflictException
441
     */
442
    public function answer()
443
    {
444
        $this->client->channels()->answer($this->id);
445
    }
446
447
    /**
448
     * Indicate ringing to a channel.
449
     *
450
     * @throws NotFoundException
451
     * @throws ConflictException
452
     */
453
    public function startRinging()
454
    {
455
        $this->client->channels()->startRinging($this->id);
456
    }
457
458
    /**
459
     * Stop ringing indication on a channel if locally generated.
460
     *
461
     * @throws NotFoundException
462
     * @throws ConflictException
463
     */
464
    public function stopRinging()
465
    {
466
        $this->client->channels()->stopRinging($this->id);
467
    }
468
469
    /**
470
     * Send provided DTMF to a given channel.
471
     *
472
     * @param string $dtmf DTMF To send.
473
     * @param int $before Amount of time to wait before DTMF digits (specified in milliseconds) start.
474
     * @param int $between Amount of time in between DTMF digits (specified in milliseconds).  Default: 100
475
     * @param int $duration Length of each DTMF digit (specified in milliseconds).  Default: 100
476
     * @param int $after Amount of time to wait after DTMF digits (specified in milliseconds) end.
477
     * @throws InvalidParameterException
478
     * @throws NotFoundException
479
     * @throws ConflictException
480
     */
481
    public function sendDtmf($dtmf, $before = null, $between = null, $duration = null, $after = null)
482
    {
483
        $this->client->channels()->sendDtmf($this->id, $dtmf, $before, $between, $duration, $after);
484
    }
485
486
    /**
487
     * Mute a channel.
488
     *
489
     * @param string $direction (default both) Direction in which to mute audio
490
     * @throws NotFoundException
491
     * @throws ConflictException
492
     */
493
    public function mute($direction)
494
    {
495
        $this->client->channels()->mute($this->id, $direction);
496
    }
497
498
    /**
499
     * Unmute a channel.
500
     *
501
     * @param string $direction (default both) Direction in which to unmute audio
502
     * @throws NotFoundException
503
     * @throws ConflictException
504
     */
505
    public function unmute($direction)
506
    {
507
        $this->client->channels()->unmute($this->id, $direction);
508
    }
509
510
    /**
511
     * Hold a channel.
512
     *
513
     * @throws NotFoundException
514
     * @throws ConflictException
515
     */
516
    public function hold()
517
    {
518
        $this->client->channels()->hold($this->id);
519
    }
520
521
    /**
522
     * Remove a channel from hold.
523
     *
524
     * @throws NotFoundException
525
     * @throws ConflictException
526
     */
527
    public function unhold()
528
    {
529
        $this->client->channels()->unhold($this->id);
530
    }
531
532
    /**
533
     * Play music on hold to a channel. Using media operations such as /play on a channel playing MOH in
534
     * this manner will suspend MOH without resuming automatically. If continuing music on hold is
535
     * desired, the stasis application must reinitiate music on hold.
536
     *
537
     * @param string $mohClass Music on hold class to use
538
     * @throws NotFoundException
539
     * @throws ConflictException
540
     */
541
    public function startMusicOnHold($mohClass)
542
    {
543
        $this->client->channels()->startMusicOnHold($this->id, $mohClass);
544
    }
545
546
    /**
547
     * Stop playing music on hold to a channel.
548
     *
549
     * @throws NotFoundException
550
     * @throws ConflictException
551
     */
552
    public function stopMusicOnHold()
553
    {
554
        $this->client->channels()->stopMusicOnHold($this->id);
555
    }
556
557
    /**
558
     * 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.
559
     *
560
     * @throws NotFoundException
561
     * @throws ConflictException
562
     */
563
    public function startSilence()
564 2
    {
565
        $this->client->channels()->startSilence($this->id);
566 2
    }
567
568
    /**
569
     * Stop playing silence to a channel.
570
     *
571
     * @throws NotFoundException
572
     * @throws ConflictException
573
     */
574
    public function stopSilence()
575
    {
576
        $this->client->channels()->stopSilence($this->id);
577
    }
578
579
    /**
580
     * Start playback of media. The media URI may be any of a number of URI's. Currently sound:,
581
     * recording:, number:, digits:, characters:, and tone: URI's are supported. This operation creates a
582
     * playback resource that can be used to control the playback of media (pause, rewind, fast forward,
583
     * etc.)
584
     *
585 4
     * @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback
586
     *
587 4
     * @param string $media (required) Media's URI to play.
588 4
     * @param string $lang For sounds, selects language for sound.
589
     * @param int $offsetms Number of media to skip before playing.
590
     * @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations.
591
     * @param string $playbackId Playback Id.
592
     * @return Playback
593
     * @throws NotFoundException
594
     * @throws ConflictException
595
     */
596
    public function playMedia($media, $lang = null, $offsetms = null, $skipms = null, $playbackId = null)
597
    {
598
        return $this->client->channels()->playMedia($this->id, $media, $lang, $offsetms, $skipms, $playbackId);
599
    }
600
601
    /**
602
     * Start playback of media and specify the playbackId. The media URI may be any of a number of URI's.
603
     * Currently sound: and recording: URI's are supported. This operation creates a playback resource
604
     * that can be used to control the playback of media (pause, rewind, fast forward, etc.)
605
     *
606
     * @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback
607
     *
608
     * @param string $media (required) Media's URI to play.
609
     * @param string $lang For sounds, selects language for sound.
610
     * @param int $offsetms Number of media to skip before playing.
611
     * @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations.
612
     * @param string $playbackId Playback Id.
613
     * @return Playback
614
     * @throws NotFoundException
615
     * @throws ConflictException
616
     */
617
    public function playMediaWithId($media, $lang = null, $offsetms = null, $skipms = null, $playbackId = null)
618
    {
619
        return $this->client->channels()->playMediaWithId($this->getId(), $media, $lang, $offsetms, $skipms,
620
            $playbackId);
621
    }
622
623
    /**
624
     * Start a recording. Record audio from a channel. Note that this will not capture audio sent to the
625
     * channel. The bridge itself has a record feature if that's what you want.
626
     *
627
     * @param string $name (required) Recording's filename
628
     * @param string $format (required) Format to encode audio in
629
     * @param int $maxDurationSeconds Maximum duration of the recording, in seconds. 0 for no limit.  Allowed range: Min: 0; Max: None
630
     * @param int $maxSilenceSeconds Maximum duration of silence, in seconds. 0 for no limit.  Allowed range: Min: 0; Max: None
631
     * @param string $ifExists = Action to take if a recording with the same name already exists. default: fail, Allowed values: fail, overwrite, append
632
     * @param boolean $beep Play beep when recording begins
633
     * @param string $terminateOn DTMF input to terminate recording.  Default: none, Allowed values: none, any, *, #
634
     * @return LiveRecording
635
     * @throws InvalidParameterException
636
     * @throws NotFoundException
637
     * @throws ConflictException
638
     * @throws UnprocessableEntityException
639
     */
640 View Code Duplication
    public function record(
0 ignored issues
show
Duplication introduced by Brian Smith
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...
641
        $name,
642
        $format,
643
        $maxDurationSeconds = null,
644
        $maxSilenceSeconds = null,
645
        $ifExists = null,
646
        $beep = null,
647
        $terminateOn = null
648
    ) {
649
        return $this->client->channels()->record($this->id, $name, $format, $maxDurationSeconds, $maxSilenceSeconds,
650
            $ifExists, $beep, $terminateOn);
651
    }
652
653
    /**
654
     * Get the value of a channel variable or function.
655
     *
656
     * @param string $variable
657
     * @param null|string $default The value to return if the variable does not exist
658
     * @return string|Variable
659
     * @throws InvalidParameterException
660
     * @throws NotFoundException
661
     * @throws ConflictException
662
     */
663
    public function getVariable($variable, $default = null)
664
    {
665
        return $this->client->channels()->getVariable($this->id, $variable, $default);
0 ignored issues
show
Deprecated Code introduced by Brian Smith
The method phparia\Api\Channels::getVariable() has been deprecated.

This method has been deprecated.

Loading history...
666
    }
667
668
    /**
669
     * Set the value of a channel variable or function.
670
     *
671
     * @param string $variable
672
     * @param string $value
673
     * @return Variable
674
     * @throws InvalidParameterException
675
     * @throws NotFoundException
676
     * @throws ConflictException
677
     */
678
    public function setVariable($variable, $value)
679
    {
680
        return $this->client->channels()->setVariable($this->id, $variable, $value);
0 ignored issues
show
Deprecated Code introduced by wormling
The method phparia\Api\Channels::setVariable() has been deprecated.

This method has been deprecated.

Loading history...
681
    }
682
683
    /**
684
     * Start snooping. Snoop (spy/whisper) on a specific channel.
685
     *
686
     * @param string $spy (default none) Direction of audio to spy on
687
     * @param string $whisper (default none) Direction of audio to whisper into
688
     * @param string $app (required) Application the snooping channel is placed into
689 38
     * @param string $appArgs The application arguments to pass to the Stasis application
690
     * @param string $snoopId Unique ID to assign to snooping channel
691 38
     * @return Channel
692
     * @throws InvalidParameterException
693 38
     * @throws NotFoundException
694 38
     */
695 38
    public function startSnoop($spy, $whisper, $app, $appArgs, $snoopId)
696 38
    {
697 38
        return $this->client->channels()->startSnoop($this->id, $spy, $whisper, $app, $appArgs, $snoopId);
698 38
    }
699 38
700 38
    /**
701 38
     * Start snooping. Snoop (spy/whisper) on a specific channel.
702
     *
703
     * @param string $spy (default none) Direction of audio to spy on
704
     * @param string $whisper (default none) Direction of audio to whisper into
705
     * @param string $app (required) Application the snooping channel is placed into
706
     * @param string $appArgs The application arguments to pass to the Stasis application
707
     * @param string $snoopId Unique ID to assign to snooping channel
708
     * @return Channel
709
     * @throws InvalidParameterException
710
     * @throws NotFoundException
711
     */
712
    public function startSnoopWithId($spy, $whisper, $app, $appArgs, $snoopId)
713
    {
714
        return $this->client->channels()->startSnoopWithId($this->id, $spy, $whisper, $app, $appArgs, $snoopId);
715
    }
716
717
    /**
718
     * @param AriClient $client
719
     * @param string $response
720
     */
721
    public function __construct(AriClient $client, $response)
722
    {
723
        parent::__construct($client, $response);
724
725
        $this->accountCode = $this->getResponseValue('accountcode');
726
        $this->caller = $this->getResponseValue('caller', '\phparia\Resources\CallerId');
727
        $this->connected = $this->getResponseValue('connected', '\phparia\Resources\CallerId');
728
        $this->creationTime = $this->getResponseValue('creationtime', '\DateTime');
729
        $this->dialplan = $this->getResponseValue('dialplan', '\phparia\Resources\DialplanCep');
730
        $this->id = $this->getResponseValue('id');
731
        $this->name = $this->getResponseValue('name');
732
        $this->state = $this->getResponseValue('state');
733
    }
734
735
}
736