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\Post\Post; |
9
|
|
|
use Pnz\MattermostClient\Model\Status; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
final class PostsApi extends HttpApi |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Create a post. Required parameters: 'channel_id', 'message'. |
16
|
|
|
* |
17
|
|
|
* @param array $params |
18
|
|
|
* |
19
|
|
|
* @return Post|ResponseInterface |
20
|
|
|
*/ |
21
|
8 |
|
public function createPost(array $params) |
22
|
|
|
{ |
23
|
8 |
|
$response = $this->httpPost('/posts', $params); |
24
|
|
|
|
25
|
8 |
|
return $this->handleResponse($response, Post::class); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Partially update a post by providing only the fields you want to update. |
30
|
|
|
* Omitted fields will not be updated. |
31
|
|
|
* |
32
|
|
|
* @see https://api.mattermost.com/v4/#tag/posts%2Fpaths%2F~1posts~1%7Bpost_id%7D~1patch%2Fput |
33
|
|
|
* |
34
|
|
|
* @param string $postId |
35
|
|
|
* @param array $params |
36
|
|
|
* |
37
|
|
|
* @return Post|ResponseInterface |
38
|
|
|
*/ |
39
|
9 |
View Code Duplication |
public function patchPost(string $postId, array $params) |
|
|
|
|
40
|
|
|
{ |
41
|
9 |
|
if (empty($postId)) { |
42
|
1 |
|
throw new InvalidArgumentException('PostId can not be empty'); |
43
|
|
|
} |
44
|
|
|
|
45
|
8 |
|
$response = $this->httpPut(sprintf('/posts/%s/patch', $postId), $params); |
46
|
|
|
|
47
|
8 |
|
return $this->handleResponse($response, Post::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Update a post. |
52
|
|
|
* |
53
|
|
|
* @see https://api.mattermost.com/v4/#tag/posts%2Fpaths%2F~1posts~1%7Bpost_id%7D%2Fput |
54
|
|
|
* |
55
|
|
|
* @param string $postId ID of the post to update |
56
|
|
|
* @param array $params |
57
|
|
|
* |
58
|
|
|
* @return Post|ResponseInterface |
59
|
|
|
*/ |
60
|
9 |
View Code Duplication |
public function updatePost(string $postId, array $params) |
|
|
|
|
61
|
|
|
{ |
62
|
9 |
|
if (empty($postId)) { |
63
|
1 |
|
throw new InvalidArgumentException('PostId can not be empty'); |
64
|
|
|
} |
65
|
|
|
|
66
|
8 |
|
$response = $this->httpPut(sprintf('/posts/%s', $postId), $params); |
67
|
|
|
|
68
|
8 |
|
return $this->handleResponse($response, Post::class); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get a single post. |
73
|
|
|
* |
74
|
|
|
* @param string $postId ID of the post to get |
75
|
|
|
* |
76
|
|
|
* @return Post|ResponseInterface |
77
|
|
|
*/ |
78
|
9 |
|
public function getPost(string $postId) |
79
|
|
|
{ |
80
|
9 |
|
if (empty($postId)) { |
81
|
1 |
|
throw new InvalidArgumentException('PostId can not be empty'); |
82
|
|
|
} |
83
|
|
|
|
84
|
8 |
|
$response = $this->httpGet(sprintf('/posts/%s', $postId)); |
85
|
|
|
|
86
|
8 |
|
return $this->handleResponse($response, Post::class); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Pin a post to a channel it is in based from the provided post id string. |
91
|
|
|
* |
92
|
|
|
* @see https://api.mattermost.com/v4/#tag/posts%2Fpaths%2F~1posts~1%7Bpost_id%7D~1pin%2Fpost |
93
|
|
|
* |
94
|
|
|
* @param string $postId Post GUID |
95
|
|
|
* |
96
|
|
|
* @return Status|ResponseInterface |
97
|
|
|
*/ |
98
|
9 |
View Code Duplication |
public function pinPost($postId) |
|
|
|
|
99
|
|
|
{ |
100
|
9 |
|
if (empty($postId)) { |
101
|
1 |
|
throw new InvalidArgumentException('PostId can not be empty'); |
102
|
|
|
} |
103
|
|
|
|
104
|
8 |
|
$response = $this->httpPost(sprintf('/posts/%s/pin', $postId)); |
105
|
|
|
|
106
|
8 |
|
return $this->handleResponse($response, Status::class); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Unpin a post to a channel it is in based from the provided post id string. |
111
|
|
|
* |
112
|
|
|
* @see https://api.mattermost.com/v4/#tag/posts%2Fpaths%2F~1posts~1%7Bpost_id%7D~1unpin%2Fpost |
113
|
|
|
* |
114
|
|
|
* @param string $postId Post GUID |
115
|
|
|
* |
116
|
|
|
* @return Status|ResponseInterface |
117
|
|
|
*/ |
118
|
9 |
View Code Duplication |
public function unpinPost($postId) |
|
|
|
|
119
|
|
|
{ |
120
|
9 |
|
if (empty($postId)) { |
121
|
1 |
|
throw new InvalidArgumentException('PostId can not be empty'); |
122
|
|
|
} |
123
|
|
|
|
124
|
8 |
|
$response = $this->httpPost(sprintf('/posts/%s/unpin', $postId)); |
125
|
|
|
|
126
|
8 |
|
return $this->handleResponse($response, Status::class); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Soft deletes a post, by marking the post as deleted in the database. Soft deleted posts will not be returned in post queries. |
131
|
|
|
* |
132
|
|
|
* @see https://api.mattermost.com/v4/#tag/posts%2Fpaths%2F~1posts~1%7Bpost_id%7D%2Fdelete |
133
|
|
|
* |
134
|
|
|
* @param string $postId ID of the post to delete |
135
|
|
|
* |
136
|
|
|
* @return Status|ResponseInterface |
137
|
|
|
*/ |
138
|
9 |
|
public function deletePost(string $postId) |
139
|
|
|
{ |
140
|
9 |
|
if (empty($postId)) { |
141
|
1 |
|
throw new InvalidArgumentException('PostId can not be empty'); |
142
|
|
|
} |
143
|
|
|
|
144
|
8 |
|
$response = $this->httpDelete(sprintf('/posts/%s', $postId)); |
145
|
|
|
|
146
|
8 |
|
return $this->handleResponse($response, Status::class); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
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.