Completed
Push — master ( 6a7fa3...3317e8 )
by Ryota
11s
created

ApiMethods::watchers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Polidog\Esa;
4
5
use GuzzleHttp\ClientInterface;
6
7
/**
8
 * Class ApiMethods.
9
 */
10
class ApiMethods
11
{
12
    private $httpClient;
13
    private $currentTeam;
14
15
    /**
16
     * @param ClientInterface $client
17
     * @param $currentTeam
18
     */
19
    public function __construct(ClientInterface $client, $currentTeam)
20
    {
21
        $this->httpClient = $client;
22
        $this->currentTeam = $currentTeam;
23
    }
24
25
    /**
26
     * @param array $params
27
     * @param null  $headers
28
     */
29
    public function user(array $params = [])
30
    {
31
        return $this->httpClient->request('GET', 'user', [
32
            'query' => $params,
33
        ]);
34
    }
35
36
    /**
37
     * @return \Psr\Http\Message\ResponseInterface
38
     */
39
    public function teams()
40
    {
41
        return $this->httpClient->request('GET', 'teams');
42
    }
43
44
    /**
45
     * @param null $name
46
     *
47
     * @return \Psr\Http\Message\ResponseInterface
48
     */
49
    public function team($name = null)
50
    {
51
        return $this->httpClient->request('GET', "teams/{$name}");
52
    }
53
54
    /**
55
     * @return \Psr\Http\Message\ResponseInterface
56
     */
57
    public function stats()
58
    {
59
        return $this->httpClient->request('GET', "teams/{$this->currentTeam}/stats");
60
    }
61
62
    /**
63
     * @return \Psr\Http\Message\ResponseInterface
64
     */
65
    public function members()
66
    {
67
        return $this->httpClient->request('GET', "teams/{$this->currentTeam}/members");
68
    }
69
70
    /**
71
     * @param array $params
72
     *
73
     * @return \Psr\Http\Message\ResponseInterface
74
     */
75
    public function posts(array $params = [])
76
    {
77
        return $this->httpClient->request('GET', "teams/{$this->currentTeam}/posts", [
78
            'query' => $params,
79
        ]);
80
    }
81
82
    /**
83
     * @param $number
84
     *
85
     * @return \Psr\Http\Message\ResponseInterface
86
     */
87
    public function post($number)
88
    {
89
        return $this->httpClient->request('GET', "teams/{$this->currentTeam}/posts/{$number}");
90
    }
91
92
    /**
93
     * @param $data
94
     *
95
     * @return \Psr\Http\Message\ResponseInterface
96
     */
97
    public function createPost($data)
98
    {
99
        return $this->httpClient->request('POST', "teams/{$this->currentTeam}/posts", [
100
            'json' => [
101
                'post' => $data,
102
            ],
103
        ]);
104
    }
105
106
    /**
107
     * @param $number
108
     * @param $data
109
     *
110
     * @return \Psr\Http\Message\ResponseInterface
111
     */
112
    public function updatePost($number, $data)
113
    {
114
        return $this->httpClient->request('PATCH', "teams/{$this->currentTeam}/posts/{$number}", [
115
            'json' => [
116
                'post' => $data,
117
            ],
118
        ]);
119
    }
120
121
    /**
122
     * @param $number
123
     *
124
     * @return \Psr\Http\Message\ResponseInterface
125
     */
126
    public function deletePost($number)
127
    {
128
        return $this->httpClient->request('DELETE', "teams/{$this->currentTeam}/posts/{$number}");
129
    }
130
131
    /**
132
     * @param int   $number
133
     * @param array $params
134
     *
135
     * @return \Psr\Http\Message\ResponseInterface
136
     */
137
    public function comments($number = null, $params = [])
138
    {
139
        if (empty($number)) {
140
            return $this->httpClient->request('GET', "teams/{$this->currentTeam}/comments", [
141
                'query' => $params,
142
            ]);
143
        }
144
145
        return $this->httpClient->request('GET', "teams/{$this->currentTeam}/posts/{$number}/comments", [
146
            'query' => $params,
147
        ]);
148
    }
149
150
    /**
151
     * @param       $commentId
152
     * @param array $params
153
     *
154
     * @return \Psr\Http\Message\ResponseInterface
155
     */
156
    public function comment($commentId, $params = [])
157
    {
158
        return $this->httpClient->request('GET', "teams/{$this->currentTeam}/comments/{$commentId}", [
159
            'query' => $params,
160
        ]);
161
    }
162
163
    /**
164
     * @param $postNumber
165
     * @param $data
166
     *
167
     * @return \Psr\Http\Message\ResponseInterface
168
     */
169
    public function createComment($postNumber, $data)
170
    {
171
        return $this->httpClient->request('POST', "teams/{$this->currentTeam}/posts/{$postNumber}/comments", [
172
            'json' => [
173
                'comment' => $data,
174
            ],
175
        ]);
176
    }
177
178
    /**
179
     * @param $commentId
180
     * @param $data
181
     *
182
     * @return \Psr\Http\Message\ResponseInterface
183
     */
184
    public function updateComment($commentId, $data)
185
    {
186
        return $this->httpClient->request('PATCH', "teams/{$this->currentTeam}/comments/{$commentId}", [
187
            'json' => [
188
                'comment' => $data,
189
            ],
190
        ]);
191
    }
192
193
    /**
194
     * @param $commentId
195
     *
196
     * @return \Psr\Http\Message\ResponseInterface
197
     */
198
    public function deleteComment($commentId)
199
    {
200
        return $this->httpClient->request('DELETE', "teams/{$this->currentTeam}/comments/{$commentId}");
201
    }
202
203
    /**
204
     * @param $postNumber
205
     * @return \Psr\Http\Message\ResponseInterface
206
     */
207
    public function createSharing($postNumber)
208
    {
209
        return $this->httpClient->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/sharing"));
210
    }
211
212
    /**
213
     * @param $postNumber
214
     * @return \Psr\Http\Message\ResponseInterface
215
     */
216
    public function deleteSharing($postNumber)
217
    {
218
        return $this->httpClient->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/sharing"));
219
    }
220
221
    /**
222
     * @param       $postNumber
223
     * @param array $params
224
     *
225
     * @return \Psr\Http\Message\ResponseInterface
226
     */
227
    public function postStargazers($postNumber, $params = [])
228
    {
229
        return $this->httpClient->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/stargazers"), [
230
            'query' => $params,
231
        ]);
