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

Bridges::removeChannel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2.004
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\Resources\Bridge;
23
use phparia\Exception\ConflictException;
24
use phparia\Exception\InvalidParameterException;
25
use phparia\Exception\NotFoundException;
26
use phparia\Exception\UnprocessableEntityException;
27
28
/**
29
 * Bridges API
30
 *
31
 * @author Brian Smith <[email protected]>
32
 */
33
class Bridges extends MediaBase
34
{
35
    /**
36
     * List all active bridges in Asterisk.
37
     *
38
     * @return Bridge[]
39
     */
40 View Code Duplication
    public function getBridges()
0 ignored issues
show
Duplication introduced by
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...
41
    {
42 2
        $uri = 'bridges';
43
        $response = $this->client->getEndpoint()->get($uri);
44 2
45 2
        $bridges = [];
46
        foreach (\GuzzleHttp\json_decode($response->getBody()) as $bridge) {
47 2
            $bridges[] = new Bridge($this->client, $bridge);
48 2
        }
49 2
50 2
        return $bridges;
51
    }
52 2
53
    /**
54
     * Create a new bridge. This bridge persists until it has been shut down, or Asterisk has been shut down.
55
     *
56
     * @param string $bridgeId Unique ID to give to the bridge being created.
57
     * @param string $type Comma separated list of bridge type attributes (mixing, holding, dtmf_events, proxy_media).
58
     * @param string $name Name to give to the bridge being created.
59
     * @return Bridge
60
     */
61
    public function createBridge($bridgeId, $type, $name)
62
    {
63 23
        $uri = 'bridges';
64
        $response = $this->client->getEndpoint()->post($uri, [
65 23
            'form_params' => [
66 23
                'bridgeId' => $bridgeId,
67 23
                'type' => $type,
68 23
                'name' => $name,
69 23
            ]
70 23
        ]);
71
72 23
        return new Bridge($this->client, \GuzzleHttp\json_decode($response->getBody()));
73
    }
74
75
    /**
76
     * Create a new bridge or updates an existing one. This bridge persists until it has been shut down, or Asterisk has been shut down.
77
     *
78
     * @param string $bridgeId Unique ID to give to the bridge being created.
79
     * @param string $type Comma separated list of bridge type attributes (mixing, holding, dtmf_events, proxy_media) to set.
80
     * @param string $name Set the name of the bridge.
81
     * @return Bridge
82
     */
83
    public function updateBridge($bridgeId, $type, $name)
84
    {
85
        $uri = "bridges/$bridgeId";
86
        $response = $this->client->getEndpoint()->post($uri, [
87
            'form_params' => [
88
                'type' => $type,
89
                'name' => $name,
90
            ]
91
        ]);
92
93
        return new Bridge($this->client, \GuzzleHttp\json_decode($response->getBody()));
94
    }
95
96
    /**
97
     * Get bridge details.
98
     *
99
     * @param string $bridgeId Bridge's id
100
     * @return Bridge
101 9
     * @throws NotFoundException
102
     */
103 9 View Code Duplication
    public function getBridge($bridgeId)
0 ignored issues
show
Duplication introduced by
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...
104
    {
105 9
        $uri = "bridges/$bridgeId";
106 9
        try {
107 4
            $response = $this->client->getEndpoint()->get($uri);
108
        } catch (RequestException $e) {
109
            $this->processRequestException($e);
110 5
        }
111
112
        return new Bridge($this->client, \GuzzleHttp\json_decode($response->getBody()));
113
    }
114
115
    /**
116
     * Shut down a bridge. If any channels are in this bridge, they will be removed and resume whatever they were doing beforehand.
117
     *
118
     * @param string $bridgeId Bridge's id
119 14
     * @throws NotFoundException
120
     */
121 14
    public function deleteBridge($bridgeId)
122
    {
123 14
        $uri = "bridges/$bridgeId";
124 14
        try {
125 2
            $this->client->getEndpoint()->delete($uri);
126
        } catch (RequestException $e) {
127 12
            $this->processRequestException($e);
128
        }
129
    }
130
131
    /**
132
     * Add a channel to a bridge.
133
     *
134
     * @param string $bridgeId Bridge's id
135
     * @param string $channel (required) Ids of channels to add to bridge.  Allows comma separated values.
136
     * @param string $role Channel's role in the bridge
137
     * @throws NotFoundException
138
     * @throws ConflictException
139 12
     * @throws UnprocessableEntityException
140
     */
141 12
    public function addChannel($bridgeId, $channel, $role = null)
142
    {
143 12
        $uri = "bridges/$bridgeId/addChannel";
144 12
        try {
145 12
            $this->client->getEndpoint()->post($uri, [
146 12
                'form_params' => [
147 12
                    'channel' => $channel,
148 2
                    'role' => $role,
149 2
                ]
150 2
            ]);
151
        } catch (RequestException $e) {
152
            $this->processRequestException($e);
153
        }
154
    }
155
156 8
    /**
157
     * Remove a channel from a bridge.
158
     *
159
     * @param string $bridgeId Bridge's id
160
     * @param string $channel (required) Ids of channels to remove from bridge.  Allows comma separated values.
161
     * @throwe InvalidParameterException
162
     * @throws NotFoundException
163
     * @throws ConflictException
164
     * @throws UnprocessableEntityException
165
     */
166
    public function removeChannel($bridgeId, $channel)
167 6
    {
168
        $uri = "bridges/$bridgeId/removeChannel";
169 6
        try {
170
            $this->client->getEndpoint()->post($uri, [
171 6
                'form_params' => [
172 6
                    'channel' => $channel,
173 6
                ]
174 6
            ]);
175 2
        } catch (RequestException $e) {
176 2
            $this->processRequestException($e);
177 2
        }
178
    }
179
180
    public function getType()
181
    {
182
        return 'bridges';
183 2
    }
184
}
185