Completed
Push — master ( 5d5473...3f41e3 )
by Luca
02:37
created

UserEndpoint   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 233
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A retrieveUsers() 0 11 3
A retrieveUser() 0 5 1
A createUser() 0 5 1
A deleteUser() 0 5 1
A updateUser() 0 6 1
A retrieveAllUserGroups() 0 5 1
A addUserToGroups() 0 6 1
A addUserToGroup() 0 5 1
A deleteUserFromGroups() 0 6 1
A deleteUserFromGroup() 0 5 1
A lockoutUser() 0 5 1
A unlockUser() 0 5 1
A retrieveUserRoster() 0 5 1
A createUserRosterEntry() 0 6 1
A deleteUserRosterEntry() 0 6 1
A updateUserRosterEntry() 0 6 1
1
<?php
2
/**
3
 * OpenFireRestAPI is based entirely on official documentation of the REST API
4
 * Plugin and you can extend it by following the directives of the documentation
5
 *
6
 * For the full copyright and license information, please read the LICENSE
7
 * file that was distributed with this source code. For the full list of
8
 * contributors, visit https://github.com/gnello/PHPOpenFireRestAPI/contributors
9
 *
10
 * @author Luca Agnello <[email protected]>
11
 * @link https://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html
12
 */
13
14
namespace Gnello\OpenFireRestAPI\Endpoints;
15
16
use \Gnello\OpenFireRestAPI\Dispatcher\Method;
17
use \Gnello\OpenFireRestAPI\Dispatcher\Dispatcher;
18
use \Gnello\OpenFireRestAPI\Settings\SubscriptionType;
19
use \Gnello\OpenFireRestAPI\Payloads;
20
21
/**
22
 * User related REST Endpoint
23
 * Class UserEndpoint
24
 * @package Gnello\OpenFireRestAPI\Endpoints
25
 * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#user-related-rest-endpoints
26
 */
