1
|
|
|
<?php |
2
|
|
|
namespace FlexyProject\GitHub\Receiver\Organizations; |
3
|
|
|
|
4
|
|
|
use FlexyProject\GitHub\AbstractApi; |
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Members API class gives you access to the available organization's members. |
9
|
|
|
* |
10
|
|
|
* @package FlexyProject\GitHub\Receiver\Organizations |
11
|
|
|
*/ |
12
|
|
|
class Members extends AbstractOrganizations |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Members list |
17
|
|
|
* |
18
|
|
|
* @link https://developer.github.com/v3/orgs/members/#members-list |
19
|
|
|
* |
20
|
|
|
* @param string $org |
21
|
|
|
* @param string $filter |
22
|
|
|
* @param string $role |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public function listMembers(string $org, string $filter = AbstractApi::FILTER_ALL, string $role = 'admin'): array |
27
|
|
|
{ |
28
|
|
|
$this->getApi()->setAccept('application/vnd.github.moondragon+json'); |
29
|
|
|
|
30
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/members?:args', $org, |
31
|
|
|
http_build_query(['filter' => $filter, 'role' => $role]))); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Check membership |
36
|
|
|
* |
37
|
|
|
* @link https://developer.github.com/v3/orgs/members/#check-membership |
38
|
|
|
* |
39
|
|
|
* @param string $org |
40
|
|
|
* @param string $username |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function checkMembership(string $org, string $username): bool |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$this->getApi()->request($this->getApi()->sprintf('/orgs/:org/members/:username', $org, $username)); |
48
|
|
|
|
49
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Remove a member |
58
|
|
|
* |
59
|
|
|
* @link https://developer.github.com/v3/orgs/members/#remove-a-member |
60
|
|
|
* |
61
|
|
|
* @param string $org |
62
|
|
|
* @param string $username |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
* @throws \Exception |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function removeMember(string $org, string $username): bool |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$this->getApi()->request($this->getApi()->sprintf('/orgs/:org/members/:username', $org, $username), |
70
|
|
|
Request::METHOD_DELETE); |
71
|
|
|
|
72
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Public members list |
81
|
|
|
* |
82
|
|
|
* @link https://developer.github.com/v3/orgs/members/#public-members-list |
83
|
|
|
* |
84
|
|
|
* @param string $org |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
* @throws \Exception |
88
|
|
|
*/ |
89
|
|
|
public function listPublicMembers(string $org): array |
90
|
|
|
{ |
91
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/public_members', $org)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Check public membership |
96
|
|
|
* |
97
|
|
|
* @link https://developer.github.com/v3/orgs/members/#check-public-membership |
98
|
|
|
* |
99
|
|
|
* @param string $org |
100
|
|
|
* @param string $username |
101
|
|
|
* |
102
|
|
|
* @return bool |
103
|
|
|
* @throws \Exception |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function checkPublicMembership(string $org, string $username): bool |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$this->getApi()->request($this->getApi()->sprintf('/orgs/:org/public_members/:username', $org, $username)); |
108
|
|
|
|
109
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Publicize a user’s membership |
118
|
|
|
* |
119
|
|
|
* @link https://developer.github.com/v3/orgs/members/#publicize-a-users-membership |
120
|
|
|
* |
121
|
|
|
* @param string $org |
122
|
|
|
* @param string $username |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
* @throws \Exception |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public function publicizeUsersMembership(string $org, string $username): bool |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$this->getApi()->request($this->getApi()->sprintf('/orgs/:org/public_members/:username', $org, $username), |
130
|
|
|
Request::METHOD_PUT); |
131
|
|
|
|
132
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Conceal a user’s membership |
141
|
|
|
* |
142
|
|
|
* @link https://developer.github.com/v3/orgs/members/#conceal-a-users-membership |
143
|
|
|
* |
144
|
|
|
* @param string $org |
145
|
|
|
* @param string $username |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
* @throws \Exception |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
public function concealUsersMembership(string $org, string $username): bool |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$this->getApi()->request($this->getApi()->sprintf('/orgs/:org/public_members/:username', $org, $username), |
153
|
|
|
Request::METHOD_DELETE); |
154
|
|
|
|
155
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
156
|
|
|
return true; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get organization membership |
164
|
|
|
* |
165
|
|
|
* @link https://developer.github.com/v3/orgs/members/#get-organization-membership |
166
|
|
|
* |
167
|
|
|
* @param string $org |
168
|
|
|
* @param string $username |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
* @throws \Exception |
172
|
|
|
*/ |
173
|
|
|
public function getOrganizationMembership(string $org, string $username): array |
174
|
|
|
{ |
175
|
|
|
$this->getApi()->setAccept('application/vnd.github.moondragon+json'); |
176
|
|
|
|
177
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/memberships/:username', $org, $username)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Add or update organization membership |
182
|
|
|
* |
183
|
|
|
* @link https://developer.github.com/v3/orgs/members/#add-or-update-organization-membership |
184
|
|
|
* |
185
|
|
|
* @param string $org |
186
|
|
|
* @param string $username |
187
|
|
|
* @param string $role |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
* @throws \Exception |
191
|
|
|
*/ |
192
|
|
|
public function addUpdateOrganizationMembership(string $org, string $username, string $role): array |
193
|
|
|
{ |
194
|
|
|
$this->getApi()->setAccept('application/vnd.github.moondragon+json'); |
195
|
|
|
|
196
|
|
|
return $this->getApi()->request($this->getApi() |
197
|
|
|
->sprintf('/orgs/:org/memberships/:username?:args', $org, $username, |
198
|
|
|
http_build_query(['role' => $role])), Request::METHOD_PUT); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Remove organization membership |
203
|
|
|
* |
204
|
|
|
* @link https://developer.github.com/v3/orgs/members/#remove-organization-membership |
205
|
|
|
* |
206
|
|
|
* @param string $org |
207
|
|
|
* @param string $username |
208
|
|
|
* |
209
|
|
|
* @return bool |
210
|
|
|
* @throws \Exception |
211
|
|
|
*/ |
212
|
|
View Code Duplication |
public function removeOrganizationMembership(string $org, string $username): bool |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
$this->getApi()->setAccept('application/vnd.github.moondragon+json'); |
215
|
|
|
|
216
|
|
|
$this->getApi()->request($this->getApi()->sprintf('/orgs/:org/memberships/:username', $org, $username), |
217
|
|
|
Request::METHOD_DELETE); |
218
|
|
|
|
219
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
220
|
|
|
return true; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* List your organization memberships |
228
|
|
|
* |
229
|
|
|
* @link https://developer.github.com/v3/orgs/members/#list-your-organization-memberships |
230
|
|
|
* |
231
|
|
|
* @param string|null $state |
232
|
|
|
* |
233
|
|
|
* @return array |
234
|
|
|
* @throws \Exception |
235
|
|
|
*/ |
236
|
|
|
public function listYourOrganizationMembership(string $state = null): array |
237
|
|
|
{ |
238
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/user/memberships/orgs?:args', |
239
|
|
|
http_build_query(['state' => $state]))); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Get your organization membership |
244
|
|
|
* |
245
|
|
|
* @link https://developer.github.com/v3/orgs/members/#get-your-organization-membership |
246
|
|
|
* |
247
|
|
|
* @param string $org |
248
|
|
|
* |
249
|
|
|
* @return array |
250
|
|
|
* @throws \Exception |
251
|
|
|
*/ |
252
|
|
|
public function getYourOrganizationMembership(string $org): array |
253
|
|
|
{ |
254
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/user/memberships/orgs/:org', $org)); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Edit your organization membership |
259
|
|
|
* |
260
|
|
|
* @link https://developer.github.com/v3/orgs/members/#edit-your-organization-membership |
261
|
|
|
* |
262
|
|
|
* @param string $org |
263
|
|
|
* @param string $state |
264
|
|
|
* |
265
|
|
|
* @return array |
266
|
|
|
* @throws \Exception |
267
|
|
|
*/ |
268
|
|
|
public function editYourOrganizationMembership(string $org, string $state = AbstractApi::STATE_ACTIVE): array |
269
|
|
|
{ |
270
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/user/memberships/orgs/:org', $org), |
271
|
|
|
Request::METHOD_PATCH, [ |
272
|
|
|
'state' => $state |
273
|
|
|
]); |
274
|
|
|
} |
275
|
|
|
} |
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.