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
Push — master ( 511799...093d46 )
by Ema
02:48
created

UsersApi::getUserById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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