Completed
Pull Request — master (#267)
by
unknown
24:40
created

AppClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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