Completed
Pull Request — master (#267)
by
unknown
10:23
created

AppClient::deleteApp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Qiniu\Rtc;
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)
0 ignored issues
show
Unused Code introduced by
The parameter $userId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
    {
149
        $url = sprintf("%s/%s/rooms/%s/users/%s", $this->_baseURL, $appId, $roomName, $UserId);
0 ignored issues
show
Bug introduced by
The variable $UserId does not exist. Did you mean $userId?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
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, $offset, $limit)
0 ignored issues
show
Unused Code introduced by
The parameter $prefix is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $offset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $limit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
    {
168
        $url = sprintf("%s/%s/rooms", $this->_baseURL, $appId);
169
        try {
170
            $ret = $this->_transport->send(HttpRequest::GET, $url);
171
        } catch (\Exception $e) {
172
            return $e;
173
        }
174
        return $ret;
175
    }
176
177
    /*
178
     * appId: app 的唯一标识,创建的时候由系统生成。
179
     * roomName: 房间名称,需满足规格 ^[a-zA-Z0-9_-]{3,64}$
180
     * userId: 请求加入房间的用户 ID,需满足规格 ^[a-zA-Z0-9_-]{3,50}$
181
     * expireAt: int64 类型,鉴权的有效时间,传入以秒为单位的64位Unix绝对时间,token 将在该时间后失效。
182
     * permission: 该用户的房间管理权限,"admin" 或 "user",默认为 "user" 。当权限角色为 "admin" 时,拥有将其他用户移除出房间等特权.
183
     */
184
    public function appToken($appId, $roomName, $userId, $expireAt, $permission)
185
    {
186
        $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...
187
        $params['userId'] = $userId;
188
        $params['roomName'] = $roomName;        
189
        $params['permission'] = $permission;
190
        $params['expireAt'] = $expireAt;
191
        $appAccessString = json_encode($params);
192
        $encodedappAccess = Utils::base64UrlEncode($appAccessString);
193
        $sign = hash_hmac('sha1', $encodedappAccess, $this->_mac->_secretKey, true);
194
        $encodedSign = Utils::base64UrlEncode($sign);
195
        return $this->_mac->_accessKey . ":" . $encodedSign . ":" . $encodedappAccess;
196
    }
197
}
198