Passed
Push — master ( 0deb8f...f54565 )
by Goffy
04:13
created

GithubClient   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 192
rs 10
c 0
b 0
f 0
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserRepositories() 0 5 1
A getSetting() 0 12 2
A getLatestRelease() 0 18 4
A getOrgRepositories() 0 5 1
A getReleases() 0 5 1
A __construct() 0 4 1
A _get() 0 13 1
A getInstance() 0 8 2
A getReadme() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Wggithub\Github;
6
7
/*
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
/**
17
 * @copyright    XOOPS Project https://xoops.org/
18
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
19
 * @since
20
 * @author       Goffy - XOOPS Development Team
21
 */
22
23
use XoopsModules\Wggithub;
24
use XoopsModules\Wggithub\{
25
    Helper,
26
    Constants,
27
    Github
28
};
29
30
/**
31
 * Class GithubClient
32
 */
33
class GithubClient extends Api
34
{
35
    /**
36
     * @var string
37
     */
38
    public const BASE_URL = 'https://api.github.com/';
39
40
    /**
41
     * @var string
42
     */
43
    public $userAuth = 'myusername';
44
45
    /**
46
     * @var string
47
     */
48
    public $tokenAuth = 'mytoken';
49
50
    /**
51
     * @var string
52
     */
53
    public $requestType = 'curl';
54
55
    /**
56
     * Verbose debugging for curl (when putting)
57
     * @var bool
58
     */
59
    public $debug = false;
60
61
    /**
62
     * @var object
63
     */
64
    private $helper = null;
65
66
    /**
67
     * @var bool
68
     */
69
    public $apiErrorLimit = false;
70
71
    /**
72
     * @var bool
73
     */
74
    public $apiErrorMisc = false;
75
76
    /**
77
     * Constructor
78
     *
79
     * @param null
80
     */
81
    public function __construct()
82
    {
83
        $this->helper = \XoopsModules\Wggithub\Helper::getInstance();
84
        $this->getSetting();
85
    }
86
87
    /**
88
     * @static function &getInstance
89
     *
90
     * @param null
91
     * @return GitHubClient of Api
92
     */
93
    public static function getInstance()
94
    {
95
        static $instance = false;
96
        if (!$instance) {
97
            $instance = new self();
98
        }
99
100
        return $instance;
101
    }
102
103
    /**
104
     * Get repositories of given user
105
     *
106
     * @param     $username
107
     * @param int $per_page
108
     * @param int $page
109
     * @return array
110
     */
111
    public function getUserRepositories($username, $per_page = 100, $page = 1)
112
    {
113
        $url = static::BASE_URL . 'users/' . \rawurlencode($username) . '/repos?per_page=' . $per_page . '&page=' . $page;
114
115
        return $this->_get($url);
116
    }
117
118
    /**
119
     * Get repositories of given organisation
120
     *
121
     * @param     $org
122
     * @param int $per_page
123
     * @param int $page
124
     * @return array
125
     */
126
    public function getOrgRepositories($org, $per_page = 100, $page = 1)
127
    {
128
        $url = static::BASE_URL . 'orgs/' . \rawurlencode($org) . '/repos?per_page=' . $per_page . '&page=' . $page;
129
130
        return $this->_get($url);
131
    }
132
133
    /**
134
     * Get the readme content for a repository by its username and repository name.
135
     *
136
     * @link http://developer.github.com/v3/repos/contents/#get-the-readme
137
     *
138
     * @param string $username   the user who owns the repository
139
     * @param string $repository the name of the repository
140
     *
141
     * @return string|array the readme content
142
     */
143
    public function getReadme($username, $repository)
144
    {
145
        $url = static::BASE_URL . 'repos/' . \rawurlencode($username) . '/' . \rawurlencode($repository) . '/readme';
146
147
        return $this->_get($url, false, false);
0 ignored issues
show
Unused Code introduced by
The call to XoopsModules\Wggithub\Github\GithubClient::_get() has too many arguments starting with false. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
        return $this->/** @scrutinizer ignore-call */ _get($url, false, false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
148
    }
149
150
    /**
151
     * Get all releases
152
     *
153
     * @param $user
154
     * @param $repository
155
     * @return array
156
     */
157
    public function getReleases($user, $repository)
158
    {
159
        $url = static::BASE_URL . 'repos/' . $user . '/' . $repository . '/releases';
160
161
        return $this->_get($url);
162
    }
163
164
    /**
165
     * Get latest release
166
     *
167
     * @param $user
168
     * @param $repository
169
     * @param bool $prerelease
170
     * @return array
171
     */
172
    public function getLatestRelease($user, $repository, $prerelease = false)
173
    {
174
        if ($prerelease) {
175
            $url = static::BASE_URL . 'repos/' . $user . '/' . $repository . '/releases';
176
        } else {
177
            $url = static::BASE_URL . 'repos/' . $user . '/' . $repository . '/releases/latest';
178
        }
179
        $result = $this->_get($url);
180
181
        if ($prerelease) {
182
            if (\is_array($result)) {
0 ignored issues
show
introduced by
The condition is_array($result) is always true.
Loading history...
183
                return $result[0];
184
            } else {
185
                return [];
186
            }
187
        }
188
189
        return $result;
190
    }
191
192
    public function _get($url)
193
    {
194
        $logsHandler = $this->helper->getHandler('Logs');
195
        $logsHandler->updateTableLogs(Constants::LOG_TYPE_REQUEST, $url, '');
196
197
        $api = new Github\Api;
198
        $token = new Github\OAuth\Token($this->tokenAuth, 'bearer', ['repo']);
199
        $api->setToken($token);
200
        $request = $api->createRequest('GET', $url, [], [], '');
201
        $response = $api->request($request);
202
        $data = (array)$api->decode($response);
203
204
        return $data;
205
    }
206
207
    /**
208
     * Get primary setting
209
     *
210
     * @param bool $user
211
     * @return bool|array
212
     */
213
    private function getSetting($user = false)
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

213
    private function getSetting(/** @scrutinizer ignore-unused */ $user = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
214
    {
215
        $settingsHandler = $this->helper->getHandler('Settings');
216
        $setting = $settingsHandler->getPrimarySetting();
217
218
        if (0 == \count($setting)) {
219
            \redirect_header(\XOOPS_URL . '/index.php', 3, \_AM_WGGITHUB_THEREARENT_SETTINGS);
0 ignored issues
show
Bug introduced by
The function redirect_header was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

219
            /** @scrutinizer ignore-call */ 
220
            \redirect_header(\XOOPS_URL . '/index.php', 3, \_AM_WGGITHUB_THEREARENT_SETTINGS);
Loading history...
Bug introduced by
The constant XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
220
        }
221
        $this->userAuth = $setting['user'];
222
        $this->tokenAuth = $setting['token'];
223
224
        return true;
225
    }
226
}
227