Completed
Pull Request — master (#270)
by
unknown
57:33 queued 32:36
created

AppClient::listActiveRooms()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 16
nop 4
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace Qiniu\Rtc;
3
4
use Qiniu\Http\Client;
5
use Qiniu\Http\Error;
6
use Qiniu\Config;
7
use Qiniu\Auth;
8
9
class AppClient
10
{
11
    private $auth;
12
    private $baseURL;
13
14
    public function __construct(Auth $auth)
15
    {
16
        $this->auth = $auth;
17
18
        $this->baseURL = sprintf("%s/%s/apps", Config::RTCAPI_HOST, Config::RTCAPI_VERSION);
19
    }
20
21
    /*
22
     * hub: 直播空间名
23
     * title: app 的名称  注意,Title 不是唯一标识,重复 create 动作将生成多个 app
24
     * maxUsers:人数限制
25
     * NoAutoKickUser: bool 类型,可选,禁止自动踢人(抢流)。默认为 false ,
26
       即同一个身份的 client (app/room/user) ,新的连麦请求可以成功,旧连接被关闭。
27
     */
28
    public function createApp($hub, $title, $maxUsers = null, $noAutoKickUser = null)
29
    {
30
        $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...
31
        $params['title'] = $title;
32
        if (!empty($maxUsers)) {
33
            $params['maxUsers'] = $maxUsers;
34
        }
35
        if (!empty($noAutoKickUser)) {
36
            $params['noAutoKickUser'] = $noAutoKickUser;
37
        }
38
        $body = json_encode($params);
39
        $ret = $this->post($this->baseURL, $body);
40
        return $ret;
41
    }
42
43
    /*
44
     * appId: app 的唯一标识,创建的时候由系统生成。
45
     * Title: app 的名称, 可选。
46
     * Hub: 绑定的直播 hub,可选,用于合流后 rtmp 推流。
47
     * MaxUsers: int 类型,可选,连麦房间支持的最大在线人数。
48
     * NoAutoKickUser: bool 类型,可选,禁止自动踢人。
49
     * MergePublishRtmp: 连麦合流转推 RTMP 的配置,可选择。其详细配置包括如下
50
            Enable: 布尔类型,用于开启和关闭所有房间的合流功能。
51
            AudioOnly: 布尔类型,可选,指定是否只合成音频。
52
            Height, Width: int64,可选,指定合流输出的高和宽,默认为 640 x 480。
53
            OutputFps: int64,可选,指定合流输出的帧率,默认为 25 fps 。
54
            OutputKbps: int64,可选,指定合流输出的码率,默认为 1000 。
55
            URL: 合流后转推旁路直播的地址,可选,支持魔法变量配置按照连麦房间号生成不同的推流地址。如果是转推到七牛直播云,不建议使用该配置。
56
            StreamTitle: 转推七牛直播云的流名,可选,支持魔法变量配置按照连麦房间号生成不同的流名。例如,配置 Hub 为 qn-zhibo ,配置 StreamTitle 为 $(roomName) ,则房间 meeting-001 的合流将会被转推到 rtmp://pili-publish.qn-zhibo.***.com/qn-zhibo/meeting-001地址。详细配置细则,请咨询七牛技术支持。
57
     */
58
    public function updateApp($appId, $hub, $title, $maxUsers = null, $mergePublishRtmp = null, $noAutoKickUser = 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 (!empty($noAutoKickUser)) {
67
            $params['noAutoKickUser'] = $noAutoKickUser;
68
        }
69
        if (!empty($mergePublishRtmp)) {
70
            $params['mergePublishRtmp'] = $mergePublishRtmp;
71
        }
72
        $body = json_encode($params);
73
        $ret = $this->post($url, $body);
74
        return $ret;
75
    }
76
77
    /*
78
     * appId: app 的唯一标识,创建的时候由系统生成。
79
     */
80
    public function getApp($appId)
81
    {
82
        $url = $this->baseURL . '/' . $appId;
83
        $ret  = $this->get($url);
84
        return $ret;
85
    }
86
87
    /*
88
     * appId: app 的唯一标识,创建的时候由系统生成
89
     */
90
    public function deleteApp($appId)
91
    {
92
        $url = $this->baseURL . '/' . $appId;
93
        list(, $err)  = $this->delete($url);
94
        return $err;
95
    }
96
97
    /*
98
     * 获取房间的人数
99
     * appId: app 的唯一标识,创建的时候由系统生成。
100
     * roomName: 操作所查询的连麦房间。
101
     */
102
    public function listUser($appId, $roomName)
103
    {
104
        $url = sprintf("%s/%s/rooms/%s/users", $this->baseURL, $appId, $roomName);
105
        $ret  = $this->get($url);
106
        return $ret;
107
    }
108
109
   /*
110
    * 踢出玩家
111
    * appId: app 的唯一标识,创建的时候由系统生成。
112
    * roomName: 连麦房间
113
    * userId: 请求加入房间的用户ID
114
    */
115
    public function kickUser($appId, $roomName, $userId)
116
    {
117
        $url = sprintf("%s/%s/rooms/%s/users/%s", $this->baseURL, $appId, $roomName, $userId);
118
        list(, $err)  = $this->delete($url);
119
        return $err;
120
    }
121
122
    /*
123
     * 获取房间的人数
124
     * appId: app 的唯一标识,创建的时候由系统生成。
125
     * prefix: 所查询房间名的前缀索引,可以为空。
126
     * offset: int 类型,分页查询的位移标记。
127
     * limit: int 类型,此次查询的最大长度。
128
     * GET /v3/apps/<AppID>/rooms?prefix=<RoomNamePrefix>&offset=<Offset>&limit=<Limit>
129
     */
130
    public function listActiveRooms($appId, $prefix = null, $offset = null, $limit = null)
131
    {
132
        if (isset($prefix)) {
133
            $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...
134
        }
135
        if (isset($offset)) {
136
            $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...
137
        }
138
        if (isset($limit)) {
139
            $query['limit'] = $limit;
140
        }
141
        if ($query != null) {
142
            $query = '?' . http_build_query($query);
143
            $url = sprintf("%s/%s/rooms%s", $this->baseURL, $appId, $query);
144
        } else {
145
            $url = sprintf("%s/%s/rooms", $this->baseURL, $appId);
146
        }
147
        $ret  = $this->get($url);
148
        return $ret;
149
    }
150
151
    /*
152
     * appId: app 的唯一标识,创建的时候由系统生成。
153
     * roomName: 房间名称,需满足规格 ^[a-zA-Z0-9_-]{3,64}$
154
     * userId: 请求加入房间的用户 ID,需满足规格 ^[a-zA-Z0-9_-]{3,50}$
155
     * expireAt: int64 类型,鉴权的有效时间,传入以秒为单位的64位Unix
156
       绝对时间,token 将在该时间后失效。
157
     * permission: 该用户的房间管理权限,"admin" 或 "user",默认为 "user" 。
158
       当权限角色为 "admin" 时,拥有将其他用户移除出房间等特权.
159
     */
160
    public function appToken($appId, $roomName, $userId, $expireAt, $permission)
161
    {
162
        $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...
163
        $params['userId'] = $userId;
164
        $params['roomName'] = $roomName;
165
        $params['permission'] = $permission;
166
        $params['expireAt'] = $expireAt;
167
        $appAccessString = json_encode($params);
168
        return $this->auth->signWithData($appAccessString);
169
    }
170
171
    private function get($url, $cType = null)
172
    {
173
        $rtcToken = $this->auth->authorizationV2($url, "GET", $body, $cType);
0 ignored issues
show
Bug introduced by
The variable $body does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
174
        $rtcToken['Content-Type'] = $cType;
175
        $ret = Client::get($url, $rtcToken);
176
        if (!$ret->ok()) {
177
            return array(null, new Error($url, $ret));
178
        }
179
        return array($ret->json(), null);
180
    }
181
182
    private function delete($url, $cType = 'application/json')
183
    {
184
        $rtcToken = $this->auth->authorizationV2($url, "DELETE", $body, $cType);
0 ignored issues
show
Bug introduced by
The variable $body does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
185
        $rtcToken['Content-Type'] = $cType;
186
        $ret = Client::delete($url, $rtcToken);
187
        if (!$ret->ok()) {
188
            return array(null, new Error($url, $ret));
189
        }
190
        return array($ret->json(), null);
191
    }
192
193
    private function post($url, $body, $cType = 'application/json')
194
    {
195
        $rtcToken = $this->auth->authorizationV2($url, "POST", $body, $cType);
196
        $rtcToken['Content-Type'] = $cType;
197
        $ret = Client::post($url, $body, $rtcToken);
198
        if (!$ret->ok()) {
199
            return array(null, new Error($url, $ret));
200
        }
201
        $r = ($ret->body === null) ? array() : $ret->json();
202
        return array($r, null);
203
    }
204
}
205