GitHubAPI   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 9
Bugs 3 Features 2
Metric Value
wmc 5
c 9
b 3
f 2
lcom 0
cbo 5
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getNumberOfRepos() 0 11 2
A getGithubInfo() 0 15 3
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
10
namespace Lib;
11
12
use Lib\Exceptions\NullUserException;
13
use Lib\Exceptions\UserNotFoundException;
14
use GuzzleHttp\Client;
15
use GuzzleHttp\Exception\ClientException;
16
17
/**
18
 * @category Class
19
 * @package  Lib\EvangelistStatus
20
 */
21
class GitHubAPI
22
{
23
    /**
24
     * Returns the number of public repos a developer has on GitHub
25
     *
26
     * @param  string $username    GitHub username
27
     * @return mixed
28
     * @throws NullUserException
29
     */
30
    public static function getNumberOfRepos($username)
31
    {
32
        if (empty(trim($username))) {
33
            throw new NullUserException("A valid username is required.");
34
        }
35
36
        $githubInfo = self::getGithubInfo($username);
37
        $obj = json_decode($githubInfo);
38
39
        return $obj->{"public_repos"};
40
    }
41
42
    /**
43
     * Returns the developer's public GitHub information in JSON
44
     *
45
     * @param  string $username    The username whose information is to be retrieved
46
     * @return string
47
     * @throws UserNotFoundException
48
     */
49
    private static function getGithubInfo($username)
50
    {
51
        $client = new Client(["base_uri" => "https://api.github.com/users/"]);
52
53
        try {
54
            $response = $client->get($username);
55
            return $response->getBody(true);
56
        } catch (ClientException $e) {
57
            if ($e->getCode() === 404) {
58
                throw new UserNotFoundException(
59
                    "The user " . $username . " was not found on GitHub."
60
                );
61
            }
62
        }
63
    }
64
}
65