27
class UserEndpoint extends Dispatcher
28
{
29
    public static $endpoint = '/users';
30
31
    /**
32
     * Get all or filtered users
33
     * @param string $search        Search/Filter by username.
34
     * @param string $propertyKey   Filter by user propertyKey.
35
     * @param string $propertyValue Filter by user propertyKey and propertyValue.
36
     * @return array with Users
37
     * @throws \Exception
38
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#retrieve-users
39
     */
40
    public static function retrieveUsers($search = null, $propertyKey = null, $propertyValue = null)
41
    {
42
        if (!empty($propertyValue) && empty($propertyKey)) {
43
            throw new \Exception("propertyValue can only be used within propertyKey parameter!");
44
        }
45
46
        $getData = compact($search, $propertyKey, $propertyValue);
47
        $getData = http_build_query($getData);
48
        $endpoint = self::$endpoint . '?' . $getData;
49
        return self::sendRequest(Method::GET, $endpoint);
50
    }
51
52
    /**
53
     * Get information over a specific user
54
     * @param $username
55
     * @return array
56
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#retrieve-a-user
57
     */
58
    public static function retrieveUser($username)
59
    {
60
        $endpoint = self::$endpoint . '/' . $username;
61
        return self::sendRequest(Method::GET, $endpoint);
62
    }
63
64
    /**
65
     * Create a new user
66
     * @param $username
67
     * @param $password
68
     * @param null $name
69
     * @param null $email
70
     * @param array $properties
71
     * @return array with HTTP status 201 (Created)
72
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#create-a-user
73
     */
74
    public static function createUser($username, $password, $name = null, $email = null, $properties = array())
75
    {
76
        $payload = new Payloads\UserPayload(compact('username', 'password', 'name', 'email', 'properties'));
77
        return self::sendRequest(Method::POST, self::$endpoint, $payload);
78
    }
79
80
    /**
81
     * Delete a user
82
     * @param $username
83
     * @return array with HTTP status 200 (OK)
84
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#delete-a-user
85
     */
86
    public static function deleteUser($username)
87
    {
88
        $endpoint = self::$endpoint . '/' . $username;
89
        return self::sendRequest(Method::DELETE, $endpoint);
90
    }
91
92
    /**
93
     * Update/rename a user
94
     * @param $username
95
     * @param $password
96
     * @param null $name
97
     * @param null $email
98
     * @param array $properties
99
     * @return array with HTTP status 200 (OK)
100
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#update-a-user
101
     */
102
    public static function updateUser($username, $password, $name = null, $email = null, $properties = array())
103
    {
104
        $payload = new Payloads\UserPayload(compact('username', 'password', 'name', 'email', 'properties'));
105
        $endpoint = self::$endpoint . '/' . $username;
106
        return self::sendRequest(Method::PUT, $endpoint, $payload);
107
    }
108
109
    /**
110
     * Get group names of a specific user
111
     * @param $username
112
     * @return array with Group
113
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#retrieve-all-user-Group
114
     */
115
    public static function retrieveAllUserGroups($username)
116
    {
117
        $endpoint = self::$endpoint . '/' . $username . GroupEndpoint::$endpoint;
118
        return self::sendRequest(Method::GET, $endpoint);
119
    }
120
121
    /**
122
     * Add user to a groups
123
     * @param $username
124
     * @param $groupname
125
     * @return array with HTTP status 201 (Created)
126
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#add-user-to-groups
127
     */
128
    public static function addUserToGroups($username, $groupname)
129
    {
130
        $payload = new Payloads\GroupPayload(compact('groupname'));
131
        $endpoint = self::$endpoint . '/' . $username . GroupEndpoint::$endpoint;
132
        return self::sendRequest(Method::POST, $endpoint, $payload);
133
    }
134
135
    /**
136
     * Add user to a group
137
     * @param $username
138
     * @param $groupName
139
     * @return array with HTTP status 201 (Created)
140
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#add-user-to-group
141
     */
142
    public static function addUserToGroup($username, $groupName)
143
    {
144
        $endpoint = self::$endpoint . '/' . $username . GroupEndpoint::$endpoint . '/' . $groupName;
145
        return self::sendRequest(Method::POST, $endpoint);
146
    }
147
148
    /**
149
     * Remove a user from a groups
150
     * @param $username
151
     * @param $groupname
152
     * @return array with HTTP status 200 (OK)
153
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#delete-a-user-from-a-groups
154
     */
155
    public static function deleteUserFromGroups($username, $groupname)
156
    {
157
        $payload = new Payloads\GroupPayload(compact('groupname'));
158
        $endpoint = self::$endpoint . '/' . $username . GroupEndpoint::$endpoint;
159
        return self::sendRequest(Method::DELETE, $endpoint, $payload);
160
    }
161
162
    /**
163
     * Remove a user from a group
164
     * @param $username
165
     * @param $groupName
166
     * @return array with HTTP status 200 (OK)
167
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#delete-a-user-from-a-group
168
     */
169
    public static function deleteUserFromGroup($username, $groupName)
170
    {
171
        $endpoint = self::$endpoint . '/' . $username . GroupEndpoint::$endpoint . '/' . $groupName;
172
        return self::sendRequest(Method::DELETE, $endpoint);
173
    }
174
175
    /**
176
     * Lockout/ban the user from the chat server.
177
     * The user will be kicked if the user is online.
178
     * @param $username
179
     * @return array with HTTP status 201 (Created)
180
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#lockout-a-user
181
     */
182
    public static function lockoutUser($username)
183
    {
184
        $endpoint = '/lockouts/' . $username;
185
        return self::sendRequest(Method::POST, $endpoint);
186
    }
187
188
    /**
189
     * Unlock/unban the user
190
     * @param $username
191
     * @return array with HTTP status 200 (OK)
192
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#unlock-a-user
193
     */
194
    public static function unlockUser($username)
195
    {
196
        $endpoint = '/lockouts/' . $username;
197
        return self::sendRequest(Method::DELETE, $endpoint);
198
    }
199
200
    /**
201
     * Get roster entries (buddies) from a specific user
202
     * @param $username
203
     * @return array with Roster
204
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#retrieve-user-roster
205
     */
206
    public static function retrieveUserRoster($username)
207
    {
208
        $endpoint = self::$endpoint . '/' . $username . '/roster';
209
        return self::sendRequest(Method::GET, $endpoint);
210
    }
211
212
    /**
213
     * Add a new roster entry to a user
214
     * @param $username
215
     * @param $jid
216
     * @param null $nickname
217
     * @param int $subscriptionType
218
     * @param int $groups
219
     * @return array with HTTP status 201 (Created)
220
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#create-a-user-roster-entry
221
     */
222
    public static function createUserRosterEntry($username, $jid, $nickname = null, $subscriptionType = SubscriptionType::BOTH, $groups)
223
    {
224
        $payload = new Payloads\RosterItemPayload(compact('jid', 'nickname', 'subscriptionType', 'groups'));
225
        $endpoint = self::$endpoint . '/' . $username . '/roster';
226
        return self::sendRequest(Method::POST, $endpoint, $payload);
227
    }
228
229
    /**
230
     * Remove a roster entry from a user
231
     * @param $username
232
     * @param $jid
233
     * @return array with HTTP status 200 (OK)
234
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#delete-a-user-roster-entry
235
     */
236
    public static function deleteUserRosterEntry($username, $jid)
237
    {
238
        $payload = new Payloads\RosterItemPayload(compact('jid'));
239
        $endpoint = self::$endpoint . '/' . $username . '/roster/' . $payload->getJid();
240
        return self::sendRequest(Method::DELETE, $endpoint);
241
    }
242
243
    /**
244
     * Update a roster entry
245
     * @param $username
246
     * @param $jid
247
     * @param null $nickname
248
     * @param $subscriptionType
249
     * @param $groups
250
     * @return array with HTTP status 200 (OK)
251
     * @link http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#update-a-user-roster-entry
252
     */
253
    public static function updateUserRosterEntry($username, $jid, $nickname = null, $subscriptionType = SubscriptionType::BOTH, $groups)
254
    {
255
        $payload = new Payloads\RosterItemPayload(compact('jid', 'nickname', 'subscriptionType', 'groups'));
256
        $endpoint = self::$endpoint . '/' . $username . '/roster/' . $payload->getJid();
257
        return self::sendRequest(Method::PUT, $endpoint, $payload);
258
    }
259
}