Completed
Pull Request — master (#21)
by Саша
02:39
created

OpenFireRestApi::unlockUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
	
3
namespace Gidkom\OpenFireRestApi;
4
5
use GuzzleHttp\Client;
6
7
class OpenFireRestApi
8
{
9
    public $host = 'localhost';
10
    public $port = '9090';
11
    public $plugin = '/plugins/restapi/v1';
12
    public $secret = 'SuperSecret';
13
    public $useSSL = false;
14
    protected $params  = array();
15
    public $client;
16
17
    public function __construct()
18
    {
19
        $this->client = new Client();
20
    }
21
22
    /**
23
     * Make the request and analyze the result
24
     *
25
     * @param   string          $type           Request method
26
     * @param   string          $endpoint       Api request endpoint
27
     * @param   array           $params         Parameters
28
     * @return  array|false                     Array with data or error, or False when something went fully wrong
29
     */
30
    
31
    protected function doRequest($type, $endpoint, $params=array())
32
    {
33
    	$base = ($this->useSSL) ? "https" : "http";
34
    	$url = $base . "://" . $this->host . ":" .$this->port.$this->plugin.$endpoint;
35
    	$headers = array(
36
  			'Accept' => 'application/json',
37
  			'Authorization' => $this->secret
38
  		);
39
40
        $body = json_encode($params);
41
42
        switch ($type) {
43
            case 'get':
44
                $result = $this->client->get($url, compact('headers'));
45
                break;
46 View Code Duplication
            case 'post':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
                $headers += ['Content-Type'=>'application/json'];
48
                $result = $this->client->post($url, compact('headers','body'));
49
                break;
50 View Code Duplication
            case 'delete':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
                $headers += ['Content-Type'=>'application/json'];
52
                $result = $this->client->delete($url, compact('headers','body'));
53
                break;
54 View Code Duplication
            case 'put':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
                $headers += ['Content-Type'=>'application/json'];
56
                $result = $this->client->put($url, compact('headers','body'));
57
                break;
58
            default:
59
                $result = null;
60
                break;
61
        }
62
        
63
        if ($result->getStatusCode() == 200 || $result->getStatusCode() == 201) {
64
            return array('status'=>true, 'message'=>json_decode($result->getBody()));
65
        }
66
        return array('status'=>false, 'message'=>json_decode($result->getBody()));
67
    	
68
    }
69
    
70
71
    /**
72
     * Get all registered users
73
     *
74
     * @return json|false       Json with data or error, or False when something went fully wrong
75
     */
76
    public function getUsers()
77
    {
78
    	$endpoint = '/users';        
79
    	return $this->doRequest('get',$endpoint);
80
    }
81
82
83
    /**
84
     * Get information for a specified user
85
     *
86
     * @return json|false       Json with data or error, or False when something went fully wrong
87
     */
88
    public function getUser($username)
89
    {
90
        $endpoint = '/users/'.$username; 
91
        return $this->doRequest('get', $endpoint);
92
    }
93
94
95
    /**
96
     * Creates a new OpenFire user
97
     *
98
     * @param   string          $username   Username
99
     * @param   string          $password   Password
100
     * @param   string|false    $name       Name    (Optional)
101
     * @param   string|false    $email      Email   (Optional)
102
     * @param   string[]|false  $groups     Groups  (Optional)
103
     * @return  json|false                 Json with data or error, or False when something went fully wrong
104
     */
105
    public function addUser($username, $password, $name=false, $email=false, $groups=false)
106
    {
107
        $endpoint = '/users'; 
108
        return $this->doRequest('post', $endpoint, compact('username', 'password','name','email', 'groups'));
109
    }
110
111
112
    /**
113
     * Deletes an OpenFire user
114
     *
115
     * @param   string          $username   Username
116
     * @return  json|false                 Json with data or error, or False when something went fully wrong
117
     */
118
    public function deleteUser($username)
119
    {
120
        $endpoint = '/users/'.$username; 
121
        return $this->doRequest('delete', $endpoint);
122
    }
123
124
    /**
125
     * Updates an OpenFire user
126
     *
127
     * @param   string          $username   Username
128
     * @param   string|false    $password   Password (Optional)
129
     * @param   string|false    $name       Name (Optional)
130
     * @param   string|false    $email      Email (Optional)
131
     * @param   string[]|false  $groups     Groups (Optional)
132
     * @return  json|false                 Json with data or error, or False when something went fully wrong
133
     */
134
    public function updateUser($username, $password, $name=false, $email=false, $groups=false)
135
    {
136
        $endpoint = '/users/'.$username; 
137
        return $this->doRequest('put', $endpoint, compact('username', 'password','name','email', 'groups'));
138
    }
139
140
     /**
141
     * locks/Disables an OpenFire user
142
     *
143
     * @param   string          $username   Username
144
     * @return  json|false                 Json with data or error, or False when something went fully wrong
145
     */
146
    public function lockoutUser($username)
147
    {
148
        $endpoint = '/lockouts/'.$username; 
149
        return $this->doRequest('post', $endpoint);
150
    }
151
152
153
    /**
154
     * unlocks an OpenFire user
155
     *
156
     * @param   string          $username   Username
157
     * @return  json|false                 Json with data or error, or False when something went fully wrong
158
     */
159
    public function unlockUser($username)
160
    {
161
        $endpoint = '/lockouts/'.$username; 
162
        return $this->doRequest('delete', $endpoint);
163
    }
164
165
166
    /**
167
     * Adds to this OpenFire user's roster
168
     *
169
     * @param   string          $username       	Username
170
     * @param   string          $jid            	JID
171
     * @param   string|false    $name           	Name         (Optional)
172
     * @param   int|false       $subscriptionType   	Subscription (Optional)
173
     * @return  json|false                     		Json with data or error, or False when something went fully wrong
174
     */
175
    public function addToRoster($username, $jid, $name=false, $subscriptionType=false)
176
    {
177
        $endpoint = '/users/'.$username.'/roster';
178
        return $this->doRequest('post', $endpoint, compact('jid','name','subscriptionType'));
179
    }
180
181
182
    /**
183
     * Removes from this OpenFire user's roster
184
     *
185
     * @param   string          $username   Username
186
     * @param   string          $jid        JID
187
     * @return  json|false                 Json with data or error, or False when something went fully wrong
188
     */
189
    public function deleteFromRoster($username, $jid)
190
    {
191
        $endpoint = '/users/'.$username.'/roster/'.$jid;
192
        return $this->doRequest('delete', $endpoint, $jid);
0 ignored issues
show
Documentation introduced by
$jid is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
193
    }
194
195
    /**
196
     * Updates this OpenFire user's roster
197
     *
198
     * @param   string          $username           Username
199
     * @param   string          $jid                 JID
200
     * @param   string|false    $nickname           Nick Name (Optional)
201
     * @param   int|false       $subscriptionType   Subscription (Optional)
202
     * @return  json|false                          Json with data or error, or False when something went fully wrong
203
     */
204
    public function updateRoster($username, $jid, $nickname=false, $subscriptionType=false)
0 ignored issues
show
Unused Code introduced by
The parameter $nickname 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...
205
    {
206
        $endpoint = '/users/'.$username.'/roster/'.$jid;
207
        return $this->doRequest('put', $endpoint, $jid, compact('jid','username','subscriptionType'));     
0 ignored issues
show
Documentation introduced by
$jid is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to OpenFireRestApi::doRequest() has too many arguments starting with compact('jid', 'username', 'subscriptionType').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
208
    }
209
210
    /**
211
     * Get all groups
212
     *
213
     * @return  json|false      Json with data or error, or False when something went fully wrong
214
     */
215
    public function getGroups()
216
    {
217
        $endpoint = '/groups';
218
        return $this->doRequest('get', $endpoint);
219
    }
220
221
    /**
222
     *  Retrieve a group
223
     *
224
     * @param  string   $name                       Name of group
225
     * @return  json|false                          Json with data or error, or False when something went fully wrong
226
     */
227
    public function getGroup($name)
228
    {
229
        $endpoint = '/groups/'.$name;
230
        return $this->doRequest('get', $endpoint);
231
    }
232
233
    /**
234
     * Create a group 
235
     *
236
     * @param   string   $name                      Name of the group
237
     * @param   string   $description               Some description of the group
238
     *
239
     * @return  json|false                          Json with data or error, or False when something went fully wrong
240
     */
241
    public function createGroup($name, $description = false)
242
    {
243
        $endpoint = '/groups/';
244
        return $this->doRequest('post', $endpoint, compact('name','description'));
245
    }
246
247
    /**
248
     * Delete a group
249
     *
250
     * @param   string      $name               Name of the Group to delete
251
     * @return  json|false                          Json with data or error, or False when something went fully wrong
252
     */
253
    public function deleteGroup($name)
254
    {
255
        $endpoint = '/groups/'.$name;
256
        return $this->doRequest('delete', $endpoint);
257
    }
258
259
    /**
260
     * Update a group (description)
261
     *
262
     * @param   string      $name               Name of group
263
     * @param   string      $description        Some description of the group
264
     *
265
     */
266
    public function updateGroup($name,  $description)
267
    {
268
        $endpoint = '/groups/'.$name;
269
        return $this->doRequest('put', $endpoint, compact('name','description'));
270
    }
271
272
    /**
273
     * Gell all active sessions
274
     *
275
     * @return json|false       Json with data or error, or False when something went fully wrong
276
     */
277
    public function getSessions()
278
    {
279
        $endpoint = '/sessions';
280
        return $this->doRequest('get', $endpoint);
281
    }
282
283
    public function getChatRoom($name)
284
    {
285
        return $this->doRequest('get', '/chatrooms/'.$name);
286
    }
287
288
    public function createChatRoom($naturalName, $roomName, $description)
289
    {
290
        return $this->doRequest('post', '/chatrooms', compact('naturalName', 'roomName', 'description'));
291
    }
292
293
    public function deleteChatRoom($name)
294
    {
295
        return $this->doRequest('delete', '/chatrooms/'.$name);
296
    }
297
}
298