Completed
Push — master ( 0c057f...83f3fd )
by Christopher
13:35
created

GithubApi   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 8 Features 1
Metric Value
wmc 5
c 8
b 8
f 1
lcom 1
cbo 3
dl 0
loc 45
ccs 16
cts 16
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUsername() 0 4 1
A __construct() 0 7 2
A getRepos() 0 13 2
1
<?php
2
/**
3
 *  @author chris.vundi
4
 *  This class makes a call to Github and returns
5
 *  the number of repos one owns provided a username
6
 *  is provided.
7
 */
8
9
namespace Vundi\Checkpoint1;
10
use GuzzleHttp\Client;
11
use Vundi\Checkpoint1\EvangelizeException;
12
13
class GithubApi
14
{
15
    /**
16
     * Github username
17
     * @var string
18
     */
19
    protected $username;
20
21 5
    public function __construct($username = null)
22
    {
23 5
        $this->username = $username;
24 5
        if (is_null($username)) {
25 1
            throw new EvangelizeException("You have to pass in a username, Username cannot be null", 1);
26
        }
27 5
    }
28
29
    /**
30
     * Get username passed as the parameter
31
     * @return string
32
     */
33 1
    public function getUsername()
34
    {
35 1
        return $this->username;
36
    }
37
38
    /**
39
     * Return an integer representing number of public repos
40
     * the username provided has on github
41
     * @return int
42
     */
43 2
    public function getRepos()
44
    {
45 2
        $url = "https://api.github.com/users/{$this->username}/repos";
46 2
        $client = new Client();
47
        //will return http response with the body in json format
48 2
        $res = $client->request('GET', $url, ['exceptions' => false]);
49 2
        if ($res->getStatusCode() == 404) {
50 1
            throw new EvangelizeException("The username you passed is not a valid Github username", 1);
51
        }
52 1
        $decoded = json_decode($res->getBody(), true);
53 1
        $number = count($decoded);
54 1
        return $number;
55
    }
56
57
}