Completed
Push — master ( 159c3f...20dece )
by Andrew
03:17
created

GitHubAPI::getGithubInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 12
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
1
<?php
2
/**
3
 * @file     GitHubAPI.php
4
 * This file is a class that retrieves GitHub API information
5
 * @package  Lib\EvangelistStatus
6
 * @author   andrew <[email protected]>
7
 * @license  MIT => https://opensource.org/licenses/MIT
8
 */
9
namespace Lib;
10
11
use Lib\Exceptions\NullUserException;
12
13
/**
14
 * @category Class
15
 * @package  Lib\EvangelistStatus
16
 */
17
class GitHubAPI
18
{
19
    /**
20
     * Returns the number of public repos a developer has on GitHub
21
     *
22
     * @param  string $username GitHub username
23
     * @return mixed
24
     * @throws NullUserException
25
     */
26
    public static function getNumberOfRepos($username)
27
    {
28
        if (empty(trim($username))) {
29
            throw new NullUserException("A username is required");
30
        }
31
32
        $url = 'https://api.github.com/users/' . $username;
33
        $json = self::getGithubInfo($url, "evangelist-status");
34
        $obj = json_decode($json);
35
36
        return $obj->{'public_repos'};
37
    }
38
39
    /**
40
     * Returns the developer's public GitHub information in JSON
41
     *
42
     * @param  string $url       The URL to retrieve API information from
43
     * @param  string $useragent The user agent or app name required by GitHub
44
     * @return string
45
     */
46
    private static function getGithubInfo($url, $useragent) 
47
    {
48
        // use guzzle, gouete
49
        $curl_handle = curl_init();
50
        curl_setopt($curl_handle, CURLOPT_URL, $url);
51
        curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); 
52
        curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 10);
53
        curl_setopt($curl_handle, CURLOPT_USERAGENT, $useragent);
54
        $content = curl_exec($curl_handle);
55
        curl_close($curl_handle);
56
        return $content;
57
    }
58
}
59