Completed
Pull Request — master (#267)
by
unknown
08:50
created

AppClient::createApp()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 32
nop 6
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
namespace QiniuRtc;
3
4
class AppClient
5
{
6
    private $_transport;
7
    private $_mac;
8
    private $_baseURL;
9
10
    public function __construct($mac)
11
    {
12
        $this->_mac = $mac;
13
        $this->_transport = new Transport($mac);
14
15
        $cfg = Config::getInstance();
16
        $this->_baseURL = sprintf("%s/%s/apps", $cfg->RTCAPI_HOST, $cfg->RTCAPI_VERSION);
17
    }
18
19
    /*
20
     * hub: 直播空间名
21
     * title: app 的名称  注意,Title 不是唯一标识,重复 create 动作将生成多个 app
22
     * NoAutoCloseRoom: bool 类型,可选,禁止自动关闭房间。默认为 false ,即用户退出房间后,房间会被主动清理释放。
23
     * NoAutoCreateRoom: bool 类型,可选,禁止自动创建房间。默认为 false ,即不需要主动调用接口创建即可加入房间。
24
     * NoAutoKickUser: bool 类型,可选,禁止自动踢人(抢流)。默认为 false ,即同一个身份的 client (app/room/user) ,新的连麦请求可以成功,旧连接被关闭。
25
     */
26
    public function createApp($hub, $title, $maxUsers = null, $noAutoCloseRoom = null, $noAutoCreateRoom = null, $noAutoKickUser = null)
27
    {
28
        $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...
29
        $params['title'] = $title;
30
        if (!empty($maxUsers)) {
31
            $params['maxUsers'] = $maxUsers;
32
        }
33
        if (!empty($noAutoCloseRoom)) {
34
            $params['noAutoCloseRoom'] = $noAutoCloseRoom;
35
        }
36
        if (!empty($noAutoCreateRoom)) {
37
            $params['noAutoCreateRoom'] = $noAutoCreateRoom;
38
        }
39
        if (!empty($noAutoKickUser)) {
40
            $params['noAutoKickUser'] = $noAutoKickUser;
41
        }
42
        $body = json_encode($params);
43
        try {
44
            $ret = $this->_transport->send(HttpRequest::POST, $this->_baseURL, $body);
45
        } catch (\Exception $e) {
46
            return $e;
47
        }
48
        return $ret;
49
    }
50
51
    /*
52
     * appId: app 的唯一标识,创建的时候由系统生成。
53
     * Title: app 的名称, 可选。
54
     * Hub: 绑定的直播 hub,可选,用于合流后 rtmp 推流。
55
     * MaxUsers: int 类型,可选,连麦房间支持的最大在线人数。
56
     * NoAutoCloseRoom: bool 指针类型,可选,true 表示禁止自动关闭房间。
57
     * NoAutoCreateRoom: bool 指针指型,可选,true 表示禁止自动创建房间。
58
     * NoAutoKickUser: bool 类型,可选,禁止自动踢人。
59
     * MergePublishRtmp: 连麦合流转推 RTMP 的配置,可选择。其详细配置包括如下
60
            Enable: 布尔类型,用于开启和关闭所有房间的合流功能。
61
            AudioOnly: 布尔类型,可选,指定是否只合成音频。
62
            Height, Width: int64,可选,指定合流输出的高和宽,默认为 640 x 480。
63
            OutputFps: int64,可选,指定合流输出的帧率,默认为 25 fps 。
64
            OutputKbps: int64,可选,指定合流输出的码率,默认为 1000 。
65
            URL: 合流后转推旁路直播的地址,可选,支持魔法变量配置按照连麦房间号生成不同的推流地址。如果是转推到七牛直播云,不建议使用该配置。
66
            StreamTitle: 转推七牛直播云的流名,可选,支持魔法变量配置按照连麦房间号生成不同的流名。例如,配置 Hub 为 qn-zhibo ,配置 StreamTitle 为 $(roomName) ,则房间 meeting-001 的合流将会被转推到 rtmp://pili-publish.qn-zhibo.***.com/qn-zhibo/meeting-001地址。详细配置细则,请咨询七牛技术支持。
67
     */
68
    public function updateApp($appId, $hub, $title, $maxUsers = null, $mergePublishRtmp = null, $noAutoCloseRoom = null, $noAutoCreateRoom = null, $noAutoKickUser = null)
69
    {
70
        $url = $this->_baseURL . '/' . $appId;
71
        $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...
72
        $params['title'] = $title;
73
        if (!empty($maxUsers)) {
74
            $params['maxUsers'] = $maxUsers;
75
        }
76
        if (!empty($noAutoCloseRoom)) {
77
            $params['noAutoCloseRoom'] = $noAutoCloseRoom;
78
        }
79
        if (!empty($noAutoCreateRoom)) {
80
            $params['noAutoCreateRoom'] = $noAutoCreateRoom;
81
        }
82
        if (!empty($noAutoKickUser)) {
83
            $params['noAutoKickUser'] = $noAutoKickUser;
84
        }
85
        if (!empty($mergePublishRtmp)) {
86
            $params['mergePublishRtmp'] = $mergePublishRtmp;
87
        }
88
        $body = json_encode($params);
89
        try {
90
            $ret = $this->_transport->send(HttpRequest::POST, $url, $body);
91
        } catch (\Exception $e) {
92
            return $e;
93
        }
94
        return $ret;
95
    }
96
97
    /*
98
     * appId: app 的唯一标识,创建的时候由系统生成。
99
     */
100
    public function getApp($appId)
101
    {
102
        $url = $this->_baseURL . '/' . $appId;
103
        try {
104
            $ret = $this->_transport->send(HttpRequest::GET, $url);
105
        } catch (\Exception $e) {
106
            return $e;
107
        }
108
        return $ret;
109
    }
110
111
    /*
112
     * appId: app 的唯一标识,创建的时候由系统生成
113
     */
114
    public function deleteApp($appId)
115
    {
116
        $url = $this->_baseURL . '/' . $appId;
117
        try {
118
            $ret = $this->_transport->send(HttpRequest::DELETE, $url);
119
        } catch (\Exception $e) {
120
            return $e;
121
        }
122
        return $ret;
123
    }
124
125
    /*
126
     * 获取房间的人数
127
     * appId: app 的唯一标识,创建的时候由系统生成。
128
     * roomName: 操作所查询的连麦房间。
129
     */
130
    public function getappUserNum($appId, $roomName)
131
    {
132
        $url = sprintf("%s/%s/rooms/%s/users", $this->_baseURL, $appId, $roomName);
133
        try {
134
            $ret = $this->_transport->send(HttpRequest::GET, $url);
135
        } catch (\Exception $e) {
136
            return $e;
137
        }
138
        return $ret;
139
    }
140
141
   /*
142
    * 踢出玩家
143
    * appId: app 的唯一标识,创建的时候由系统生成。
144
    * roomName: 连麦房间
145
    * userId: 请求加入房间的用户ID
146
    */
147
    public function kickingPlayer($appId, $roomName, $userId)
148
    {
149
        $url = sprintf("%s/%s/rooms/%s/users/%s", $this->_baseURL, $appId, $roomName, $userId);
150
        try {
151
            $ret = $this->_transport->send(HttpRequest::DELETE, $url);
152
        } catch (\Exception $e) {
153
            return $e;
154
        }
155
        return $ret;
156
    }
157
158
    /*
159
     * 获取房间的人数
160
     * appId: app 的唯一标识,创建的时候由系统生成。
161
     * prefix: 所查询房间名的前缀索引,可以为空。
162
     * offset: int 类型,分页查询的位移标记。
163
     * limit: int 类型,此次查询的最大长度。
164
     * GET /v3/apps/<AppID>/rooms?prefix=<RoomNamePrefix>&offset=<Offset>&limit=<Limit>
165
     */
166
    public function listRooms($appId, $prefix = null, $offset = null, $limit = null)
167
    {
168
        if(isset($prefix)){
169
            $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...
170
        }
171
        if(isset($offset)){
172
            $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...
173
        }
174
        if(isset($limit)){
175
            $query['limit'] = $limit;
176
        }
177
        if ($query != null) {
178
            $query = '?' . http_build_query($query);
179
            $url = sprintf("%s/%s/rooms%s", $this->_baseURL, $appId, $query);
180
        } else {
181
            $url = sprintf("%s/%s/rooms", $this->_baseURL, $appId);
182
        }
183
        try {
184
            $ret = $this->_transport->send(HttpRequest::GET, $url);
185
        } catch (\Exception $e) {
186
            return $e;
187
        }
188
        return $ret;
189
    }
190
191
    /*
192
     * appId: app 的唯一标识,创建的时候由系统生成。
193
     * roomName: 房间名称,需满足规格 ^[a-zA-Z0-9_-]{3,64}$
194
     * userId: 请求加入房间的用户 ID,需满足规格 ^[a-zA-Z0-9_-]{3,50}$
195
     * expireAt: int64 类型,鉴权的有效时间,传入以秒为单位的64位Unix绝对时间,token 将在该时间后失效。
196
     * permission: 该用户的房间管理权限,"admin" 或 "user",默认为 "user" 。当权限角色为 "admin" 时,拥有将其他用户移除出房间等特权.
197
     */
198
    public function appToken($appId, $roomName, $userId, $expireAt, $permission)
199
    {
200
        $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...
201
        $params['userId'] = $userId;
202
        $params['roomName'] = $roomName;        
203
        $params['permission'] = $permission;
204
        $params['expireAt'] = $expireAt;
205
        $appAccessString = json_encode($params);
206
        $encodedappAccess = Utils::base64UrlEncode($appAccessString);
207
        $sign = hash_hmac('sha1', $encodedappAccess, $this->_mac->_secretKey, true);
208
        $encodedSign = Utils::base64UrlEncode($sign);
209
        return $this->_mac->_accessKey . ":" . $encodedSign . ":" . $encodedappAccess;
210
    }
211
}
212