Completed
Push — master ( 0af6cb...e278f8 )
by Bai
12s queued 10s
created

AppClient::stopMerge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Qiniu\Rtc;
4
5
use Qiniu\Auth;
6
use Qiniu\Config;
7
use Qiniu\Http\Error;
8
use Qiniu\Http\Client;
9
10
class AppClient
11
{
12
    private $auth;
13
    private $baseURL;
14
15
    public function __construct(Auth $auth)
16
    {
17
        $this->auth = $auth;
18
19
        $this->baseURL = sprintf("%s/%s/apps", Config::RTCAPI_HOST, Config::RTCAPI_VERSION);
20
    }
21
22
    /**
23
     * 创建应用
24
     *
25
     * @param string $hub 绑定的直播 hub
26
     * @param string $title app 的名称  注意,Title 不是唯一标识,重复 create 动作将生成多个 app
27
     * @param int $maxUsers 连麦房间支持的最大在线人数
28
     * @param bool $noAutoKickUser 禁止自动踢人(抢流),默认为 false
29
     * @return array
30
     * @link  https://doc.qnsdk.com/rtn/docs/server_overview#2_1
31
     */
32
    public function createApp($hub, $title, $maxUsers = null, $noAutoKickUser = false)
33
    {
34
        $params['hub'] = $hub;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
35
        $params['title'] = $title;
36
        if (!empty($maxUsers)) {
37
            $params['maxUsers'] = $maxUsers;
38
        }
39
        if ($noAutoKickUser !== null) {
40
            $params['noAutoKickUser'] = $noAutoKickUser;
41
        }
42
        $body = json_encode($params);
43
        return $this->post($this->baseURL, $body);
44
    }
45
46
    /**
47
     * 更新一个应用的配置信息
48
     *
49
     * @param string $appId app 的唯一标识,创建的时候由系统生成
50
     * @param string $hub app 的名称,可选
51
     * @param string $title 绑定的直播 hub,可选,用于合流后 rtmp 推流
52
     * @param int $maxUsers 连麦房间支持的最大在线人数,可选
53
     * @param bool $noAutoKickUser 禁止自动踢人,可选
54
     * @param null $mergePublishRtmp 连麦合流转推 RTMP 的配置,可选择。其详细配置可以参考文档
55
     * @return array
56
     * @link  https://doc.qnsdk.com/rtn/docs/server_overview#2_1
57
     */
58
    public function updateApp($appId, $hub, $title, $maxUsers = null, $noAutoKickUser = false, $mergePublishRtmp = null)
59
    {
60
        $url = $this->baseURL . '/' . $appId;
61
        $params['hub'] = $hub;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
62
        $params['title'] = $title;
63
        if (!empty($maxUsers)) {
64
            $params['maxUsers'] = $maxUsers;
65
        }
66
        if ($noAutoKickUser !== null) {
67
            $params['noAutoKickUser'] = $noAutoKickUser;
68
        }
69
        if (!empty($mergePublishRtmp)) {
70
            $params['mergePublishRtmp'] = $mergePublishRtmp;
71
        }
72
        $body = json_encode($params);
73
        return $this->post($url, $body);
74
    }
75
76
    /**
77
     * 获取应用信息
78
     *
79
     * @param string $appId
80
     * @return array
81
     * @link  https://doc.qnsdk.com/rtn/docs/server_overview#2_1
82
     */
83
    public function getApp($appId)
84
    {
85
        $url = $this->baseURL . '/' . $appId;
86
        return $this->get($url);
87
    }
88
89
    /**
90
     * 删除应用
91
     *
92
     * @param string $appId app 的唯一标识,创建的时候由系统生成
93
     * @return array
94
     * @link  https://doc.qnsdk.com/rtn/docs/server_overview#2_1
95
     */
96
    public function deleteApp($appId)
97
    {
98
        $url = $this->baseURL . '/' . $appId;
99
        return $this->delete($url);
100
    }
101
102
    /**
103
     * 获取房间内用户列表
104
     *
105
     * @param string $appId app 的唯一标识,创建的时候由系统生成
106
     * @param string $roomName 操作所查询的连麦房间
107
     * @return array
108
     * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_2
109
     */
110
    public function listUser($appId, $roomName)
111
    {
112
        $url = sprintf("%s/%s/rooms/%s/users", $this->baseURL, $appId, $roomName);
113
        return $this->get($url);
114
    }
115
116
    /**
117
     * 指定一个用户踢出房间
118
     *
119
     * @param string $appId app 的唯一标识,创建的时候由系统生成
120
     * @param string $roomName 连麦房间
121
     * @param string $userId 操作所剔除的用户
122
     * @return mixed
123
     * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_2
124
     */
125
    public function kickUser($appId, $roomName, $userId)
126
    {
127
        $url = sprintf("%s/%s/rooms/%s/users/%s", $this->baseURL, $appId, $roomName, $userId);
128
        return $this->delete($url);
129
    }
130
131
    /**
132
     * 停止一个房间的合流转推
133
     *
134
     * @param string $appId
135
     * @param string $roomName
136
     * @return array
137
     * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_2
138
     */
139
    public function stopMerge($appId, $roomName)
140
    {
141
        $url = sprintf("%s/%s/rooms/%s/merge", $this->baseURL, $appId, $roomName);
142
        return $this->delete($url);
143
    }
144
145
    /**
146
     * 获取应用中活跃房间
147
     *
148
     * @param string $appId 连麦房间所属的 app
149
     * @param null $prefix 所查询房间名的前缀索引,可以为空。
150
     * @param int $offset 分页查询的位移标记
151
     * @param int $limit 此次查询的最大长度
152
     * @return array
153
     * @link https://doc.qnsdk.com/rtn/docs/server_overview#2_2
154
     */
155
    public function listActiveRooms($appId, $prefix = null, $offset = null, $limit = null)
156
    {
157
        if (isset($prefix)) {
158
            $query['prefix'] = $prefix;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$query was never initialized. Although not strictly required by PHP, it is generally a good practice to add $query = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
159
        }
160
        if (isset($offset)) {
161
            $query['offset'] = $offset;
0 ignored issues
show
Bug introduced by
The variable $query does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
162
        }
163
        if (isset($limit)) {
164
            $query['limit'] = $limit;
165
        }
166
        if (isset($query) && !empty($query)) {
167
            $query = '?' . http_build_query($query);
168
            $url = sprintf("%s/%s/rooms%s", $this->baseURL, $appId, $query);
169
        } else {
170
            $url = sprintf("%s/%s/rooms", $this->baseURL, $appId);
171
        }
172
        return $this->get($url);
173
    }
174
175
    /**
176
     * 生成加入房间的令牌
177
     *
178
     * @param string $appId app 的唯一标识,创建的时候由系统生成
179
     * @param string $roomName 房间名称,需满足规格 ^[a-zA-Z0-9_-]{3,64}$
180
     * @param int $userId 请求加入房间的用户 ID,需满足规格 ^[a-zA-Z0-9_-]{3,50}$
181
     * @param int $expireAt 鉴权的有效时间,传入以秒为单位的64位 Unix 绝对时间
182
     * @param string $permission 该用户的房间管理权限,"admin" 或 "user",默认为 "user"
183
     * @return string
184
     * @link https://doc.qnsdk.com/rtn/docs/server_overview#1
185
     */
186
    public function appToken($appId, $roomName, $userId, $expireAt, $permission)
187
    {
188
        $params['appId'] = $appId;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
189
        $params['userId'] = $userId;
190
        $params['roomName'] = $roomName;
191
        $params['permission'] = $permission;
192
        $params['expireAt'] = $expireAt;
193
        $appAccessString = json_encode($params);
194
        return $this->auth->signWithData($appAccessString);
195
    }
196
197
    private function get($url, $cType = null)
198
    {
199
        $rtcToken = $this->auth->authorizationV2($url, "GET", null, $cType);
200
        $rtcToken['Content-Type'] = $cType;
201
        $ret = Client::get($url, $rtcToken);
202
        if (!$ret->ok()) {
203
            return array(null, new Error($url, $ret));
204
        }
205
        return array($ret->json(), null);
206
    }
207
208
    private function delete($url, $contentType = 'application/json')
209
    {
210
        $rtcToken = $this->auth->authorizationV2($url, "DELETE", null, $contentType);
211
        $rtcToken['Content-Type'] = $contentType;
212
        $ret = Client::delete($url, $rtcToken);
213
        if (!$ret->ok()) {
214
            return array(null, new Error($url, $ret));
215
        }
216
        return array($ret->json(), null);
217
    }
218
219
    private function post($url, $body, $contentType = 'application/json')
220
    {
221
        $rtcToken = $this->auth->authorizationV2($url, "POST", $body, $contentType);
222
        $rtcToken['Content-Type'] = $contentType;
223
        $ret = Client::post($url, $body, $rtcToken);
224
        if (!$ret->ok()) {
225
            return array(null, new Error($url, $ret));
226
        }
227
        $r = ($ret->body === null) ? array() : $ret->json();
228
        return array($r, null);
229
    }
230
}
231