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

GitHubAPI   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 6
Bugs 2 Features 2
Metric Value
wmc 3
c 6
b 2
f 2
lcom 0
cbo 1
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getNumberOfRepos() 0 12 2
A getGithubInfo() 0 12 1
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