GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#3)
by Ema
19:48 queued 09:52
created

UsersApi::getUserStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pnz\MattermostClient\Api;
6
7
use Pnz\MattermostClient\Exception\InvalidArgumentException;
8
use Pnz\MattermostClient\Model\Status;
9
use Pnz\MattermostClient\Model\Team\Teams;
10
use Pnz\MattermostClient\Model\User\User;
11
use Pnz\MattermostClient\Model\User\Users;
12
use Pnz\MattermostClient\Model\User\UserStatus;
13
use Psr\Http\Message\ResponseInterface;
14
15
final class UsersApi extends HttpApi
16
{
17
    /**
18
     * @param string $loginId  The login Id
19
     * @param string $password The password
20
     * @param string $token    The login token, as output variable
21
     *
22
     * @return User|ResponseInterface
23 10
     */
24
    public function login(string $loginId, string $password, string &$token = null)
25 10
    {
26 2
        if (empty($loginId) || empty($password)) {
27
            throw new InvalidArgumentException('LoginId and Password cannot be empty');
28
        }
29 8
30 8
        $response = $this->httpPost('/users/login', [
31 8
            'login_id' => $loginId,
32
            'password' => $password,
33
        ]);
34
35 8
        // Use any valid status code here
36 7
        if (200 !== $response->getStatusCode()) {
37
            $this->handleErrors($response);
38
        }
39 1
40 1
        $tokens = $response->getHeader('Token');
41 1
        if (\count($tokens)) {
42
            $token = reset($tokens);
43
        }
44 1
45
        return $this->handleResponse($response, User::class);
46
    }
47
48
    /**
49
     * Returns an user by its ID, use "me" to get the current logged in user.
50
     *
51
     * @param string $userId User GUID
52
     *
53
     * @return User|ResponseInterface
54 9
     */
55
    public function getUserById(string $userId)
56 9
    {
57 1
        if (empty($userId)) {
58
            throw new InvalidArgumentException('UserId can not be empty');
59
        }
60 8
61
        $response = $this->httpGet(sprintf('/users/%s', $userId));
62 8
63
        return $this->handleResponse($response, User::class);
64
    }
65
66
    /**
67
     * Get a list of teams that a user is on.
68
     *
69
     * @param string $userId User GUID
70
     *
71
     * @return User|ResponseInterface
72 9
     */
73
    public function getUserTeams(string $userId)
74 9
    {
75 1
        if (empty($userId)) {
76
            throw new InvalidArgumentException('UserId can not be empty');
77
        }
78 8
79
        $response = $this->httpGet(sprintf('/users/%s/teams', $userId));
80 8
81
        return $this->handleResponse($response, Teams::class);
82
    }
83
84
    /**
85
     * Returns a collection of users matching the given IDs.
86
     *
87
     * @param string[] $userIds
88
     *
89
     * @return Users|ResponseInterface
90 9
     */
91
    public function getUsersByIds(array $userIds)
92 9
    {
93 1
        if (empty($userIds)) {
94
            throw new InvalidArgumentException('UserIDs can not be empty');
95
        }
96 8
97
        $response = $this->httpPost('/users/ids', $userIds);
98 8
99
        return $this->handleResponse($response, Users::class);
100
    }
101
102
    /**
103
     * Returns a collection of users.
104
     *
105
     * @param array $params The listing params, 'page', 'per_page', 'in_channel', 'in_team', 'not_in_channel'
106
     *
107
     * @return Users|ResponseInterface
108 9
     */
109
    public function getUsers(array $params = [])
110 9
    {
111
        $response = $this->httpGet('/users', $params);
112 9
113
        return $this->handleResponse($response, Users::class);
114
    }
115
116
    /**
117
     * Returns a user given its email.
118
     *
119
     *
120
     * @return User|ResponseInterface
121 9
     */
122
    public function getUserByEmail(string $email)
123 9
    {
124 1
        if (empty($email)) {
125
            throw new InvalidArgumentException('Email can not be empty');
126
        }
127 8
128
        $response = $this->httpGet(sprintf('/users/email/%s', $email));
129 8
130
        return $this->handleResponse($response, User::class);
131
    }
132
133
    /**
134
     * Returns a collection of users matching the given usernames.
135
     *
136
     * @param string[] $userNames
137
     *
138
     * @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1usernames%2Fpost
139
     *
140
     * @return Users|ResponseInterface
141 9
     */
142
    public function getUsersByUsernames(array $userNames)
143 9
    {
144 1
        if (empty($userNames)) {
145
            throw new InvalidArgumentException('Usernames can not be empty');
146
        }
147 8
148
        $response = $this->httpPost('/users/usernames', $userNames);
149 8
150
        return $this->handleResponse($response, Users::class);
151
    }
152
153
    /**
154
     * Update user active or inactive status.
155
     *
156
     * @param string $userId       The user ID
157
     * @param bool   $activeStatus Use `true` to set the user active, `false` for inactive
158
     *
159
     * @return Status|ResponseInterface
160 9
     */
161
    public function setUserActive(string $userId, bool $activeStatus)
162 9
    {
163 1
        if (empty($userId)) {
164
            throw new InvalidArgumentException('User ID can not be empty');
165
        }
166 8
167 8
        $response = $this->httpPut(sprintf('/users/%s/active', $userId), [
168
            'active' => $activeStatus,
169
        ]);
170 8
171
        return $this->handleResponse($response, Status::class);
172
    }
173
174
    /**
175
     * Deactivates the user by archiving its user object.
176
     *
177
     * @param string $userId The user GUID
178
     *
179
     * @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1%7Buser_id%7D%2Fdelete
180
     *
181
     * @return Status|ResponseInterface
182 9
     */
183
    public function deactivateUser(string $userId)
184 9
    {
185 1
        if (empty($userId)) {
186
            throw new InvalidArgumentException('User ID can not be empty');
187
        }
188 8
189
        $response = $this->httpDelete(sprintf('/users/%s', $userId));
190 8
191
        return $this->handleResponse($response, Status::class);
192
    }
193
194
    /**
195
     * Update a user's system-level roles.
196
     * Valid user roles are "system_user", "system_admin" or both of them.
197
     * Overwrites any previously assigned system-level roles.
198
     *
199
     * @param string $userId The user GUID
200
     * @param string $roles  Space-delimited system roles to assign to the user
201
     *
202
     * @return Status|ResponseInterface
203 9
     */
204
    public function updateUserRoles(string $userId, string $roles)
205 9
    {
206 1
        if (empty($userId)) {
207
            throw new InvalidArgumentException('User ID can not be empty');
208
        }
209 8
210 8
        $response = $this->httpPut(sprintf('/users/%s/roles', $userId), [
211
            'roles' => $roles,
212
        ]);
213 8
214
        return $this->handleResponse($response, Status::class);
215
    }
216
217
    /**
218
     * Create a user. Required parameters: 'username', 'email' and 'password'.
219
     *
220
     * @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users%2Fpost
221
     *
222
     * @return User|ResponseInterface
223
     */
224
    public function createUser(array $params)
225 8
    {
226
        $response = $this->httpPost('/users', $params);
227 8
228
        return $this->handleResponse($response, User::class);
229 8
    }
230
231
    /**
232
     * Patch a user.
233
     *
234
     * @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1%7Buser_id%7D~1patch%2Fput
235
     *
236
     * @return User|ResponseInterface
237
     */
238
    public function patchUser(string $userId, array $params)
239
    {
240
        if (empty($userId)) {
241 9
            throw new InvalidArgumentException('UserId can not be empty');
242
        }
243 9
244 1
        $response = $this->httpPut(sprintf('/users/%s/patch', $userId), $params);
245
246
        return $this->handleResponse($response, User::class);
247 8
    }
248
249 8
    /**
250
     * Update a user.
251
     *
252
     * @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1%7Buser_id%7D%2Fput
253
     *
254
     * @return User|ResponseInterface
255
     */
256
    public function updateUser(string $userId, array $params)
257
    {
258
        if (empty($userId)) {
259
            throw new InvalidArgumentException('UserId can not be empty');
260
        }
261 9
262
        $response = $this->httpPut(sprintf('/users/%s', $userId), $params);
263 9
264 1
        return $this->handleResponse($response, User::class);
265
    }
266
267 8
    /**
268
     * Update a user's password. New password must meet password policy set by server configuration.
269 8
     *
270
     * @param string $currentPassword The current password for the user
271
     * @param string $newPassword     The new password for the user
272
     *
273
     * @return Status|ResponseInterface
274
     */
275
    public function updateUserPassword(string $userId, string $currentPassword, string $newPassword)
276
    {
277
        if (empty($userId)) {
278
            throw new InvalidArgumentException('UserId can not be empty');
279
        }
280 11
        if (empty($currentPassword) || empty($newPassword)) {
281
            throw new InvalidArgumentException('The current password and the new password can not be empty');
282 11
        }
283 1
284
        $response = $this->httpPut(sprintf('/users/%s/password', $userId), [
285 10
            'current_password' => $currentPassword,
286 2
            'new_password' => $newPassword,
287
        ]);
288
289 8
        return $this->handleResponse($response, Status::class);
290 8
    }
291 8
292
    /**
293
     * Returns an user by its username, use "me" to get the current logged in user.
294 8
     *
295
     * @return User|ResponseInterface
296
     */
297
    public function getUserByUsername(string $username)
298
    {
299
        if (empty($username)) {
300
            throw new InvalidArgumentException('Username can not be empty');
301
        }
302 9
303
        $response = $this->httpGet(sprintf('/users/username/%s', $username));
304 9
305 1
        return $this->handleResponse($response, User::class);
306
    }
307
308 8
    /**
309
     * Get the user status by its ID.
310 8
     *
311
     * @return UserStatus|ResponseInterface
312
     */
313
    public function getUserStatus(string $userId)
314
    {
315
        if (empty($userId)) {
316
            throw new InvalidArgumentException('UserId can not be empty');
317
        }
318
319
        $response = $this->httpGet(sprintf('/users/%s/status', $userId));
320
        var_dump($response->getBody()->getContents());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($response->getBody()->getContents()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
321
322
        return $this->handleResponse($response, UserStatus::class);
323
    }
324
}
325