Gists::listPublicGists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace FlexyProject\GitHub\Receiver;
3
4
use DateTime;
5
use Symfony\Component\HttpFoundation\Request;
6
7
/**
8
 * This class provides access to Gists API.
9
 *
10
 * @link    https://developer.github.com/v3/gists/
11
 * @package FlexyProject\GitHub\Receiver
12
 */
13
class Gists extends AbstractReceiver
14
{
15
16
    /** Available sub-Receiver */
17
    const COMMENTS = 'Comments';
18
19
    /**
20
     * List a user's gists
21
     *
22
     * @link https://developer.github.com/v3/gists/#list-a-users-gists
23
     *
24
     * @param string $username
25
     * @param string $since
26
     *
27
     * @return array
28
     */
29
    public function listGists(string $username = null, string $since = '1970-01-01'): array
30
    {
31
        $url = '/gists';
32
        if (null !== $username) {
33
            $url = '/users/:username/gists';
34
        }
35
36
        return $this->getApi()->request($this->getApi()->sprintf(':url?:arg', $url, $username,
37
            http_build_query(['since' => (new DateTime($since))->format(DateTime::ATOM)])));
38
    }
39
40
    /**
41
     * List all public gists:
42
     *
43
     * @link https://developer.github.com/v3/gists/#list-all-public-gists
44
     *
45
     * @param string $since
46
     *
47
     * @return array
48
     */
49
    public function listPublicGists(string $since = '1970-01-01'): array
50
    {
51
        return $this->getApi()->request($this->getApi()->sprintf('/gists/public?:arg',
52
            http_build_query(['since' => (new DateTime($since))->format(DateTime::ATOM)])));
53
    }
54
55
    /**
56
     * List the authenticated user’s starred gists
57
     *
58
     * @link https://developer.github.com/v3/gists/#list-starred-gists
59
     *
60
     * @param string $since
61
     *
62
     * @return array
63
     */
64
    public function listUsersStarredGists(string $since = '1970-01-01'): array
65
    {
66
        return $this->getApi()->request($this->getApi()->sprintf('/gists/starred?:arg',
67
            http_build_query(['since' => (new DateTime($since))->format(DateTime::ATOM)])));
68
    }
69
70
    /**
71
     * Get a single gist
72
     *
73
     * @link https://developer.github.com/v3/gists/#get-a-single-gist
74
     *
75
     * @param string $id
76
     *
77
     * @return array
78
     */
79
    public function getGist(string $id): array
80
    {
81
        return $this->getApi()->request($this->getApi()->sprintf('/gists/:id', $id));
82
    }
83
84
    /**
85
     * Get a specific revision of a gist
86
     *
87
     * @link https://developer.github.com/v3/gists/#get-a-specific-revision-of-a-gist
88
     *
89
     * @param string $id
90
     * @param string $sha
91
     *
92
     * @return array
93
     * @throws \Exception
94
     */
95
    public function getGistRevision(string $id, string $sha): array
96
    {
97
        return $this->getApi()->request($this->getApi()->sprintf('/gists/:id/:sha', $id, $sha));
98
    }
99
100
    /**
101
     * Create a gist
102
     *
103
     * @link https://developer.github.com/v3/gists/#create-a-gist
104
     *
105
     * @param array  $files
106
     * @param string $description
107
     * @param bool   $public
108
     *
109
     * @return array
110
     */
111
    public function createGist(array $files, string $description = null, bool $public = false): array
112
    {
113
        return $this->getApi()->request($this->getApi()->sprintf('/gists'), Request::METHOD_POST, [
114
            'files'       => $files,
115
            'description' => $description,
116
            'public'      => $public
117
        ]);
118
    }
119
120
    /**
121
     * Edit a gist
122
     *
123
     * @link https://developer.github.com/v3/gists/#edit-a-gist
124
     *
125
     * @param string $id
126
     * @param string $description
127
     * @param array  $files
128
     * @param string $content
129
     * @param string $filename
130
     *
131
     * @return array
132
     */
133
    public function editGist(string $id, string $description = '', array $files = [], string $content = '',
134
                             string $filename = ''): array
135
    {
136
        return $this->getApi()->request($this->getApi()->sprintf('/gists/:id', $id), Request::METHOD_PATCH, [
137
            'description' => $description,
138
            'files'       => $files,
139
            'content'     => $content,
140
            'filename'    => $filename
141
        ]);
142
    }
143
144
    /**
145
     * List gist commits
146
     *
147
     * @link https://developer.github.com/v3/gists/#list-gist-commits
148
     *
149
     * @param string $id
150
     *
151
     * @return array
152
     */
153
    public function listGistsCommits(string $id): array
154
    {
155
        return $this->getApi()->request($this->getApi()->sprintf('/gists/:id/commits', $id));
156
    }
157
158
    /**
159
     * Star a gist
160
     *
161
     * @link https://developer.github.com/v3/gists/#star-a-gist
162
     *
163
     * @param string $id
164
     *
165
     * @return bool
166
     */
167 View Code Duplication
    public function starGist(string $id): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
168
    {
169
        $this->getApi()->request($this->getApi()->sprintf('/gists/:id/star', $id), Request::METHOD_PUT);
170
171
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
172
            return true;
173
        }
174
175
        return false;
176
    }
177
178
    /**
179
     * Unstar a gist
180
     *
181
     * @link https://developer.github.com/v3/gists/#unstar-a-gist
182
     *
183
     * @param string $id
184
     *
185
     * @return bool
186
     */
187 View Code Duplication
    public function unStarGist(string $id): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
188
    {
189
        $this->getApi()->request($this->getApi()->sprintf('/gists/:id/star', $id), Request::METHOD_DELETE);
190
191
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
192
            return true;
193
        }
194
195
        return false;
196
    }
197
198
    /**
199
     * Check if a gist is starred
200
     *
201
     * @link https://developer.github.com/v3/gists/#check-if-a-gist-is-starred
202
     *
203
     * @param string $id
204
     *
205
     * @return bool
206
     */
207 View Code Duplication
    public function checkGistIsStarred(string $id): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
208
    {
209
        $this->getApi()->request($this->getApi()->sprintf('/gists/:id/star', $id));
210
211
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
212
            return true;
213
        }
214
215
        return false;
216
    }
217
218
    /**
219
     * Fork a gist
220
     *
221
     * @link https://developer.github.com/v3/gists/#fork-a-gist
222
     *
223
     * @param string $id
224
     *
225
     * @return array
226
     */
227
    public function forkGist(string $id): array
228
    {
229
        return $this->getApi()->request($this->getApi()->sprintf('/gists/:id/forks', $id), Request::METHOD_POST);
230
    }
231
232
    /**
233
     * List gist forks
234
     *
235
     * @link https://developer.github.com/v3/gists/#list-gist-forks
236
     *
237
     * @param string $id
238
     *
239
     * @return array
240
     */
241
    public function listGistForks(string $id): array
242
    {
243
        return $this->getApi()->request($this->getApi()->sprintf('/gists/:id/forks', $id));
244
    }
245
246
    /**
247
     * Delete a gist
248
     *
249
     * @link https://developer.github.com/v3/gists/#delete-a-gist
250
     *
251
     * @param string $id
252
     *
253
     * @return bool
254
     */
255 View Code Duplication
    public function deleteGist(string $id): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
256
    {
257
        $this->getApi()->request($this->getApi()->sprintf('/gists/:id', $id), Request::METHOD_DELETE);
258
259
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
260
            return true;
261
        }
262
263
        return false;
264
    }
265
}