1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Helpers; |
4
|
|
|
|
5
|
|
|
class GistFinder |
6
|
|
|
{ |
7
|
|
|
private $gistsApi; |
8
|
|
|
|
9
|
|
|
private $paginator; |
10
|
|
|
|
11
|
|
|
public function __construct(\Github\Client $githubClient, \Github\ResultPager $paginator) |
12
|
|
|
{ |
13
|
|
|
$githubClient->authenticate(\Auth::user()->token, null, \Github\Client::AUTH_HTTP_TOKEN); |
14
|
|
|
|
15
|
|
|
$this->gistsApi = $githubClient->api('gists'); |
16
|
|
|
|
17
|
|
|
$this->paginator = $paginator; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function fetchGistsAndTags() |
21
|
|
|
{ |
22
|
|
|
// fetch user and starred gists |
23
|
|
|
$rawUserGists = collect($this->paginator->fetchAll($this->gistsApi, 'all')) |
24
|
|
|
->keyBy('id') |
25
|
|
|
->toArray(); |
26
|
|
|
$rawStarredGists = collect($this->paginator->fetchAll($this->gistsApi, 'all', ['starred'])) |
27
|
|
|
->keyBy('id') |
28
|
|
|
->toArray(); |
29
|
|
|
|
30
|
|
|
// add starred label to starred gists |
31
|
|
|
foreach ($rawStarredGists as $key => $gist) { |
32
|
|
|
$rawStarredGists[$key]['starred'] = true; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// merge user and starred gists |
36
|
|
|
$rawMergedGists = $rawStarredGists + $rawUserGists; |
37
|
|
|
|
38
|
|
|
// prepare gists and tags arrays |
39
|
|
|
$gists = []; |
40
|
|
|
$tags = []; |
41
|
|
|
foreach ($rawMergedGists as $gist) { |
42
|
|
|
// separate description and tags |
43
|
|
|
$descAndTags = $this->separateDescriptionAndTags($gist['description']); |
44
|
|
|
|
45
|
|
|
// set gist owner |
46
|
|
|
$gistOwner = isset($gist['owner']) ? $gist['owner']['login'] : 'anonymous'; |
47
|
|
|
|
48
|
|
|
// set gist type |
49
|
|
|
$gistType = []; |
50
|
|
|
if (isset($gist['starred'])) { |
51
|
|
|
$gistType[] = '@starred'; |
52
|
|
|
} |
53
|
|
|
if (\Auth::user()->nickname == $gistOwner) { |
54
|
|
|
$gistType[] = '@owned'; |
55
|
|
|
} |
56
|
|
|
if ($gist['public']) { |
57
|
|
|
$gistType[] = '@public'; |
58
|
|
|
} else { |
59
|
|
|
$gistType[] = '@private'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// prepare files array |
63
|
|
|
$files = []; |
64
|
|
|
foreach ($gist['files'] as $file) { |
65
|
|
|
$files[] = [ |
66
|
|
|
'filename' => $file['filename'], |
67
|
|
|
'raw_url' => $file['raw_url'] |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// add gist to gists array |
72
|
|
|
$gists[] = [ |
73
|
|
|
'description' => $descAndTags['description'] ? $descAndTags['description'] : 'No description', |
74
|
|
|
'tags' => $descAndTags['tags'], |
75
|
|
|
'owner' => $gistOwner, |
76
|
|
|
'created' => date('Y-m-d H:i:s', strtotime($gist['created_at'])), |
77
|
|
|
'updated' => date('Y-m-d H:i:s', strtotime($gist['updated_at'])), |
78
|
|
|
'type' => $gistType, |
79
|
|
|
'html_url' => $gist['html_url'], |
80
|
|
|
'files' => $files |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
// add tags to tags array |
84
|
|
|
foreach ($descAndTags['tags'] as $tag) { |
85
|
|
|
if (!in_array($tag, $tags)) { |
86
|
|
|
$tags[] = $tag; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// sort tags |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return compact('gists', 'tags'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Separate gist description and tags |
99
|
|
|
* |
100
|
|
|
* Seeks tags in original gist description and saves them and description separately |
101
|
|
|
* |
102
|
|
|
* @param string $descriptionWithTags Gist description with optional tags |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
private function separateDescriptionAndTags($descriptionWithTags) |
106
|
|
|
{ |
107
|
|
|
$tags = []; |
108
|
|
|
$description = trim( |
109
|
|
|
preg_replace_callback( |
110
|
|
|
'~#\w+~', |
111
|
|
|
function ($matches) use (&$tags) { |
112
|
|
|
$tags[] = $matches[0]; |
113
|
|
|
}, |
114
|
|
|
$descriptionWithTags |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
if (empty($tags)) { |
119
|
|
|
$tags[] = '#none'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return compact('description', 'tags'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|