Completed
Push — master ( 3184f1...72fb6a )
by Christopher
16:36
created

GithubApi   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 87.5%

Importance

Changes 7
Bugs 7 Features 1
Metric Value
wmc 5
c 7
b 7
f 1
lcom 1
cbo 2
dl 0
loc 45
rs 10
ccs 14
cts 16
cp 0.875

3 Methods

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