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\Channel\Channels; |
9
|
|
|
use Pnz\MattermostClient\Model\Status; |
10
|
|
|
use Pnz\MattermostClient\Model\Team\Team; |
11
|
|
|
use Pnz\MattermostClient\Model\Team\TeamMember; |
12
|
|
|
use Pnz\MattermostClient\Model\Team\TeamMembers; |
13
|
|
|
use Pnz\MattermostClient\Model\Team\Teams; |
14
|
|
|
use Pnz\MattermostClient\Model\Team\TeamStats; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
final class TeamsApi extends HttpApi |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Returns an team by its ID. |
21
|
|
|
* |
22
|
|
|
* @param string $id |
23
|
|
|
* |
24
|
|
|
* @return Team|ResponseInterface |
25
|
|
|
*/ |
26
|
9 |
View Code Duplication |
public function getTeamById($id) |
|
|
|
|
27
|
|
|
{ |
28
|
9 |
|
if (empty($id)) { |
29
|
1 |
|
throw new InvalidArgumentException('Id can not be empty'); |
30
|
|
|
} |
31
|
|
|
|
32
|
8 |
|
$response = $this->httpGet(sprintf('/teams/%s', $id)); |
33
|
|
|
|
34
|
8 |
|
return $this->handleResponse($response, Team::class); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a team. Required parameters: 'name', 'display_name' and 'type'. |
39
|
|
|
* |
40
|
|
|
* @param array $params |
41
|
|
|
* |
42
|
|
|
* @return Team|ResponseInterface |
43
|
|
|
*/ |
44
|
8 |
|
public function createTeam(array $params) |
45
|
|
|
{ |
46
|
8 |
|
$response = $this->httpPost('/teams', |
47
|
8 |
|
$params |
48
|
|
|
); |
49
|
|
|
|
50
|
8 |
|
return $this->handleResponse($response, Team::class); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns a collection of teams. |
55
|
|
|
* |
56
|
|
|
* @param array $params The listing params, 'page', 'per_page' |
57
|
|
|
* |
58
|
|
|
* @return Teams|ResponseInterface |
59
|
|
|
*/ |
60
|
8 |
|
public function getTeams(array $params = []) |
61
|
|
|
{ |
62
|
8 |
|
$response = $this->httpGet('/teams', $params); |
63
|
|
|
|
64
|
8 |
|
return $this->handleResponse($response, Teams::class); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Delete a team softly and put in archived only. |
69
|
|
|
* |
70
|
|
|
* @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D%2Fdelete |
71
|
|
|
* |
72
|
|
|
* @param string $teamId Team GUID |
73
|
|
|
* @param bool $permanent permanently delete the team, to be used for complience reasons only |
74
|
|
|
* |
75
|
|
|
* @return Status|ResponseInterface |
76
|
|
|
*/ |
77
|
10 |
|
public function deleteTeam(string $teamId, bool $permanent = false) |
78
|
|
|
{ |
79
|
10 |
|
if (empty($teamId)) { |
80
|
1 |
|
throw new InvalidArgumentException('User ID can not be empty'); |
81
|
|
|
} |
82
|
|
|
|
83
|
9 |
|
$pathParams = $permanent ? ['permanent' => true] : []; |
84
|
|
|
|
85
|
9 |
|
$response = $this->httpDelete(sprintf('/teams/%s', $teamId), [], $pathParams); |
86
|
|
|
|
87
|
9 |
|
return $this->handleResponse($response, Status::class); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns a team given its name. |
92
|
|
|
* |
93
|
|
|
* @param string $name |
94
|
|
|
* |
95
|
|
|
* @return Team|ResponseInterface |
96
|
|
|
*/ |
97
|
9 |
|
public function getTeamByName($name) |
98
|
|
|
{ |
99
|
9 |
|
if (empty($name)) { |
100
|
1 |
|
throw new InvalidArgumentException('TeamName can not be empty'); |
101
|
|
|
} |
102
|
|
|
|
103
|
8 |
|
$response = $this->httpGet(sprintf('/teams/name/%s', $name)); |
104
|
|
|
|
105
|
8 |
|
return $this->handleResponse($response, Team::class); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Add a user to a team, with specific roles. |
110
|
|
|
* |
111
|
|
|
* @param string $teamId |
112
|
|
|
* @param string $userId |
113
|
|
|
* @param string $roles |
114
|
|
|
* @param array $pathParams |
115
|
|
|
* |
116
|
|
|
* @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1members%2Fpost |
117
|
|
|
* |
118
|
|
|
* @return TeamMember|ResponseInterface |
119
|
|
|
*/ |
120
|
11 |
View Code Duplication |
public function addTeamMember($teamId, $userId, $roles = '', $pathParams = []) |
|
|
|
|
121
|
|
|
{ |
122
|
11 |
|
if (empty($teamId) || empty($userId)) { |
123
|
3 |
|
throw new InvalidArgumentException('Team ID or user ID can not be empty'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$body = [ |
127
|
8 |
|
'team_id' => $teamId, |
128
|
8 |
|
'user_id' => $userId, |
129
|
8 |
|
'roles' => $roles, |
130
|
|
|
]; |
131
|
|
|
|
132
|
8 |
|
$response = $this->httpPost(sprintf('/teams/%s/members', $teamId), $body, $pathParams); |
133
|
|
|
|
134
|
8 |
|
return $this->handleResponse($response, TeamMember::class); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Return the team members. |
139
|
|
|
* |
140
|
|
|
* @param string $teamId The Team ID |
141
|
|
|
* @param array $params The listing params, 'page', 'per_page' |
142
|
|
|
* |
143
|
|
|
* @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1members%2Fget |
144
|
|
|
* |
145
|
|
|
* @return TeamMembers |
146
|
|
|
*/ |
147
|
9 |
|
public function getTeamMembers(string $teamId, array $params = []) |
148
|
|
|
{ |
149
|
9 |
|
if (empty($teamId)) { |
150
|
1 |
|
throw new InvalidArgumentException('TeamID can not be empty'); |
151
|
|
|
} |
152
|
|
|
|
153
|
8 |
|
$response = $this->httpGet(sprintf('/teams/%s/members', $teamId), $params); |
154
|
|
|
|
155
|
8 |
|
return $this->handleResponse($response, TeamMembers::class); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get a team member from the system given a Team and User IDs. |
160
|
|
|
* |
161
|
|
|
* @param string $teamId The Team GUID |
162
|
|
|
* @param string $userId The User GUID |
163
|
|
|
* |
164
|
|
|
* @return TeamMember |
165
|
|
|
*/ |
166
|
11 |
View Code Duplication |
public function getTeamMember(string $teamId, string $userId) |
|
|
|
|
167
|
|
|
{ |
168
|
11 |
|
if (empty($teamId) || empty($userId)) { |
169
|
3 |
|
throw new InvalidArgumentException('TeamID and UserId can not be empty'); |
170
|
|
|
} |
171
|
|
|
|
172
|
8 |
|
$response = $this->httpGet(sprintf('/teams/%s/members/%s', $teamId, $userId)); |
173
|
|
|
|
174
|
8 |
|
return $this->handleResponse($response, TeamMember::class); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Remove a team member. |
179
|
|
|
* |
180
|
|
|
* @param string $teamId The team ID |
181
|
|
|
* @param string $userId The user ID |
182
|
|
|
* |
183
|
|
|
* https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1members~1%7Buser_id%7D%2Fdelete |
184
|
|
|
* |
185
|
|
|
* @return Status|ResponseInterface |
186
|
|
|
*/ |
187
|
11 |
|
public function removeTeamMember(string $teamId, string $userId) |
188
|
|
|
{ |
189
|
11 |
|
if (empty($teamId) || empty($userId)) { |
190
|
3 |
|
throw new InvalidArgumentException('TeamID and UserId can not be empty'); |
191
|
|
|
} |
192
|
|
|
|
193
|
8 |
|
$response = $this->httpDelete(sprintf('/teams/%s/members/%s', $teamId, $userId)); |
194
|
|
|
|
195
|
8 |
|
return $this->handleResponse($response, Status::class); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Return the list of public channels in the given team. |
200
|
|
|
* |
201
|
|
|
* @param string $teamId The team ID |
202
|
|
|
* @param array $params The listing params, 'page', 'per_page' |
203
|
|
|
* |
204
|
|
|
* @return Channels|ResponseInterface |
205
|
|
|
*/ |
206
|
9 |
|
public function getTeamPublicChannels(string $teamId, array $params = []) |
207
|
|
|
{ |
208
|
9 |
|
if (empty($teamId)) { |
209
|
1 |
|
throw new InvalidArgumentException('TeamID can not be empty'); |
210
|
|
|
} |
211
|
|
|
|
212
|
8 |
|
$response = $this->httpGet(sprintf('/teams/%s/channels', $teamId), $params); |
213
|
|
|
|
214
|
8 |
|
return $this->handleResponse($response, Channels::class); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Retrieve the team statistics. |
219
|
|
|
* |
220
|
|
|
* @param string $teamId The Team ID |
221
|
|
|
* |
222
|
|
|
* @return TeamStats|ResponseInterface |
223
|
|
|
*/ |
224
|
9 |
|
public function getTeamStats($teamId) |
225
|
|
|
{ |
226
|
9 |
|
if (empty($teamId)) { |
227
|
1 |
|
throw new InvalidArgumentException('TeamID can not be empty'); |
228
|
|
|
} |
229
|
|
|
|
230
|
8 |
|
$response = $this->httpGet(sprintf('/teams/%s/stats', $teamId)); |
231
|
|
|
|
232
|
8 |
|
return $this->handleResponse($response, TeamStats::class); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Patch a team. |
237
|
|
|
* |
238
|
|
|
* @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1patch%2Fput |
239
|
|
|
* |
240
|
|
|
* @param string $teamId |
241
|
|
|
* @param array $params |
242
|
|
|
* |
243
|
|
|
* @return Team|ResponseInterface |
244
|
|
|
*/ |
245
|
9 |
|
public function patchTeam(string $teamId, array $params) |
246
|
|
|
{ |
247
|
9 |
|
if (empty($teamId)) { |
248
|
1 |
|
throw new InvalidArgumentException('TeamId can not be empty'); |
249
|
|
|
} |
250
|
|
|
|
251
|
8 |
|
$response = $this->httpPut(sprintf('/teams/%s/patch', $teamId), $params); |
252
|
|
|
|
253
|
8 |
|
return $this->handleResponse($response, Team::class); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Update a team. |
258
|
|
|
* |
259
|
|
|
* @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D%2Fput |
260
|
|
|
* |
261
|
|
|
* @param string $teamId |
262
|
|
|
* @param array $params, required paramaters: display_name, description, company_name, allowed_domains, invite_id, allow_open_invite |
|
|
|
|
263
|
|
|
* |
264
|
|
|
* @return Team|ResponseInterface |
265
|
|
|
*/ |
266
|
9 |
|
public function updateTeam(string $teamId, array $params) |
267
|
|
|
{ |
268
|
9 |
|
if (empty($teamId)) { |
269
|
1 |
|
throw new InvalidArgumentException('TeamId can not be empty'); |
270
|
|
|
} |
271
|
|
|
|
272
|
8 |
|
$response = $this->httpPut(sprintf('/teams/%s', $teamId), $params); |
273
|
|
|
|
274
|
8 |
|
return $this->handleResponse($response, Team::class); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
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.