GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

GistFinder   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
lcom 2
cbo 5
dl 0
loc 156
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
C getGistsAndTags() 0 60 10
A fetchRawGists() 0 20 2
A getGistFileContents() 0 4 1
A __construct() 0 11 1
A separateDescriptionAndTags() 0 19 2
1
<?php
2
3
namespace App\Helpers;
4
5
class GistFinder
6
{
7
    /** @var \Github\Api\ApiInterface $gistsApi */
8
    private $gistsApi;
9
10
    /** @var \Github\ResultPager $paginator */
11
    private $paginator;
12
13
    /** @var \GuzzleHttp\Client $guzzle */
14
    private $guzzle;
15
16
    public function __construct(
17
        \Github\Client $githubClient,
18
        \Github\ResultPager $paginator,
19
        \GuzzleHttp\Client $guzzleClient
20
    ) {
21
        $githubClient->authenticate(\Auth::user()->token, null, \Github\Client::AUTH_HTTP_TOKEN);
22
23
        $this->gistsApi = $githubClient->api('gists');
24
        $this->paginator = $paginator;
25
        $this->guzzle = $guzzleClient;
26
    }
27
28
    /**
29
     * Get gists and tags.
30
     *
31
     * @return array `['gists' => [...], 'tags' => [...]]`
32
     */
33
    public function getGistsAndTags()
34
    {
35
        $rawMergedGists = $this->fetchRawGists();
36
37
        // prepare gists and tags arrays
38
        $gists = [];
39
        $tags = [];
40
        foreach ($rawMergedGists as $gist) {
41
            // separate description and tags
42
            $descAndTags = $this->separateDescriptionAndTags($gist['description']);
43
44
            // set gist owner
45
            $gistOwner = isset($gist['owner']) ? $gist['owner']['login'] : 'anonymous';
46
47
            // set gist type
48
            $gistType = [];
49
            if (isset($gist['starred'])) {
50
                $gistType[] = '@starred';
51
            }
52
            if (\Auth::user()->nickname == $gistOwner) {
53
                $gistType[] = '@owned';
54
            }
55
            if ($gist['public']) {
56
                $gistType[] = '@public';
57
            } else {
58
                $gistType[] = '@private';
59
            }
60
61
            // prepare files array
62
            $files = [];
63
            foreach ($gist['files'] as $file) {
64
                $files[] = [
65
                    'filename' => $file['filename'],
66
                    'raw_url'  => $file['raw_url']
67
                ];
68
            }
69
70
            // add gist to gists array
71
            $gists[] = [
72
                'id'          => $gist['id'],
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
91
        return compact('gists', 'tags');
92
    }
93
94
    /**
95
     * Fetch raw gists from GitHub.
96
     *
97
     * @return array
98
     */
99
    public function fetchRawGists()
100
    {
101
        // fetch user and starred gists
102
        $rawUserGists = collect($this->paginator->fetchAll($this->gistsApi, 'all'))
103
            ->keyBy('id')
104
            ->toArray();
105
        $rawStarredGists = collect($this->paginator->fetchAll($this->gistsApi, 'all', ['starred']))
106
            ->keyBy('id')
107
            ->toArray();
108
109
        // add starred label to starred gists
110
        foreach ($rawStarredGists as $key => $gist) {
111
            $rawStarredGists[$key]['starred'] = true;
112
        }
113
114
        // merge user and starred gists
115
        $rawMergedGists = $rawStarredGists + $rawUserGists;
116
117
        return $rawMergedGists;
118
    }
119
120
    /**
121
     * Get the content of a Gist file.
122
     *
123
     * @param string $url
124
     *
125
     * @return string
126
     */
127
    public function getGistFileContents($url)
128
    {
129
        return (string)$this->guzzle->get($url)->getBody();
130
    }
131
132
    /**
133
     * Separate gist description and tags.
134
     *
135
     * Seeks tags in original gist description and saves them and description separately.
136
     *
137
     * @param string $descriptionWithTags Gist description with optional tags.
138
     *
139
     * @return array `['description' => '...', 'tags' => [...]]`
140
     */
141
    private function separateDescriptionAndTags($descriptionWithTags)
142
    {
143
        $tags = [];
144
        $description = trim(
145
            preg_replace_callback(
146
                '~(?:^|\s)(#\S+)~',
147
                function ($matches) use (&$tags) {
148
                    $tags[] = $matches[1];
149
                },
150
                $descriptionWithTags
151
            )
152
        );
153
154
        if (empty($tags)) {
155
            $tags[] = '#none';
156
        }
157
158
        return compact('description', 'tags');
159
    }
160
}
161