Passed
Push — master ( 8005c7...9b803d )
by Goffy
03:14
created

GithubClient::testMilo2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 18
rs 10
c 0
b 0
f 0
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 object
52
     */
53
    private $helper = null;
54
55
    /**
56
     * Constructor
57
     *
58
     * @param null
59
     */
60
    public function __construct()
61
    {
62
        $this->helper = \XoopsModules\Wggithub\Helper::getInstance();
63
        $this->getSetting();
64
    }
65
66
    /**
67
     * @static function &getInstance
68
     *
69
     * @param null
70
     * @return GitHubClient of Api
71
     */
72
    public static function getInstance()
73
    {
74
        static $instance = false;
75
        if (!$instance) {
76
            $instance = new self();
77
        }
78
79
        return $instance;
80
    }
81
82
    public function testMilo($url) {
83
        $api = new Github\Api;
84
        $response = $api->get(static::BASE_URL . $url);
85
        $data = $api->decode($response);
86
        
87
        return $data;
88
    }
89
90
    public function testMilo2($url) {
91
        $api = new Github\Api;
92
93
        $token = new Github\OAuth\Token('{myKey}', 'bearer', ['repo', 'user', 'public_repo']);
94
        $api->setToken($token);
95
        $response = $api->get(static::BASE_URL . $url);
96
97
        $data = $api->decode($response);
98
99
        /*
100
        $api = new Github\Api;
101
102
        $request = $api->createRequest('GET', $url, [], [], '');
103
        $response = $api->request($request);
104
        $data = (array)$api->decode($response);
105
        */
106
107
        return $data;
108
    }
109
110
    /**
111
     * Get repositories of given user
112
     *
113
     * @param     $username
114
     * @param int $per_page
115
     * @param int $page
116
     * @return array
117
     */
118
    public function getUserRepositories($username, $per_page = 100, $page = 1)
119
    {
120
        $url = static::BASE_URL . 'users/' . \rawurlencode($username) . '/repos?per_page=' . $per_page . '&page=' . $page;
121
122
        return $this->_get($url);
123
    }
124
125
    /**
126
     * Get repositories of given organisation
127
     *
128
     * @param     $org
129
     * @param int $per_page
130
     * @param int $page
131
     * @return array
132
     */
133
    public function getOrgRepositories($org, $per_page = 100, $page = 1)
134
    {
135
        $url = static::BASE_URL . 'orgs/' . \rawurlencode($org) . '/repos?per_page=' . $per_page . '&page=' . $page;
136
137
        return $this->_get($url);
138
    }
139
140
    /**
141
     * Get the readme content for a repository by its username and repository name.
142
     *
143
     * @param string $username   the user who owns the repository
144
     * @param string $repository the name of the repository
145
     * @return string|array the readme content
146
     */
147
    public function getReadme($username, $repository)
148
    {
149
        $url = static::BASE_URL . 'repos/' . \rawurlencode($username) . '/' . \rawurlencode($repository) . '/readme';
150
151
        return $this->_get($url);
152
    }
153
154
    /**
155
     * Get all releases
156
     *
157
     * @param string $username   the user who owns the repository
158
     * @param string $repository the name of the repository
159
     * @return array
160
     */
161
    public function getReleases($username, $repository)
162
    {
163
        $url = static::BASE_URL . 'repos/' . $username . '/' . $repository . '/releases';
164
165
        return $this->_get($url);
166
    }
167
168
    /**
169
     * Get latest release
170
     *
171
     * @param string $username   the user who owns the repository
172
     * @param string $repository the name of the repository
173
     * @param bool   $prerelease
174
     * @return array
175
     */
176
    public function getLatestRelease($username, $repository, $prerelease = false)
177
    {
178
        if ($prerelease) {
179
            $url = static::BASE_URL . 'repos/' . $username . '/' . $repository . '/releases';
180
        } else {
181
            $url = static::BASE_URL . 'repos/' . $username . '/' . $repository . '/releases/latest';
182
        }
183
        $result = $this->_get($url);
184
185
        if ($prerelease) {
186
            if (\is_array($result)) {
0 ignored issues
show
introduced by
The condition is_array($result) is always true.
Loading history...
187
                return $result[0];
188
            } else {
189
                return [];
190
            }
191
        }
192
193
        return $result;
194
    }
195
196
    /**
197
     * Get github content
198
     *
199
     * @param $url
200
     * @return array
201
     */
202
    public function _get($url)
203
    {
204
        $error = false;
205
206
        $logsHandler = $this->helper->getHandler('Logs');
207
        $logsHandler->updateTableLogs(Constants::LOG_TYPE_REQUEST, $url, 'START');
208
209
        $api = new Github\Api;
210
        $token = new Github\OAuth\Token($this->tokenAuth, 'bearer', ['repo', 'user', 'public_repo']);
211
        $api->setToken($token);
212
        $response = $api->get($url);
213
        $code = $response->getCode();
214
        if (\in_array($code, [200, 201], true)) {
215
            $logsHandler->updateTableLogs(Constants::LOG_TYPE_REQUEST, $url, 'OK');
216
        } else {
217
            $error = true;
218
            $errMsg = $response->getContent();
219
            $logsHandler->updateTableLogs(Constants::LOG_TYPE_ERROR, $errMsg, 'ERROR ' . $code);
220
        }
221
        if ($error) {
222
            //catch common errors
223
            switch ($code) {
224
                case 401:
225
                    throw new \RuntimeException('"' . \_MA_WGGITHUB_READGH_ERROR_API_401 . '"');
226
                    break;
227
                case 403:
228
                    /*
229
                    if (\strpos($errMsg, 'API rate limit exceeded') > 0) {
230
                        $GLOBALS['xoopsTpl']->assign('apiexceed', true);
231
                    }
232
                    */
233
                    throw new \RuntimeException('"' . \_MA_WGGITHUB_READGH_ERROR_API_403 . '"');
234
                    break;
235
                case 404:
236
                    throw new \RuntimeException('"' . \_MA_WGGITHUB_READGH_ERROR_API_404 . '"');
237
                    break;
238
                case 405:
239
                    throw new \RuntimeException('"' . \_MA_WGGITHUB_READGH_ERROR_API_405 . '"');
240
                    break;
241
                case 0:
242
                default:
243
                    throw new \RuntimeException('"' . \_MA_WGGITHUB_READGH_ERROR_API . $errMsg . '"');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $errMsg does not seem to be defined for all execution paths leading up to this point.
Loading history...
244
                    break;
245
            }
246
        }
247
        $data = (array)$api->decode($response);
248
249
        return $data;
250
    }
251
252
    /**
253
     * Get primary setting
254
     *
255
     * @return bool|array
256
     */
257
    private function getSetting()
258
    {
259
        $settingsHandler = $this->helper->getHandler('Settings');
260
        $setting = $settingsHandler->getPrimarySetting();
261
262
        if (0 == \count($setting)) {
263
            \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

263
            /** @scrutinizer ignore-call */ 
264
            \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...
264
        }
265
        //$this->userAuth = $setting['user'];
266
        $this->tokenAuth = $setting['token'];
267
268
        return true;
269
    }
270
}
271