232
    }
233
234
    /**
235
     * @param       $postNumber
236
     * @param array $params
237
     *
238
     * @return \Psr\Http\Message\ResponseInterface
239
     */
240
    public function addPostStar($postNumber, $params = [])
241
    {
242
        return $this->httpClient->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/star"), [
243
            'json' => $params,
244
        ]);
245
    }
246
247
    /**
248
     * @param $postNumber
249
     *
250
     * @return \Psr\Http\Message\ResponseInterface
251
     */
252
    public function deletePostStar($postNumber)
253
    {
254
        return $this->httpClient->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/star"));
255
    }
256
257
    /**
258
     * @param       $commentId
259
     * @param array $params
260
     *
261
     * @return \Psr\Http\Message\ResponseInterface
262
     */
263
    public function commentStargazers($commentId, $params = [])
264
    {
265
        return $this->httpClient->request('GET', $this->getCurrentTeamUrl("comments/{$commentId}/star"), [
266
            'query' => $params,
267
        ]);
268
    }
269
270
    /**
271
     * @param       $commentId
272
     * @param array $params
273
     *
274
     * @return \Psr\Http\Message\ResponseInterface
275
     */
276
    public function addCommentStar($commentId, $params = [])
277
    {
278
        return $this->httpClient->request('POST', $this->getCurrentTeamUrl("comments/{$commentId}/star"), [
279
            'json' => $params,
280
        ]);
281
    }
282
283
    /**
284
     * @param $commentId
285
     *
286
     * @return \Psr\Http\Message\ResponseInterface
287
     */
288
    public function deleteCommentStar($commentId)
289
    {
290
        return $this->httpClient->request('DELETE', $this->getCurrentTeamUrl("comments/{$commentId}/star"));
291
    }
292
293
    /**
294
     * @param       $postNumber
295
     * @param array $params
296
     *
297
     * @return \Psr\Http\Message\ResponseInterface
298
     */
299
    public function watchers($postNumber, $params = [])
