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