Skip to content

Commit 05edaf9

Browse files
committed
added User class (fix: #14)
1 parent fdb04a9 commit 05edaf9

File tree

4 files changed

+175
-79
lines changed

4 files changed

+175
-79
lines changed

src/API.php

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,16 @@ public function uploadTrack($path)
293293
/**
294294
* getUsers
295295
*
296-
* @return array users
296+
* @return User[] users
297297
*/
298298
public function getUsers()
299299
{
300-
return $this->request('/bot/users');
300+
$users = $this->request('/bot/users');
301+
$out = [];
302+
foreach ($users as $user) {
303+
array_push($out, new User($this->token, $this->url, $this->timeout, $user));
304+
}
305+
return $out;
301306
}
302307

303308
/**
@@ -306,88 +311,53 @@ public function getUsers()
306311
* @param string $username Username
307312
* @param string $password Password
308313
* @param integer $privileges Bitmask-Value
309-
* @return array status
314+
* @return User user object
310315
*/
311316
public function addUser($username, $password, $privileges = 0)
312317
{
313-
return $this->request('/bot/users', 'POST', [
318+
$this->request('/bot/users', 'POST', [
314319
'username'=>$username,
315320
'password'=>$password,
316321
'privileges'=>$privileges,
317322
]);
323+
$users = $this->getUsers();
324+
foreach ($users as $user) {
325+
if ($user->getName() === $username) {
326+
return $user;
327+
}
328+
}
318329
}
319-
320-
/**
321-
* setUserPassword
322-
*
323-
* @param string $password Password
324-
* @param string $userUUID user uuid
325-
* @return array status
326-
*/
327-
public function setUserPassword($password, $userUUID)
328-
{
329-
return $this->request('/bot/users/'.$userUUID, 'PATCH', [
330-
'password'=>$password,
331-
]);
332-
}
333-
334-
335-
/**
336-
* setUserPrivileges
337-
*
338-
* @param integer $privileges Bitmask-Value
339-
* @param string $userUUID user UUID
340-
* @return array status
341-
*/
342-
public function setUserPrivileges($privileges, $userUUID)
343-
{
344-
return $this->request('/bot/users/'.$userUUID, 'PATCH', [
345-
'privileges'=>$privileges,
346-
]);
347-
}
348-
349-
/**
350-
* setUserIdentity
351-
*
352-
* @param string $identity teamspeak identity
353-
* @param string $userUUID SinusBot user UUID
354-
* @return array status
355-
*/
356-
public function setUserIdentity($identity, $userUUID)
357-
{
358-
return $this->request('/bot/users/'.$userUUID, 'PATCH', [
359-
'tsuid'=>$identity,
360-
]);
361-
}
362-
363-
364330
/**
365-
* setUserServergroup
331+
* getUserByUUID
366332
*
367-
* @param string $groupID TeamSpeak Group ID
368-
* @param string $userUUID SinusBot User UUID
369-
* @return array status
333+
* @param string $uuid User ID
334+
* @return User user object
370335
*/
371-
public function setUserServergroup($groupID, $userUUID)
336+
public function getUserByUUID($uuid)
372337
{
373-
return $this->request('/bot/users/'.$userUUID, 'PATCH', [
374-
'tsgid'=>$groupID,
375-
]);
338+
$users = $this->getUsers();
339+
foreach ($users as $user) {
340+
if ($user->getUUID() === $uuid) {
341+
return $user;
342+
}
343+
}
376344
}
377-
378-
379345
/**
380-
* deleteUser
346+
* getUserByName
381347
*
382-
* @param string $userUUID SinusBot User UUID
383-
* @return array status
348+
* @param string $username Username
349+
* @return User user object
384350
*/
385-
public function deleteUser($userUUID)
351+
public function getUserByName($username)
386352
{
387-
return $this->request('/bot/users/'.$userUUID, 'DELETE');
353+
$users = $this->getUsers();
354+
foreach ($users as $user) {
355+
if ($user->getName() === $username) {
356+
return $user;
357+
}
358+
}
388359
}
389360

390-
391361
/**
392362
* getInstances
393363
*

src/RestClient.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,9 @@ protected function request($path, $method = "GET", $payload = null, $encoded = f
6565
}
6666
}
6767
$data = curl_exec($ch);
68-
69-
if ($data === false) {
70-
$data = [
71-
'success' => false,
72-
'error' => curl_error($ch)
73-
];
74-
} else {
75-
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
76-
if ($httpcode != 200 and $httpcode != 201) {
77-
$data = [
78-
'success' => false,
79-
'error' => $this->getError($httpcode)
80-
];
81-
}
68+
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
69+
if ($httpcode != 200 && $httpcode != 201) {
70+
throw new \Exception('Not expected http status code: '.$httpcode." (".$this->getError($httpcode).")");
8271
}
8372

8473
curl_close($ch);

src/User.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* Class User | src/User.php
4+
*
5+
* A single User with his available actions
6+
*
7+
* @package SinusBot
8+
* @author Max Schmitt <max@schmitt.mx>
9+
*/
10+
11+
namespace SinusBot;
12+
13+
/**
14+
* User Instance
15+
*
16+
* User represents a single User of the SinusBot
17+
*/
18+
class User extends RestClient
19+
{
20+
/**
21+
* UUID holds the User UUID
22+
* @var array
23+
*/
24+
public $uuid = null;
25+
/**
26+
* User stores the initial received user data
27+
* @var array
28+
*/
29+
private $user = null;
30+
/**
31+
* __construct
32+
*
33+
* @param string $token SinusBot auth token
34+
* @param string $url SinusBot Bot URL
35+
* @param int $timeout HTTP Timeout which is used to perform HTTP API requests
36+
* @param array $user SinusBot User array.
37+
* @return void
38+
*/
39+
public function __construct($token, $url, $timeout, $user)
40+
{
41+
$this->token = $token;
42+
$this->url = $url;
43+
$this->timeout = $timeout;
44+
$this->uuid = $user['id'];
45+
$this->user = $user;
46+
}
47+
/**
48+
* getName returns the username
49+
*
50+
* @return string username
51+
* @api
52+
*/
53+
public function getName()
54+
{
55+
return array_key_exists('username', $this->user)?$this->user['username']:'';
56+
}
57+
/**
58+
* getUUID returns the uuid
59+
*
60+
* @return string user UUID
61+
* @api
62+
*/
63+
public function getUUID()
64+
{
65+
return $this->uuid;
66+
}
67+
68+
/**
69+
* setPassword
70+
*
71+
* @param string $password Password
72+
* @return array status
73+
* @api
74+
*/
75+
public function setPassword($password)
76+
{
77+
return $this->request('/bot/users/'.$this->uuid, 'PATCH', [
78+
'password'=>$password,
79+
]);
80+
}
81+
82+
/**
83+
* setPrivileges
84+
*
85+
* @param integer $privileges Bitmask-Value
86+
* @return array status
87+
* @api
88+
*/
89+
public function setPrivileges($privileges)
90+
{
91+
return $this->request('/bot/users/'.$this->uuid, 'PATCH', [
92+
'privileges'=>$privileges,
93+
]);
94+
}
95+
96+
/**
97+
* setIdentity
98+
*
99+
* @param string $identity teamspeak identity
100+
* @return array status
101+
* @api
102+
*/
103+
public function setIdentity($identity)
104+
{
105+
return $this->request('/bot/users/'.$this->uuid, 'PATCH', [
106+
'tsuid'=>$identity,
107+
]);
108+
}
109+
110+
111+
/**
112+
* setServergroup
113+
*
114+
* @param string $groupID TeamSpeak Group ID
115+
* @return array status
116+
* @api
117+
*/
118+
public function setServergroup($groupID)
119+
{
120+
return $this->request('/bot/users/'.$this->uuid, 'PATCH', [
121+
'tsgid'=>strval($groupID),
122+
]);
123+
}
124+
125+
126+
/**
127+
* delete
128+
*
129+
* @return array status
130+
* @api
131+
*/
132+
public function delete()
133+
{
134+
return $this->request('/bot/users/'.$this->uuid, 'DELETE');
135+
}
136+
}

src/autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
include_once("RestClient.php");
1212
include_once("Instance.php");
1313
include_once("Playlist.php");
14+
include_once("User.php");
1415
include_once("API.php");

0 commit comments

Comments
 (0)