300
    {
301
        return $this->httpClient->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/watchers"), [
302
            'query' => $params,
303
        ]);
304
    }
305
306
    /**
307
     * @param $postNumber
308
     *
309
     * @return \Psr\Http\Message\ResponseInterface
310
     */
311
    public function addWatch($postNumber)
312
    {
313
        return $this->httpClient->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/watch"));
314
    }
315
316
    /**
317
     * @param $postNumber
318
     *
319
     * @return \Psr\Http\Message\ResponseInterface
320
     */
321
    public function deleteWatch($postNumber)
322
    {
323
        return $this->httpClient->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/watch"));
324
    }
325
326
    /**
327
     * @return \Psr\Http\Message\ResponseInterface
328
     */
329
    public function categories()
330
    {
331
        return $this->httpClient->request('GET', $this->getCurrentTeamUrl('categories'));
332
    }
333
334
    /**
335
     * @param array $params
336
     *
337
     * @return \Psr\Http\Message\ResponseInterface
338
     */
339
    public function batchMoveCategory($params = [])
340
    {
341
        return $this->httpClient->request('POST', $this->getCurrentTeamUrl('categories/batch_move'), [
342
            'json' => $params,
343
        ]);
344
    }
345
346
    /**
347
     * @return \Psr\Http\Message\ResponseInterface
348
     */
349
    public function tags()
350
    {
351
        return $this->httpClient->request('GET', $this->getCurrentTeamUrl('tags'));
352
    }
353
354
    /**
355
     * @return \Psr\Http\Message\ResponseInterface
356
     */
357
    public function invitation()
358
    {
359
        return $this->httpClient->request('GET', $this->getCurrentTeamUrl('invitation'));
360
    }
361
362
    /**
363
     * @return \Psr\Http\Message\ResponseInterface
364
     */
365
    public function regenerateInvitation()
366
    {
367
        return $this->httpClient->request('POST', $this->getCurrentTeamUrl('invitation_regenerator'));
368
    }
369
370
    /**
371
     * @param $params
372
     *
373
     * @return \Psr\Http\Message\ResponseInterface
374
     */
375
    public function pendingInvitations(array $params)
376
    {
377
        return $this->httpClient->request('GET', $this->getCurrentTeamUrl('invitations'), [
378
            'query' => $params,
379
        ]);
380
    }
381
382
    /**
383
     * @param array $data
384
     *
385
     * @return \Psr\Http\Message\ResponseInterface
386
     */
387
    public function sendInvitation(array $data)
388
    {
389
        return $this->httpClient->request('POST', $this->getCurrentTeamUrl('invitations'), [
390
            'json' => [
391
                'members' => $data,
392
            ],
393
        ]);
394
    }
395
396
    /**
397
     * @param string $code
398
     *
399
     * @return \Psr\Http\Message\ResponseInterface
400
     */
401
    public function cancelInvitation($code)
402
    {
403
        return $this->httpClient->request('DELETE', $this->getCurrentTeamUrl("invitations/{$code}"));
404
    }
405
406
    /**
407
     * @param array $params
408
     *
409
     * @return \Psr\Http\Message\ResponseInterface
410
     */
411
    public function emojis(array $params = [])
412
    {
413
        return $this->httpClient->request('GET', "teams/{$this->currentTeam}/emojis", [
414
            'query' => $params,
415
        ]);
416
    }
417
418
    /**
419
     * @param $data
420
     *
421
     * @return \Psr\Http\Message\ResponseInterface
422
     */
423
    public function createEmoji($data)
424
    {
425
        return $this->httpClient->request('POST', "teams/{$this->currentTeam}/emojis", [
426
            'json' => [
427
                'emoji' => $data,
428
            ],
429
        ]);
430
    }
431
432
    /**
433
     * @param $code
434
     *
435
     * @return \Psr\Http\Message\ResponseInterface
436
     */
437
    public function deleteEmoji($code)
438
    {
439
        return $this->httpClient->request('DELETE', "teams/{$this->currentTeam}/emojis/{$code}");
440
    }
441
442
    private function getCurrentTeamUrl($path = '')
443
    {
444
        return "teams/{$this->currentTeam}/$path";
445
    }
446
}
447