|
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 |
|
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
|
|
|
* Remove a team member. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $teamId The team ID |
|
162
|
|
|
* @param string $userId The user ID |
|
163
|
|
|
* |
|
164
|
|
|
* https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1members~1%7Buser_id%7D%2Fdelete |
|
165
|
|
|
* |
|
166
|
|
|
* @return Status|ResponseInterface |
|
167
|
|
|
*/ |
|
168
|
11 |
|
public function removeTeamMember(string $teamId, string $userId) |
|
169
|
|
|
{ |
|
170
|
11 |
|
if (empty($teamId) || empty($userId)) { |
|
171
|
3 |
|
throw new InvalidArgumentException('TeamID and UserId can not be empty'); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
8 |
|
$response = $this->httpDelete(sprintf('/teams/%s/members/%s', $teamId, $userId)); |
|
175
|
|
|
|
|
176
|
8 |
|
return $this->handleResponse($response, Status::class); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Return the list of public channels in the given team. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $teamId The team ID |
|
183
|
|
|
* @param array $params The listing params, 'page', 'per_page' |
|
184
|
|
|
* |
|
185
|
|
|
* @return Channels|ResponseInterface |
|
186
|
|
|
*/ |
|
187
|
9 |
|
public function getTeamPublicChannels(string $teamId, array $params = []) |
|
188
|
|
|
{ |
|
189
|
9 |
|
if (empty($teamId)) { |
|
190
|
1 |
|
throw new InvalidArgumentException('TeamID can not be empty'); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
8 |
|
$response = $this->httpGet(sprintf('/teams/%s/channels', $teamId), $params); |
|
194
|
|
|
|
|
195
|
8 |
|
return $this->handleResponse($response, Channels::class); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Retrieve the team statistics. |
|
200
|
|
|
* |
|
201
|
|
|
* @param string $teamId The Team ID |
|
202
|
|
|
* |
|
203
|
|
|
* @return TeamStats|ResponseInterface |
|
204
|
|
|
*/ |
|
205
|
9 |
|
public function getTeamStats($teamId) |
|
206
|
|
|
{ |
|
207
|
9 |
|
if (empty($teamId)) { |
|
208
|
1 |
|
throw new InvalidArgumentException('TeamID can not be empty'); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
8 |
|
$response = $this->httpGet(sprintf('/teams/%s/stats', $teamId)); |
|
212
|
|
|
|
|
213
|
8 |
|
return $this->handleResponse($response, TeamStats::class); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Patch a team. |
|
218
|
|
|
* |
|
219
|
|
|
* @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D~1patch%2Fput |
|
220
|
|
|
* |
|
221
|
|
|
* @param string $teamId |
|
222
|
|
|
* @param array $params |
|
223
|
|
|
* |
|
224
|
|
|
* @return Team|ResponseInterface |
|
225
|
|
|
*/ |
|
226
|
9 |
|
public function patchTeam(string $teamId, array $params) |
|
227
|
|
|
{ |
|
228
|
9 |
|
if (empty($teamId)) { |
|
229
|
1 |
|
throw new InvalidArgumentException('TeamId can not be empty'); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
8 |
|
$response = $this->httpPut(sprintf('/teams/%s/patch', $teamId), $params); |
|
233
|
|
|
|
|
234
|
8 |
|
return $this->handleResponse($response, Team::class); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Update a team. |
|
239
|
|
|
* |
|
240
|
|
|
* @see https://api.mattermost.com/v4/#tag/teams%2Fpaths%2F~1teams~1%7Bteam_id%7D%2Fput |
|
241
|
|
|
* |
|
242
|
|
|
* @param string $teamId |
|
243
|
|
|
* @param array $params, required paramaters: display_name, description, company_name, allowed_domains, invite_id, allow_open_invite |
|
|
|
|
|
|
244
|
|
|
* |
|
245
|
|
|
* @return Team|ResponseInterface |
|
246
|
|
|
*/ |
|
247
|
9 |
|
public function updateTeam(string $teamId, array $params) |
|
248
|
|
|
{ |
|
249
|
9 |
|
if (empty($teamId)) { |
|
250
|
1 |
|
throw new InvalidArgumentException('TeamId can not be empty'); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
8 |
|
$response = $this->httpPut(sprintf('/teams/%s', $teamId), $params); |
|
254
|
|
|
|
|
255
|
8 |
|
return $this->handleResponse($response, Team::class); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: