MediaBase::record()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 14
cp 0
rs 9.456
c 0
b 0
f 0
cc 2
nc 2
nop 8
crap 6

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Client\AriClientAware;
23
use phparia\Exception\UnprocessableEntityException;
24
use phparia\Resources\LiveRecording;
25
use phparia\Resources\Playback;
26
use phparia\Exception\ConflictException;
27
use phparia\Exception\InvalidParameterException;
28
use phparia\Exception\NotFoundException;
29
30
/**
31
 * Base class for playing media on channels and bridges
32
 *
33
 * @author Brian Smith <[email protected]>
34
 */
35
abstract class MediaBase extends AriClientAware
36
{
37
    /**
38
     * @return string 'channels' or 'bridges'
39
     */
40
    abstract public function getType();
41
42
    /**
43
     * Play music on hold to a channel. Using media operations such as /play on a channel playing MOH in
44
     * this manner will suspend MOH without resuming automatically. If continuing music on hold is
45
     * desired, the stasis application must reinitiate music on hold.
46
     *
47
     * @param string $id Bridge/Channel's id
48
     * @param string $mohClass Music on hold class to use
49
     * @throws NotFoundException
50
     * @throws ConflictException
51
     */
52
    public function startMusicOnHold($id, $mohClass)
53
    {
54
        $uri = "{$this->getType()}/$id/moh";
55
        try {
56
            $this->client->getEndpoint()->post($uri, [
57
                'form_params' => [
58
                    'mohClass' => $mohClass,
59
                ]
60
            ]);
61
        } catch (RequestException $e) {
62
            $this->processRequestException($e);
63
        }
64
    }
65
66
    /**
67
     * Stop playing music on hold to a channel.
68
     *
69
     * @param string $id Bridge/Channel's id
70
     * @throws NotFoundException
71
     * @throws ConflictException
72
     */
73 View Code Duplication
    public function stopMusicOnHold($id)
74
    {
75
        $uri = "{$this->getType()}/$id/moh";
76
        try {
77
            $this->client->getEndpoint()->delete($uri);
78
        } catch (RequestException $e) {
79
            $this->processRequestException($e);
80
        }
81
    }
82
83
    /**
84
     * Start playback of media. The media URI may be any of a number of URI's. Currently sound:,
85
     * recording:, number:, digits:, characters:, and tone: URI's are supported. This operation creates a
86
     * playback resource that can be used to control the playback of media (pause, rewind, fast forward,
87
     * etc.)
88
     *
89
     * @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback
90
     *
91
     * @param string $id Bridge/Channel's id
92
     * @param string $media (required) Media's URI to play.
93
     * @param string $lang For sounds, selects language for sound.
94
     * @param int $offsetms Number of media to skip before playing.
95
     * @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations.
96
     * @param string $playbackId Playback Id.
97
     * @return Playback
98
     * @throws NotFoundException
99
     * @throws ConflictException
100
     */
101 View Code Duplication
    public function playMedia($id, $media, $lang = null, $offsetms = null, $skipms = null, $playbackId = null)
102
    {
103
        $uri = "{$this->getType()}/$id/play";
104
105
        try {
106
            $response = $this->client->getEndpoint()->post($uri, [
107
                'form_params' => [
108
                    'media' => $media,
109
                    'lang' => $lang,
110
                    'offsetms' => $offsetms,
111
                    'skipms' => $skipms,
112
                    'playbackId' => $playbackId,
113
                ]
114
            ]);
115
        } catch (RequestException $e) {
116
            $this->processRequestException($e);
117
        }
118
119
        return new Playback($this->client, \GuzzleHttp\json_decode($response->getBody()));
120
    }
121
122
    /**
123
     * Start playback of media and specify the playbackId. The media URI may be any of a number of URI's.
124
     * Currently sound: and recording: URI's are supported. This operation creates a playback resource
125
     * that can be used to control the playback of media (pause, rewind, fast forward, etc.)
126
     *
127
     * @link https://wiki.asterisk.org/wiki/display/AST/ARI+and+Channels%3A+Simple+Media+Manipulation Simple media playback
128
     *
129
     * @param string $id Channel's id
130
     * @param string $media (required) Media's URI to play.
131
     * @param string $lang For sounds, selects language for sound.
132
     * @param int $offsetms Number of media to skip before playing.
133
     * @param int $skipms (3000 default) Number of milliseconds to skip for forward/reverse operations.
134
     * @param string $playbackId Playback Id.
135
     * @return Playback
136
     * @throws NotFoundException
137
     * @throws ConflictException
138
     */
139 View Code Duplication
    public function playMediaWithId(
140
        $id,
141
        $media,
142
        $lang = null,
143
        $offsetms = null,
144
        $skipms = null,
145
        $playbackId = null
146
    ) {
147
        $uri = "{$this->getType()}/$id/play/$playbackId";
148
        try {
149
            $response = $this->client->getEndpoint()->post($uri, [
150
                'form_params' => [
151
                    'media' => $media,
152
                    'lang' => $lang,
153
                    'offsetms' => $offsetms,
154
                    'skipms' => $skipms,
155
                ]
156
            ]);
157
        } catch (RequestException $e) {
158
            $this->processRequestException($e);
159
        }
160
161
        return new Playback($this->client, \GuzzleHttp\json_decode($response->getBody()));
162
    }
163
164
    /**
165
     * Start a recording. Record audio from a channel. Note that this will not capture audio sent to the
166
     * channel. The bridge itself has a record feature if that's what you want.
167
     *
168
     * @param string $id Channel/Bridge ID
169
     * @param string $name (required) Recording's filename
170
     * @param string $format (required) Format to encode audio in
171
     * @param int $maxDurationSeconds Maximum duration of the recording, in seconds. 0 for no limit
172
     * @param int $maxSilenceSeconds Maximum duration of silence, in seconds. 0 for no limit
173
     * @param string $ifExists = fail - Action to take if a recording with the same name already exists.
174
     * @param boolean $beep string = fail - Action to take if a recording with the same name already exists.
175
     * @param string $terminateOn none - DTMF input to terminate recording
176
     * @return LiveRecording
177
     * @throws InvalidParameterException
178
     * @throws NotFoundException
179
     * @throws ConflictException
180
     * @throws UnprocessableEntityException
181
     */
182
    public function record(
183
        $id,
184
        $name,
185
        $format,
186
        $maxDurationSeconds = null,
187
        $maxSilenceSeconds = null,
188
        $ifExists = null,
189
        $beep = null,
190
        $terminateOn = null
191
    ) {
192
        $uri = "{$this->getType()}/$id/record";
193
        try {
194
            $response = $this->client->getEndpoint()->post($uri, [
195
                'form_params' => [
196
                    'name' => $name,
197
                    'format' => $format,
198
                    'maxDurationSeconds' => $maxDurationSeconds,
199
                    'maxSilenceSeconds' => $maxSilenceSeconds,
200
                    'ifExists' => $ifExists,
201
                    'beep' => $beep,
202
                    'terminateOn' => $terminateOn,
203
                ]
204
            ]);
205
        } catch (RequestException $e) {
206
            $this->processRequestException($e);
207
        }
208
209
        return new LiveRecording($this->client, \GuzzleHttp\json_decode($response->getBody()));
210
    }
211
}
212