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 ( 7421d9...99c645 )
by Ema
07:45
created

ChannelsApi::createDirectChannel()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

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