Completed
Push — master ( fe47f8...2aa7aa )
by Brian
09:43
created

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